From 05d6b4386c206d56b7f22adcd5bc40e44d03e90e Mon Sep 17 00:00:00 2001 From: nick evans Date: Fri, 13 Sep 2024 08:57:12 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20a=20Mutex=20to=20FakeServer?= =?UTF-8?q?=20(for=20tests=20only)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While trying to track down the cause of #287 for @hsbt, I noticed that this is likely a race condition. While this can't be the cause of that issue (the failures come from tests that don't even use FakeServer), it seems like a good idea to fix anyway, and safe. --- test/net/imap/fake_server.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/net/imap/fake_server.rb b/test/net/imap/fake_server.rb index ea561ff7..5da6acab 100644 --- a/test/net/imap/fake_server.rb +++ b/test/net/imap/fake_server.rb @@ -61,6 +61,7 @@ def initialize(...) @config = Configuration.new(...) @tcp_server = TCPServer.new(config.hostname, config.port) @connection = nil + @mutex = Thread::Mutex.new end def host; tcp_server.addr[2] end @@ -84,9 +85,11 @@ def run # accepted and closed. This may change in the future. Call #shutdown # explicitly to ensure the server socket is unbound. def shutdown - connection&.close - commands&.close if connection&.commands&.closed?&.! - tcp_server.close + @mutex.synchronize do + connection&.close + commands&.close if connection&.commands&.closed?&.! + tcp_server.close + end end # A Queue that contains every command the server has received.