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 — socketmodule.c: Reference and buffer leaks via audit hook failures · Issue #146245 · python/cpython

socketmodule.c: Reference and buffer leaks via audit hook failures #146245

New issue
Copy link
New issue
Copy link
Closed
Closed
socketmodule.c: Reference and buffer leaks via audit hook failures#146245
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@aisk
aisk
opened on Mar 21, 2026
Issue body actions

Bug report

Bug description:

Summary

Two leak bugs in socketmodule.c triggered when PySys_Audit raises:

  1. getaddrinfo (line 6983): idna and/or pstr refs leaked when audit hook raises. ~657 objects leaked per 1000 calls.
  2. sock_sendto (line 4810): pbuf Py_buffer not released when audit hook raises.

Reproducer (getaddrinfo leak)

import socket, sys

sys.addaudithook(lambda *a: (_ for _ in ()).throw(RuntimeError("audit")))
before = sys.gettotalrefcount()
for i in range(1000):
    try:
        socket.getaddrinfo("localhost", 80)
    except RuntimeError:
        pass
after = sys.gettotalrefcount()
print(f"Leaked {after - before} objects in 1000 calls")

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

  • gh-146245: Fix ref/buffer leaks in socketmodule.c on audit hook failure #146247
  • gh-146245: Fix reference and buffer leaks via audit hook in socket module #146248
  • [3.14] gh-146245: Fix reference and buffer leaks via audit hook in socket module (GH-146248) #146274
  • [3.13] gh-146245: Fix reference and buffer leaks via audit hook in socket module (GH-146248) #146275

Activity

  1. aisk
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Mar 21, 2026
  2. aisk
    added a parent issue on Mar 21, 2026
    • Umbrella issue: bugs found using cpython-review-toolkit #146102
  3. aisk
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Mar 21, 2026
  4. kimimgo
    added a commit that references this issue on Mar 21, 2026

    pythongh-146245: Fix ref/buffer leaks in socketmodule.c on audit hook…

    25217e7
  5. aisk commented on Mar 21, 2026

    @aisk
    aisk
    on Mar 21, 2026
    MemberAuthor
    More actions

    Hi @kimimgo, I already had a working PR when I created the issue. I created the issue just to get the issue number as a comment in the test.

    How did you create your PR within 10 minutes just after the issue was created? And the news entry filename in your PR is Misc/NEWS.d/next/Library/2026-03-21-00-00-00.gh-issue-146245.SockAud.rst, the time part is just zero. In current LLMs, the KV-cache is very important for performance, so most of the LLM agents will just use zero time in system prompts, so I suspect it is generated by LLM.

    If so, please read https://devguide.python.org/getting-started/generative-ai/ before contributing.

    Reacted by Imgyu Kim
  6. kimimgo commented on Mar 21, 2026

    @kimimgo
    kimimgo
    on Mar 21, 2026
    Contributor
    More actions

    @aisk You're right — I should have checked for an existing PR first. I'll close my PR (#146247) since yours was already in progress.

    To be transparent: yes, I used an AI coding assistant (Claude Code) to help analyze and fix the issue. The speed and the 00-00-00 timestamp in the NEWS entry are artifacts of that workflow. I appreciate you pointing out the devguide AI policy — I've read it and will follow it more carefully going forward.

    That said, I do review and validate all changes before submitting — the responsibility for the code is mine. In this case though, your PR takes priority. Apologies for the duplicate, and thanks for the thorough bug report!

  7. serhiy-storchaka
    added a commit that references this issue on Mar 22, 2026

    gh-146245: Fix reference and buffer leaks via audit hook in socket mo…

    c30fae4
  8. miss-islington
    added 2 commits that reference this issue on Mar 22, 2026

    pythongh-146245: Fix reference and buffer leaks via audit hook in soc…

    6bfcd89

    pythongh-146245: Fix reference and buffer leaks via audit hook in soc…

    c64d830
  9. serhiy-storchaka
    closed this as completedon Mar 22, 2026
  10. serhiy-storchaka commented on Mar 22, 2026

    @serhiy-storchaka
    serhiy-storchaka
    on Mar 22, 2026
    Member
    More actions

    There were no an existing PR yet, but it's good manners to ask if the author of the issue wants to provide a PR. Since they were the first to find this problem, they must have been working on it and may already have a solution ready. Correctly identifying the problem and creating a reproducer is important and hard work.

  11. serhiy-storchaka commented on Mar 22, 2026

    @serhiy-storchaka
    serhiy-storchaka
    on Mar 22, 2026
    Member
    More actions

    It is safer to take issues created months or years ago. If there are no ready PRs or patches, then the original reporter perhaps not so interesting in it or does not have enough time or qualification.

    But if there is a discussion, you should read it. Perhaps the issue is still open because no good solution was found.

    Reacted by Imgyu Kim and An Long
  12. serhiy-storchaka
    added 2 commits that reference this issue on Mar 22, 2026

    [3.13] gh-146245: Fix reference and buffer leaks via audit hook in so…

    2f1e341

    [3.14] gh-146245: Fix reference and buffer leaks via audit hook in so…

    69a37be
  13. kimimgo commented on Mar 22, 2026

    @kimimgo
    kimimgo
    on Mar 22, 2026
    Contributor
    More actions

    @serhiy-storchaka Thank you for the advice — that's a good rule of thumb. I'll make sure to check for existing PRs and ask before submitting in the future, especially for freshly-filed issues.

  14. CuriousLearner
    added a commit that references this issue on Mar 23, 2026

    Merge branch 'main' of github.com:python/cpython into fix-pythongh-13…

    3e05fa3
  15. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-146245: Fix reference and buffer leaks via audit hook in soc…

    fec6f91
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 dirtype-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.