Skip to content

Commit 380faa5

Browse files
committed
bindings: add Sync to Connection
1 parent b1bb5dc commit 380faa5

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Diff for: bindings/rust/s2n-tls/src/callbacks/async_cb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::pin::Pin;
2626
/// if it wants to run an asynchronous operation (disk read, network call).
2727
/// The application can return an error ([Err(Error::application())])
2828
/// to indicate connection failure.
29-
pub trait ConnectionFuture: 'static + Send {
29+
pub trait ConnectionFuture: 'static + Send + Sync {
3030
fn poll(
3131
self: Pin<&mut Self>,
3232
connection: &mut Connection,

Diff for: bindings/rust/s2n-tls/src/callbacks/client_hello.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<F: 'static + Send + Future<Output = Result<Config, Error>>> ConfigResolver<
4848
}
4949
}
5050

51-
impl<F: 'static + Send + Future<Output = Result<Config, Error>>> ConnectionFuture
51+
impl<F: 'static + Send + Sync + Future<Output = Result<Config, Error>>> ConnectionFuture
5252
for ConfigResolver<F>
5353
{
5454
fn poll(

Diff for: bindings/rust/s2n-tls/src/connection.rs

+22
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ impl fmt::Debug for Connection {
6464
/// s2n_connection objects can be sent across threads
6565
unsafe impl Send for Connection {}
6666

67+
/// # Sync
68+
///
69+
/// Although NonNull isn't Sync and allows access to mutable pointers even from
70+
/// immutable references, the Connection interface enforces that all mutating
71+
/// methods correctly require &mut self.
72+
///
73+
/// Developers and reviewers MUST ensure that new methods correctly use
74+
/// either &self or &mut self depending on their behavior. No mechanism enforces this.
75+
///
76+
/// Note: Although non-mutating methods like getters should be thread-safe by definition,
77+
/// technically the only thread safety guarantee provided by the underlying C library
78+
/// is that s2n_send and s2n_recv can be called concurrently.
79+
///
80+
unsafe impl Sync for Connection {}
81+
6782
impl Connection {
6883
pub fn new(mode: Mode) -> Self {
6984
crate::init::init();
@@ -1017,4 +1032,11 @@ mod tests {
10171032
fn assert_send<T: 'static + Send>() {}
10181033
assert_send::<Context>();
10191034
}
1035+
1036+
// ensure the connection context is sync
1037+
#[test]
1038+
fn context_sync_test() {
1039+
fn assert_sync<T: 'static + Sync>() {}
1040+
assert_sync::<Context>();
1041+
}
10201042
}

0 commit comments

Comments
 (0)