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 — Observed memory leak in ssl library: Python 3.14 GC issue · Issue #142516 · python/cpython

Observed memory leak in ssl library: Python 3.14 GC issue #142516

New issue
Copy link
New issue
Copy link
Closed
Closed
Observed memory leak in ssl library: Python 3.14 GC issue#142516
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-SSLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@HenriBlacksmith
HenriBlacksmith
opened on Dec 10, 2025
Issue body actions

Bug report

Bug description:

I already opened a bug on urllib3 for this issue, it looks that may application using MSAL for Python library (which relies on requests , which relies on urllib3, which relies on ssl) is leaking memory when being upgraded from Python 3.13 to Python 3.14 (I tested 3.14.{0, 1, 2})

The details are explained in the urllib3 ticket including outputs from memray and it points to ssl package (precisely ssl.SSLContext.load_verify_locations), see: urllib3/urllib3#3738

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs

  • gh-142516: Memory leak in SSLContext #143585
  • gh-142516: fix reference leaks in ssl.SSLContext objects #143685
  • [3.14] gh-142516: fix reference leaks in ssl.SSLContext objects (GH-143685) #145075
  • [3.13] gh-142516: fix reference leaks in ssl.SSLContext objects (GH-143685) (GH-145075) #148371
  • gh-142516: Forward-port generational GC #148687
  • [3.14] Forward-port generational GC (GH-142516) #148694
Reacted by Sebastian, HenriBlacksmith, Simon Gurcke, Bartlomiej Wiater, curekoshimizu, Sergei K., Masaya, Rikuto, Yuki Furukawa, Reona Sato and 8 more
Reacted by Alex Poone, Jalin Wang and will-wc-chen-appier

Activity

