Skip to content

Commit 4d0ce49

Browse files
committed
✅ Add mutex to FakeServer::Connection#close
I believe this was getting triggered from multiple threads: * from `FakeServer#shutdown` (which already has a mutex of its own) * called in ensure clause in `FakeServer#run` (from the server thread) * called directly by the tests (from the main thread) * from the ensure clause in `Connection#run` (runs in the server thread) So the main thread could call `FakeServer#shutdown` while the server thread is handling the ensure clause in `Connection#run`. Adding a mutex to `Connection#close` should fix the flaky tests.
1 parent bd0d1b2 commit 4d0ce49

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

test/net/imap/fake_server/connection.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def initialize(server, tcp_socket:)
1515
@reader = CommandReader.new socket
1616
@writer = ResponseWriter.new socket, config: config, state: state
1717
@router = CommandRouter.new writer, config: config, state: state
18+
@mutex = Thread::Mutex.new
1819
end
1920

2021
def commands; state.commands end
@@ -31,11 +32,13 @@ def run
3132
end
3233

3334
def close
34-
unless state.logout?
35-
state.logout
36-
writer.bye
35+
@mutex.synchronize do
36+
unless state.logout?
37+
state.logout
38+
writer.bye
39+
end
40+
socket&.close unless socket&.closed?
3741
end
38-
socket&.close unless socket&.closed?
3942
end
4043

4144
private

0 commit comments

Comments
 (0)