Fix GeventConnection close() race causing EBADF errors#701
Draft
dkropachev wants to merge 1 commit intomasterfrom
Draft
Fix GeventConnection close() race causing EBADF errors#701dkropachev wants to merge 1 commit intomasterfrom
dkropachev wants to merge 1 commit intomasterfrom
Conversation
GeventConnection.close() uses kill(block=False) which schedules GreenletExit for the next yield point, then closes the socket immediately. If close() is called from a non-gevent thread, the greenlet may still be mid-I/O when the socket is closed, causing EBADF. - Add is_closed/is_defunct guards in handle_read() and handle_write() error paths to silently exit during shutdown - Add GreenletExit exception handling in both I/O handlers for graceful greenlet termination (matching eventletreactor pattern) - 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()
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 GeventConnection that can cause
[Errno 9] Bad file descriptorerrors.close()useskill(block=False)which schedules GreenletExit for the next yield point, then closes the socket immediatelyhandle_read()/handle_write()don't catchGreenletExit(unlike eventletreactor)Changes
is_closed/is_defunctguards inhandle_read()andhandle_write()error pathsGreenletExitexception handling in both I/O handlers (matching eventletreactor pattern)last_errorinclose()whenconnected_eventis not yet setlast_erroron server-initiated close (EOF) inhandle_read()Test plan
EVENT_LOOP_MANAGER=gevent tests/unit/io/test_geventreactor.pypassesRefs #614