Next
  1. HenriBlacksmith
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Dec 10, 2025
  2. StanFromIreland
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    topic-SSL
    on Dec 10, 2025
  3. vstinner commented on Dec 10, 2025

    @vstinner
    vstinner
    on Dec 10, 2025
    Member
    More actions

    The details are explained in the urllib3 ticket including outputs from memray and it points to ssl package (precisely ssl.SSLContext.load_verify_locations), see: urllib3/urllib3#3738

    Please provide a reproducer, if possible using only the ssl module.

  4. zweizeichen commented on Dec 11, 2025

    @zweizeichen
    zweizeichen
    on Dec 11, 2025
    More actions

    No reproducer but if it helps: very similar situation here when connecting to valkey with a custom CA cert.

    Image
    Reacted by Ivan Rylov
  5. zweizeichen commented on Dec 12, 2025

    @zweizeichen
    zweizeichen
    on Dec 12, 2025
    More actions

    I did some more testing to come up with a minimal repro: seems like rss just keeps growing while the heap stays somewhat constant. I was unable to reproduce it using a simple sync case locally and on the cluster and this started to smell an awful lot like #109534 which might be async-related. Like @HenriBlacksmith's case we're also seeing this in an ASGI app on k8s using the debian trixie base image since the 3.14 release.

    I'll attempt to redeploy this glibc-less and report back.

    Update: the alleged leak is still there even in an alpine container

    Reacted by Faolain
  6. zweizeichen commented on Dec 15, 2025

    @zweizeichen
    zweizeichen
    on Dec 15, 2025
    More actions

    Potentially related: #84904

  7. HenriBlacksmith commented on Dec 15, 2025

    @HenriBlacksmith
    HenriBlacksmith
    on Dec 15, 2025
    Author
    More actions

    I applied a fix that improved the memory situation on my other FastAPI app by reducing the calls generated by MSAL.

    I applied a few dependency upgrades on top including a bump of the Python image from 3.14.0-slim to 3.14.2-slim and it caused the memory leak to be worse.

    Though I also need to collect detailed figures about this one as urllib3 version is different in this app because of some version constraints on a library.

    I will try to get more info from memray on this one.

    Reacted by Alex Poone
  8. HenriBlacksmith commented on Dec 16, 2025

    @HenriBlacksmith
    HenriBlacksmith
    on Dec 16, 2025
    Author
    More actions

    I was able to export new memray profiles from my other application.

    This time the leak is coming from httpx which also relies on ssl package under the hood:

    Image Image Image Image

    Sorry for the cuts on the capture, I am trying to "anonymize" things

  9. zweizeichen commented on Dec 16, 2025

    @zweizeichen
    zweizeichen
    on Dec 16, 2025
    More actions

    Thank you! I'm not 100% sure how to interpret these memray outputs to be honest, because if I understand this correctly this shows us where allocations do happen but not necessarily where the leak is (it may be correlated though). This is useful for finding out 'which objects eat up my memory?'. Technically speaking memray will only show us the allocations/leaks the python allocator knows about, but the leak kind of escapes the realm of the allocator by definition. Another layer which might be adding to this is that this might actually be fragmentation instead of leaks. I hope I ruled this out more or less by checking it's not glibc-related by also observing this behaviour on an alpine container, feel free to double-check.

    I'm going to try to find some time to give the minimal (hopefully stdlib only) repro another go and explore analyzing a full heap dump.

  10. HenriBlacksmith commented on Dec 16, 2025

    @HenriBlacksmith
    HenriBlacksmith
    on Dec 16, 2025
    Author
    More actions

    I am trying to use valgrind instead of memray to get more details (but I am a bit out of my comfort zone to be honest), it does not report much until k8s restarts the container.

    The command looks like:

    valgrind --tool=memcheck --log-file=/tmp/valgrind.log --trace-children=yes --leak-check=yes --verbose
                  python -m uvicorn app.main:shell --host 0.0.0.0 --log-config logging.yaml

    But nothing really comes out of it

    Reacted by Sebastian
  11. HenriBlacksmith commented on Dec 16, 2025

    @HenriBlacksmith
    HenriBlacksmith
    on Dec 16, 2025
    Author
    More actions

    Another layer which might be adding to this is that this might actually be fragmentation instead of leaks. I hope I ruled this out more or less by checking it's not glibc-related by also observing this behaviour on an alpine container, feel free to double-check.

    I just built an Alpine container and ran it and the behavior is similar (based on python:3.14.2-alpine3.23)

    I am running out of ideas, if anyone has guidance to help with investigating this, I would be really happy to test those, if I have extra time, I will try to build a MRO but I am not sure how it can be done

  12. HenriBlacksmith commented on Dec 16, 2025

    @HenriBlacksmith
    HenriBlacksmith
    on Dec 16, 2025
    Author
    More actions

    Good news, I might have a MRE, it is using httpx but that's the best I was able to do, I will share the whole code in a repo, including commands to export the graphs.

    Here is the code:

    import httpx
    import asyncio
    
    async def _main() -> None:
        while(1):
            async with httpx.AsyncClient() as client:
                await client.request("GET", "https://example.com/")
            await asyncio.sleep(1)
    
    if __name__ == "__main__":
        asyncio.run(_main())
    FROM astral/uv:0.9.17-python3.14-bookworm AS builder
    ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy UV_PYTHON_DOWNLOADS=0
    
    # Create and set the working directory
    WORKDIR /mro-app
    
    # Install runtime dependencies
    RUN --mount=type=cache,target=/root/.cache/uv \
        --mount=type=bind,source=uv.lock,target=uv.lock \
        --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
        uv sync --frozen --no-install-project --no-dev
    
    # Copy the application code (excluding files in .dockerignore)
    COPY . .
    RUN --mount=type=cache,target=/root/.cache/uv \
            uv sync --frozen --no-dev
    
    # Use an official lightweight Python image with slim variant
    FROM python:3.14.2-slim
    
    # Set environment variables
    # See: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONUNBUFFERED
    ENV PYTHONUNBUFFERED=1
    
    # Create and set the working directory
    WORKDIR /mro-app
    
    # Expose the port the app runs on
    EXPOSE 8000
    
    # Copy the application from the builder
    COPY --from=builder /mro-app /mro-app
    
    # Place executables in the environment at the front of the path
    ENV PATH="/mro-app/.venv/bin:$PATH"
    
    # Define the default command to run when the container starts
    CMD ["memray", "run", "--native", "--output", "/tmp/capture.bin", "main.py"]
    [project]
    name = "mro"
    version = "0.1.0"
    description = "Add your description here"
    readme = "README.md"
    requires-python = ">=3.14"
    dependencies = [
        "httpx>=0.28.1",
        "memray>=1.19.1",
        "urllib3>=2.6.2",
    ]

    etc. will build a repo asap with all the other files

    Image Image
    Reacted by Sebastian and Max Chernoff
  13. HenriBlacksmith commented on Dec 16, 2025

    @HenriBlacksmith
    HenriBlacksmith
    on Dec 16, 2025
    Author
    More actions

    Here is the repo, I hope it is an actual valid MRE and not just "normal" usage: https://github.com/HenriBlacksmith/python-ssl-memory-usage-mro/tree/main

    Reacted by Sebastian, Faolain and Max Chernoff
  14. HenriBlacksmith commented on Dec 16, 2025

    @HenriBlacksmith
    HenriBlacksmith
    on Dec 16, 2025
    Author
    More actions

    And I was finally able to get the native symbols:

    Image

    I will try to compare with older Python versions to see if the sensitivity to Python version holds with the MRE.

    I was able to reproduce on my Mac in a non-Docker environment.

    Reacted by Sebastian
  15. 47 remaining items

  16. picnixz
    added 2 commits that reference this issue on Apr 11, 2026

    [3.13] pythongh-142516: fix reference leaks in `ssl.SSLContext` objec…

    5aa7ad2

    [3.14] pythongh-142516: fix reference leaks in `ssl.SSLContext` objec…

    c690e8c
  17. bedevere-app commented on Apr 17, 2026

    @bedevere-app
    bedevere-appBot
    on Apr 17, 2026
    More actions

    GH-148694 is a backport of this pull request to the 3.14 branch.

  18. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-142516: fix reference leaks in `ssl.SSLContext` objects (pyt…

    bbaf112
  19. vstinner commented on Apr 30, 2026

    @vstinner
    vstinner
    on Apr 30, 2026
    Member
    More actions

    It was decided to Reverting the incremental GC in Python 3.14 and 3.15: see PR gh-148720 for the 3.14 branch. So can we close this issue?

    Reacted by Neil Schemenauer, HenriBlacksmith, Sergey Miryanov, curekoshimizu, Jalin Wang and Yuki Furukawa
    Reacted by Alex Poone
  20. hugovk commented on May 5, 2026

    @hugovk
    hugovk
    on May 5, 2026
    Member
    More actions

    This has been reverted and released in Python 3.14.5 release candidate, please test.

    https://discuss.python.org/t/python-3-14-5-release-candidate/107185/1

    We plan a 3.14.5 final for Friday.

    Reacted by Alex Poone, Gastón Avila, Nathan Burns, Haowei Wang, Jalin Wang, Yuki Furukawa and Vladyslav Burylov
  21. HenriBlacksmith commented on May 19, 2026

    @HenriBlacksmith
    HenriBlacksmith
    on May 19, 2026
    Author
    More actions

    This has been reverted and released in Python 3.14.5 release candidate, please test.

    https://discuss.python.org/t/python-3-14-5-release-candidate/107185/1

    We plan a 3.14.5 final for Friday.

    I have been testing this on two APIs in our dev environment and it looks memory is stable. Will provide more feedback once it will be in prod.

    Reacted by Hugo van Kemenade, Victor Stinner, Sergey Miryanov and Alessio Fachechi
  22. picnixz
    added a commit that references this issue on May 24, 2026

    [3.13] gh-142516: fix reference leaks in `ssl.SSLContext` objects (GH…

    e102378
  23. RonnyPfannschmidt
    added 4 commits that reference this issue on May 28, 2026

    fix: guard FDCapture.snap() against prematurely closed tmpfile (pytes…

    53167e5

    fix: guard FDCapture.snap() against prematurely closed tmpfile (pytes…

    5fbde2c

    fix: guard FDCapture.snap() against prematurely closed tmpfile (pytes…

    9e43244

    fix: guard FDCapture.snap() against prematurely closed tmpfile (pytes…

    594e4f5
  24. HenriBlacksmith commented on Jun 2, 2026

    @HenriBlacksmith
    HenriBlacksmith
    on Jun 2, 2026
    Author
    More actions

    This has been reverted and released in Python 3.14.5 release candidate, please test.
    https://discuss.python.org/t/python-3-14-5-release-candidate/107185/1
    We plan a 3.14.5 final for Friday.

    I have been testing this on two APIs in our dev environment and it looks memory is stable. Will provide more feedback once it will be in prod.

    It has been running in prod for a week with similar memory profiles as for 3.13.x.

    The issue is resolved from my point of view

    Reacted by Hugo van Kemenade, Sergey Miryanov, Cristian M, Gregory P. Smith and 93578237
  25. miketheman commented on Jun 2, 2026

    @miketheman
    miketheman
    on Jun 2, 2026
    Member
    More actions

    I can also confirm that the memory footprint is now "stable" since 3.14.5 - and a side benefit has been that the "floor" is lower from 3.13.x

    Image
    Reacted by Hugo van Kemenade, Sergey Miryanov, Cody Maloney, HenriBlacksmith, Cristian M, Gregory P. Smith, Felix Fanghaenel, 93578237, Victor Stinner and Kuthair Habboush
  26. HenriBlacksmith
    closed this as completedon Jun 3, 2026
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

    extension-modulesC modules in the Modules dirC modules in the Modules dirtopic-SSLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — 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.