◐ Shell
clean mode source ↗

Message 393584 - Python tracker

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.