classification
Title: StreamWriter.wait_closed() can hang indefinitely.
Type: behavior Stage:
Components: asyncio Versions: Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: François Voron, asvetlov, mdk, r3owen, tomchristie, yselivanov
Priority: normal Keywords:

Created on 2020-02-26 10:36 by tomchristie, last changed 2021-02-25 00:16 by r3owen.

Messages (4)
msg362688 - (view) Author: Tom Christie (tomchristie) * Date: 2020-02-26 10:36
Raising an issue that's impacting us on `httpx`.

It appears that in some cases SSL unwrapping can cause `.wait_closed()` to hang indefinately.

Trio are particularly careful to work around this case, and have an extensive comment on it: https://github.com/python-trio/trio/blob/31e2ae866ad549f1927d45ce073d4f0ea9f12419/trio/_ssl.py#L779-L829 

Originally raised via https://github.com/encode/httpx/issues/634

Tested on:

* Python 3.7.6
* Python 3.8.1

```
import asyncio
import ssl
import certifi

hostname = 'login.microsoftonline.com'
context = ssl.create_default_context()
context.load_verify_locations(cafile=certifi.where())

async def main():
    reader, writer = await asyncio.open_connection(hostname, 443, ssl=context)
    print('opened')
    writer.close()
    print('close started')
    await writer.wait_closed()
    print('close completed')

asyncio.run(main())
```
msg362738 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2020-02-26 21:02
Can reproduce in:

- 3.8.0
- 3.8.2
- 3.7.6
msg387644 - (view) Author: Russell Owen (r3owen) * Date: 2021-02-25 00:15
I am also seeing this in Python 3.8.6. I am not using SSL, but am simply calling `await writer.wait_closed()` on an `asyncio.StreamWriter`. Sometimes it works quickly and sometimes it hangs indefinitely.
msg387645 - (view) Author: Russell Owen (r3owen) * Date: 2021-02-25 00:16
Regarding my previous comment: I have never seen this in Python 3.7 (though I see that this particular bug is listed as being present there) so it may be a different underlying issue.
History
Date User Action Args
2021-02-25 00:16:14r3owensetmessages: + msg387645
2021-02-25 00:15:11r3owensetnosy: + r3owen
messages: + msg387644
2020-02-26 21:02:09mdksetnosy: + mdk
messages: + msg362738
2020-02-26 10:53:32François Voronsetnosy: + François Voron

title: StreamWriter.wait_closed() can hang indefinately. -> StreamWriter.wait_closed() can hang indefinitely.
2020-02-26 10:36:06tomchristiecreate