Fix LibevConnection close() race causing EBADF errors#700
Draft
dkropachev wants to merge 1 commit intomasterfrom
Draft
Fix LibevConnection close() race causing EBADF errors#700dkropachev wants to merge 1 commit intomasterfrom
dkropachev wants to merge 1 commit intomasterfrom
Conversation
LibevConnection.close() closes the socket immediately while watchers are stopped asynchronously in the next event loop iteration via _loop_will_run(). This creates a race window where handle_read() or handle_write() can operate on a closed socket fd, producing EBADF errors that surface as ConnectionShutdown. - Add is_closed/is_defunct guards in handle_read() and handle_write() error paths to silently exit during shutdown instead of calling defunct() with EBADF - Set last_error in close() when connected_event is not yet set to prevent factory() from returning a dead connection - Set last_error on server-initiated close (EOF) in handle_read() before calling close()
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.
Summary
Fixes the close() race condition in LibevConnection that causes
[Errno 9] Bad file descriptorerrors during node restarts.close()closes the socket immediately but watchers are stopped asynchronously in_loop_will_run()on the next event loop iterationhandle_read()/handle_write()is already running when the socket closes, they get EBADF and calldefunct()unnecessarilyfactory()can return a dead connection whenclose()is called during handshake without settinglast_errorChanges
is_closed/is_defunctguards inhandle_read()andhandle_write()error pathslast_errorinclose()whenconnected_eventis not yet setlast_erroron server-initiated close (EOF) inhandle_read()Test plan
tests/unit/io/test_libevreactor.pypassesRefs #614