From 4e292434c65b4e4a23a502b35c8101a4c3cb5c20 Mon Sep 17 00:00:00 2001 From: Amirsaman Memaripour Date: Thu, 13 Jan 2022 19:32:05 +0000 Subject: SERVER-62571 Serialize concurrent accesses to `Socket` in `ThrowOnNetworkErrorInEnsureSync` (cherry picked from commit 1b71c67c9290af3db02d75471eeba894862e1a31) --- src/mongo/transport/transport_layer_asio_test.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/mongo/transport/transport_layer_asio_test.cpp b/src/mongo/transport/transport_layer_asio_test.cpp index d08499e66f9..aec7bb9c934 100644 --- a/src/mongo/transport/transport_layer_asio_test.cpp +++ b/src/mongo/transport/transport_layer_asio_test.cpp @@ -366,7 +366,10 @@ TEST(TransportLayerASIO, ThrowOnNetworkErrorInEnsureSync) { Notification mockSessionCreated; tf.sep().setOnStartSession([&](SessionThread& st) { mockSessionCreated.set(&st); }); + unittest::Barrier barrier(2); ConnectionThread connectThread(tf.tla().listenerPort(), [&](ConnectionThread& conn) { + ON_BLOCK_EXIT([&] { barrier.countDownAndWait(); }); + // Linger timeout = 0 causes a RST packet on close. struct linger sl = {1, 0}; if (setsockopt(conn.socket().rawFD(), @@ -379,12 +382,15 @@ TEST(TransportLayerASIO, ThrowOnNetworkErrorInEnsureSync) { } }); - auto& st = *mockSessionCreated.get(); - connectThread.close(); - // We set the timeout to ensure that the setsockopt calls are actually made in ensureSync() + auto& st = *mockSessionCreated.get(); st.session().setTimeout(Milliseconds{500}); + // Synchronize with the connection thread to ensure the connection is closed only after the + // connection thread returns from calling `setsockopt`. + barrier.countDownAndWait(); + connectThread.close(); + // On Mac, setsockopt will immediately throw a SocketException since the socket is closed. // On Linux, we will throw HostUnreachable once we try to actually read the socket. // We allow for either exception here. -- cgit v1.2.1