Skip to content

SelectorEventLoop sock_connect can return before cleaning up if sock.connect encounters BlockingIOError or InterruptedError on cancellation #131728

@bdraco

Description

@bdraco

Bug report

Bug description:

Originally reported in aio-libs/aiohttp#10617 (comment)

To make this happen:

Call loop.sock_connect and cancel it before the socket is connected.

Because _sock_write_done is called via add_done_callback it gets called in the next iteration of the event loop on cancellation.

fut.add_done_callback(

sock_connect will return before _sock_write_done is called which means the writer has not been removed yet and can get reused.

Additionally, this makes the close in

sock.close()
unsafe because on cancellation the socket gets closed before the writer is removed so the fd can get reused

CPython versions tested on:

3.10 but the same code exists for 3.13+

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions