Detach socket on create_connection cancellation to prevent fd double-close#740
Open
junjzhang wants to merge 1 commit intoMagicStack:masterfrom
Open
Detach socket on create_connection cancellation to prevent fd double-close#740junjzhang wants to merge 1 commit intoMagicStack:masterfrom
junjzhang wants to merge 1 commit intoMagicStack:masterfrom
Conversation
When create_connection(sock=sock) or create_unix_connection(sock=sock) is cancelled or raises, tr._close() closes the fd via libuv but the Python socket object still believes it owns that fd number. Its __del__ later closes whatever fd the OS recycled into that slot, corrupting unrelated transports. Call sock.detach() on the error path so the Python socket sets its internal fd to -1, matching the semantics of standard asyncio where the transport always takes full ownership of the socket. Fixes MagicStack#738
Author
|
@1st1 Please have a look! thx |
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 #738.
When
create_connection(sock=sock)orcreate_unix_connection(sock=sock)is cancelled or raises,tr._close()closes the fd via libuv but the Python socket object still believes it owns that fd. Its__del__later closes whatever fd the OS recycled into that slot, silently corrupting an unrelated transport.This adds
sock.detach()on the error path of bothcreate_connectionandcreate_unix_connection, matching the semantics of standard asyncio where the transport always takes full ownership of the socket.Changes
loop.pyx: Callsock.detach()aftertr._close()in theexcept BaseExceptionblock for both TCP and Unixsock=pathstest_tcp.py: Addtest_create_connection_sock_cancel_detachesverifying thatsock.fileno() == -1after cancellation