GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — asyncio: potential leak of TLS connections · Issue #106684 · python/cpython

asyncio: potential leak of TLS connections #106684

New issue
Copy link
New issue
Copy link
Closed
Closed
asyncio: potential leak of TLS connections#106684
Copy link
Labels
3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagetopic-asynciotype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@romuald
romuald
opened on Jul 12, 2023
Issue body actions

Bug report

Synopsis

Forgetting to close TLS connections manually with asyncio.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 with asyncio.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
  • do not call writer.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

import os
import asyncio
import gc
import signal

HOST = "google.fr"  # will keep the connection alive for a few minutes at least


async def query():
    reader, writer = await asyncio.open_connection(HOST, 443, ssl=True)

    # No connection: close, remote side will keep the connection open
    writer.write(f"GET / HTTP/1.1\r\nHost: {HOST}\r\n\r\n".encode())
    await writer.drain()

    # only read the first header line
    try:
        return (await reader.readline()).decode()
    finally:
        # closing the writer will properly finalize the connection
        # writer.close()
        pass

    # reader and writer are now unreachable


async def amain():
    await query()

    # The _SSLProtocolTransport object is kept in memory and the
    # connection won't be released until the remote side closes the connection
    for _ in range(200):
        # Just be sure everything is freed, just in case
        gc.collect()

        await asyncio.sleep(1)


def main():
    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

  • GH-106684: raise ResourceWarning when StreamWriter is not closed #107650
  • [3.12] GH-106684: Close asyncio.StreamWriter when asyncio.StreamWriter is not closed by application (GH-107650) #107656
  • [3.11] GH-106684: raise ResourceWarning when asyncio.StreamWriter is not closed (GH-107650) #107657
  • [3.11] [3.12] GH-106684: Close asyncio.StreamWriter when asyncio.StreamWriter is not closed by application (GH-107650) (GH-107656) #107836

Activity

  1. romuald
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jul 12, 2023
  2. sobolevn
    added
    topic-asyncio
    on Jul 12, 2023
  3. github-project-automation
    added this to asyncioon Jul 12, 2023
  4. github-project-automation
    moved this to Todo in asyncioon Jul 12, 2023
  5. kumaraditya303
    added
    performancePerformance or resource usagePerformance or resource usage
    3.11only security fixesonly security fixes
    3.12only security fixesonly security fixes
    3.13only security fixesonly security fixes
    on Aug 5, 2023
  6. kumaraditya303
    added a commit that references this issue on Aug 5, 2023

    GH-106684: raise `ResourceWarning` when `asyncio.StreamWriter` is not…

    41178e4
  7. miss-islington
    added 2 commits that reference this issue on Aug 5, 2023

    pythonGH-106684: raise `ResourceWarning` when `asyncio.StreamWriter` …

    0004686

    pythonGH-106684: raise `ResourceWarning` when `asyncio.StreamWriter` …

    385390a
  8. evildmp
    added a commit that references this issue on Aug 5, 2023

    pythonGH-106684: raise `ResourceWarning` when `asyncio.StreamWriter` …

    d999392
  9. Yhg1s
    added a commit that references this issue on Aug 10, 2023

    [3.12] GH-106684: Close `asyncio.StreamWriter` when `asyncio.StreamWr…

    7853c76
  10. miss-islington
    added a commit that references this issue on Aug 10, 2023

    [3.12] pythonGH-106684: Close `asyncio.StreamWriter` when `asyncio.St…

    91ce3ac
  11. kumaraditya303
    added a commit that references this issue on Aug 10, 2023

    [3.11] [3.12] GH-106684: Close `asyncio.StreamWriter` when `asyncio.S…

    fb08b79
  12. kumaraditya303
    closed this as completedon Aug 10, 2023
  13. github-project-automation
    moved this from Todo to Done in asyncioon Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagetopic-asynciotype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • asyncio
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.