[issue37658] In some cases asyncio.wait_for can lead to socket leak.
Denis S. Otkidach
report at bugs.python.org
Thu May 13 10:53:36 EDT 2021
Denis S. Otkidach <denis.otkidach at gmail.com> added the comment:
The original problem can be fixed by wrapping await into try-except block:
```
async def create_connection(ssl_obj):
loop = asyncio.get_event_loop()
connector = loop.create_connection(MyEchoClientProtocol, '127.0.0.1', 5000, ssl=ssl_obj)
connector = asyncio.ensure_future(connector)
try:
tr, pr = await connector
except asyncio.CancelledError:
if connector.done():
tr, pr = connector.result()
# Uncommenting the following line fixes the problem:
# tr.close()
raise
return tr, pr
```
I've updated my example to reproduce this: https://github.com/ods/bpo-37658/commit/eca3d81d60cbe129ce687674e6451836d567f6b9
I believe it's general problem with maintaining atomicity in async code, and not a bug in `wait_for`. Probably having an interface like `async with loop.create_connection(…) as transport, protocol` might simplify correct usage for this particular case.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37658>
_______________________________________
More information about the Python-bugs-list
mailing list