Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

asyncio: potential leak of TLS connections #106684

Closed
Labels
3.11only security fixes3.12only security fixes3.13bugs and security fixesperformancePerformance or resource usagetopic-asynciotype-bugAn unexpected behavior, bug, or error
@romuald

Description

@romuald

Bug report

Synopsis

Forgetting to close TLS connections manually withasyncio.open_connection() will lead to a leak of TCP connection when the writer/reader get out of scope

Note: the reference is properly released when the remote side closes the connection

This seems to be counter intuitive relative to other python APIs where the connection is closed when the handle goes out of scope

Details

  • open a TLS connection withasyncio.open_connection(..., ssl=True)
  • do some read/writes
  • exit function, so handlers get out of scope (and possibly gc collected). This may be due to an exception for example
  • donot callwriter.close()
  • the connection is now "unreachable" from a user point of view
  • however the TCP connection is kept alive

When trying to debug this issue I found out that a_SSLProtocolTransport instance is kept in memory, probably linked to the eventloop

Example script

importosimportasyncioimportgcimportsignalHOST="google.fr"# will keep the connection alive for a few minutes at leastasyncdefquery():reader,writer=awaitasyncio.open_connection(HOST,443,ssl=True)# No connection: close, remote side will keep the connection openwriter.write(f"GET / HTTP/1.1\r\nHost:{HOST}\r\n\r\n".encode())awaitwriter.drain()# only read the first header linetry:return (awaitreader.readline()).decode()finally:# closing the writer will properly finalize the connection# writer.close()pass# reader and writer are now unreachableasyncdefamain():awaitquery()# The _SSLProtocolTransport object is kept in memory and the# connection won't be released until the remote side closes the connectionfor_inrange(200):# Just be sure everything is freed, just in casegc.collect()awaitasyncio.sleep(1)defmain():print(f"PID{os.getpid()}")task=asyncio.ensure_future(amain())loop=asyncio.get_event_loop()loop.add_signal_handler(signal.SIGTERM,task.cancel)loop.add_signal_handler(signal.SIGINT,task.cancel)loop.run_until_complete(task)if__name__=="__main__":main()

Your environment

  • CPython versions tested on:
    • 3.11.4
    • 3.10.11
  • Operating system and architecture: Debian Linux 5.19 x86_64

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13bugs and security fixesperformancePerformance or resource usagetopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp