New submission from Yury Selivanov <[email protected]>:
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
----------
components: IO, asyncio
messages: 306298
nosy: asvetlov, gvanrossum, pitrou, yselivanov
priority: normal
severity: normal
status: open
title: Add API to intercept socket.close()
versions: Python 3.7
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue32038>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com