geventreactor: fix close() race causing EBADF errors#701
Open
dkropachev wants to merge 1 commit intomasterfrom
Open
geventreactor: fix close() race causing EBADF errors#701dkropachev wants to merge 1 commit intomasterfrom
dkropachev wants to merge 1 commit intomasterfrom
Conversation
a3a0bbd to
a2aa296
Compare
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()
a2aa296 to
7163caa
Compare
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 a race condition in
GeventConnectionwhereclose()could cause[Errno 9] Bad file descriptorerrors during node-down events.Root cause:
close()callskill(block=False)which schedulesGreenletExitfor the next yield point, then immediately closes the socket. If the greenlet is still mid-I/O when the socket is closed, the read/write handlers raise EBADF instead of shutting down gracefully.Changes:
GreenletExitinhandle_read()andhandle_write()to exit cleanly (matchingeventletreactorpattern)last_errorinclose()whenconnected_eventis not yet set, so connection attempts get a proper errorlast_erroron server-initiated close (EOF) inhandle_read()Fixes #614