Message306298
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 |
|
Date |
User |
Action |
Args |
2017-11-15 19:00:27 | yselivanov | set | recipients:
+ yselivanov, gvanrossum, pitrou, asvetlov |
2017-11-15 19:00:27 | yselivanov | set | messageid: <1510772427.07.0.213398074469.issue32038@psf.upfronthosting.co.za> |
2017-11-15 19:00:27 | yselivanov | link | issue32038 messages |
2017-11-15 19:00:26 | yselivanov | create | |
|