fix(client): close the socket when the browser dies before the close handshake - #639
Merged
Merged
Conversation
…handshake `WebSocket#close` only sends a close frame; the socket is closed by the driver's `on_close`, which runs when the browser answers with a close frame of its own. A browser that is already dead never answers, so `#quit` and `#restart` left the socket open until GC finalized it -- one leaked descriptor per call, enough to raise `Errno::EMFILE` from `Process.spawn` when browsers are restarted in a loop under a low `nofile` limit. Close the socket explicitly at the end of `Client#close`, after the existing grace period for the dispatch thread, so a healthy quit still completes the handshake through `on_close` and the forced close is a no-op there.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WebSocket#closeonly sends a close frame. The socket itself is closed byon_close, which runs when the browser answers with a close frame of its own — and a browser that is already dead never answers. So#quitand#restartleft the socket open until GC finalized theTCPSocket: one leaked descriptor per call, silently, with nothing raised.That is enough to raise
Errno::EMFILEfromProcess.spawnwhen browsers are restarted in a loop under a lownofilelimit, which is what #637 hit.Fix is to close the socket explicitly at the end of
Client#close, after the grace period the dispatch thread already gets. A healthy quit still completes the handshake throughon_closeinside that window, so the forced close is a no-op there; it only does work when the peer is gone.Before
After
The new spec kills the browser's process group, quits, and asserts the socket is closed. It fails on
mainwithexpected #<TCPSocket:fd 13, AF_INET, 127.0.0.1, 60322>.closed? to be truthy, got false.Refs #637