This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author yselivanov
Recipients asvetlov, gvanrossum, pitrou, yselivanov
Date 2017-11-15.19:00:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1510772427.07.0.213398074469.issue32038@psf.upfronthosting.co.za>
In-reply-to
Content
asyncio has a few APIs that accept a Python socket object: loop.sock_recv(), loop.sock_accept() etc.

asyncio (and event loops such as uvloop) expect that it controls the socket for the duration of the call.  However, it cannot prevent the socket object from being closing *while* it is working with it:

    loop = asyncio.get_event_loop()
    sock = socket.socket()
    sock.connect(('google.com', 80))
    sock.setblocking(0)
    f = loop.sock_recv(sock, 100)
    sock.close()
    await f

The above snippet makes asyncio to forever try to read from 'sock' (resource leak).  uvloop simply segfaults (at least on Linux) because libuv assumes that sockets can't be closed while it's working with them.

Same problem exists when a user calls `loop.create_server(sock=sock)` and later manually calls `sock.close()`.

My proposal is to add a new API: `socket.register_close(callback)`.  `callback` will be called whenever a socket object is about to be closed.

This will enable asyncio and uvloop to detect when sockets they are working with are about to be closed and to either raise an error, log a debug line, or/and to stop polling the socket.

See also: https://github.com/MagicStack/uvloop/issues/100
History
Date User Action Args
2017-11-15 19:00:27yselivanovsetrecipients: + yselivanov, gvanrossum, pitrou, asvetlov
2017-11-15 19:00:27yselivanovsetmessageid: <1510772427.07.0.213398074469.issue32038@psf.upfronthosting.co.za>
2017-11-15 19:00:27yselivanovlinkissue32038 messages
2017-11-15 19:00:26yselivanovcreate