Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamWriter.wait_closed() can hang indefinitely. #83939

Closed
tomchristie mannequin opened this issue Feb 26, 2020 · 6 comments
Closed

StreamWriter.wait_closed() can hang indefinitely. #83939

tomchristie mannequin opened this issue Feb 26, 2020 · 6 comments
Labels
3.11 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@tomchristie
Copy link
Mannequin

tomchristie mannequin commented Feb 26, 2020

BPO 39758
Nosy @warsaw, @bitdancer, @asvetlov, @tomchristie, @1st1, @JulienPalard, @lysnikolaou, @aeros, @r-owen, @kumaraditya303

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2020-02-26.10:36:06.728>
labels = ['3.11', 'type-bug', 'expert-asyncio']
title = 'StreamWriter.wait_closed() can hang indefinitely.'
updated_at = <Date 2022-03-03.11:14:26.540>
user = 'https://github.com/tomchristie'

bugs.python.org fields:

activity = <Date 2022-03-03.11:14:26.540>
actor = 'kumaraditya'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['asyncio']
creation = <Date 2020-02-26.10:36:06.728>
creator = 'tomchristie'
dependencies = []
files = []
hgrepos = []
issue_num = 39758
keywords = []
message_count = 6.0
messages = ['362688', '362738', '387644', '387645', '400863', '414423']
nosy_count = 12.0
nosy_names = ['barry', 'r.david.murray', 'asvetlov', 'tomchristie', 'yselivanov', 'mdk', 'lys.nikolaou', 'aeros', 'Fran\xc3\xa7ois Voron', 'r3owen', 'kumaraditya', 'seer']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue39758'
versions = ['Python 3.11']

@tomchristie
Copy link
Mannequin Author

tomchristie mannequin commented Feb 26, 2020

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 encode/httpx#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())

@tomchristie tomchristie mannequin added type-bug An unexpected behavior, bug, or error 3.7 (EOL) end of life 3.8 only security fixes topic-asyncio labels Feb 26, 2020
@FranoisVoron FranoisVoron mannequin changed the title StreamWriter.wait_closed() can hang indefinately. StreamWriter.wait_closed() can hang indefinitely. Feb 26, 2020
@JulienPalard
Copy link
Member

Can reproduce in:

  • 3.8.0
  • 3.8.2
  • 3.7.6

@r-owen
Copy link
Mannequin

r-owen mannequin commented Feb 25, 2021

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.

@r-owen
Copy link
Mannequin

r-owen mannequin commented Feb 25, 2021

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.

@seer
Copy link
Mannequin

seer mannequin commented Sep 1, 2021

See this issue on 3.9.6
No SSL, but plain sockets.
This seems to appear when writer.write/writer.drain was cancelled, and writer.close/writer.wait_closed called after this.

@seer seer mannequin added the 3.9 only security fixes label Sep 1, 2021
@beblostanislav beblostanislav mannequin added build The build process and cross-build tests Tests in the Lib/test dir topic-email topic-C-API interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.10 only security fixes 3.11 only security fixes performance Performance or resource usage and removed 3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error labels Sep 1, 2021
@pablogsal pablogsal removed build The build process and cross-build tests Tests in the Lib/test dir topic-email topic-C-API interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Sep 1, 2021
@kumaraditya303
Copy link
Contributor

On main branch with rewritten SSL implementation #75458 bpo-44011, this raises TimeoutError:

--------------------------------------------------------------------------------
opened
close started

Traceback (most recent call last):
  File "/workspaces/cpython/main.py", line 18, in <module>
    asyncio.run(main())
    ^^^^^^^^^^^^^^^^^^^
  File "/workspaces/cpython/Lib/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspaces/cpython/Lib/asyncio/base_events.py", line 645, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/workspaces/cpython/main.py", line 14, in main
    await writer.wait_closed()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspaces/cpython/Lib/asyncio/streams.py", line 344, in wait_closed
    await self._protocol._get_close_waiter(self)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: SSL shutdown timed out

This issue has been fixed, it can be closed @asvetlov.

@kumaraditya303 kumaraditya303 removed 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes labels Mar 3, 2022
@kumaraditya303 kumaraditya303 added type-bug An unexpected behavior, bug, or error and removed performance Performance or resource usage labels Mar 3, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants