Skip to content

Commit

Permalink
✅ Add a Mutex to FakeServer (for tests only)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nevans committed Sep 13, 2024
1 parent 004305e commit 05d6b43
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions test/net/imap/fake_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 05d6b43

Please sign in to comment.