Skip to content

🧵 Close socket in #disconnect before waiting for lock & thread join #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 19, 2025

Conversation

nevans
Copy link
Collaborator

@nevans nevans commented Jun 19, 2025

While attempting to get the tests running under JRuby, #disconnect would sometimes deadlock. So, although I haven't seen that problem elsewhere and JRuby is still failing tests with another threading issue, this (and #494) did get most tests passing for me (but not in CI).

This updates #disconnect so that the socket is shutdown and closed before joining the receiver thread and before waiting for the lock to change connection_state (It will still attempt to change connection_state before closing the socket, but it won't wait if the lock isn't available).

nevans added 6 commits June 19, 2025 11:58
If `disconnected?` returns true, the connection state is most likely
already `logout`.  But, just in case, we'll ensure it's set every time.
Net::IMAP#disconnect will still attempt to enter the logout state first,
but it won't wait for the lock until after the socket has been shutdown.
The reciever thread should close the connection before it finishes, and
`@sock.shutdown` should've been enough to trigger that.  But this
ensures the socket is closed right away, without needing to wait on
whatever the receiver thread might be in the middle of doing.
It should always be safe to call `@sock.close` without external
synchronization.  And synchronizing here with could get stuck
waiting e.g. for a response_handler looping in the receiver thread.
This should work both for TCPSocket and for OpenSSL::SSL::SSLSocket.
Both will respond to `#to_io` with the TCPSocket.
@nevans nevans force-pushed the synchronize-disconnect-logout branch from b17de74 to 0c919fb Compare June 19, 2025 15:59
@nevans nevans changed the title 🧵 Update #disconnect to close the socket before waiting on locks or receiver thread 🧵 Close socket in #disconnect before waiting for lock & thread join Jun 19, 2025
@nevans nevans merged commit b6e8e5a into master Jun 19, 2025
37 checks passed
@nevans nevans deleted the synchronize-disconnect-logout branch June 19, 2025 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant