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 — Memory leak in `atexit.register()`: missing `Py_DECREF` for `func_args` tuple · Issue #144986 · python/cpython

Memory leak in atexit.register(): missing Py_DECREF for func_args tuple #144986

New issue
Copy link
New issue
Copy link
Closed
Closed
Memory leak in atexit.register(): missing Py_DECREF for func_args tuple#144986
Copy link
Labels
3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@ashm-dev
ashm-dev
opened on Feb 19, 2026
Issue body actions

Bug description

atexit_register() in Modules/atexitmodule.c leaks memory on every call. PyTuple_GetSlice() returns a new reference, but the result is never decref'd after being packed into the callback tuple via PyTuple_Pack().

Additionally, there is no NULL check after PyTuple_GetSlice(), which could lead to undefined behavior if the allocation fails.

// Modules/atexitmodule.c, atexit_register()
PyObject *func_args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
// BUG 1: missing NULL check
PyObject *func_kwargs = kwargs;
...
PyObject *callback = PyTuple_Pack(3, func, func_args, func_kwargs);
// BUG 2: missing Py_DECREF(func_args) — leaks the tuple

The leak is detected by AddressSanitizer/LeakSanitizer when running test_atexit on an ASAN build, causing 3 test failures: test_shutdown, test_atexit_instances, and test_general (subprocess exits with code 1 due to LSAN reporting 240 bytes leaked in 4 allocations).

Fix:

PyObject *func_args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
if (func_args == NULL) {
    return NULL;
}
...
PyObject *callback = PyTuple_Pack(3, func, func_args, func_kwargs);
Py_DECREF(func_args);

CPython versions tested on

CPython main branch

Operating systems tested on

Linux

Linked PRs

  • gh-144986: Fix memory leak in atexit.register() #144987
  • [3.14] gh-144986: Fix memory leak in atexit.register() (GH-144987) #145020

Activity

  1. ashm-dev
    added 3 commits that reference this issue on Feb 19, 2026

    pythongh-144986: Fix memory leak in atexit.register()

    2191c56

    Add NEWS entry for pythongh-144986

    7e3dd83

    Merge branch 'main' into pythongh-144986-fix-atexit-memleak

    1f1e8b1
  2. sergey-miryanov
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    3.14bugs and security fixesbugs and security fixes
    3.15bugs and security fixesbugs and security fixes
    on Feb 19, 2026
  3. StanFromIreland
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Feb 19, 2026
  4. ashm-dev
    added 3 commits that reference this issue on Feb 19, 2026

    Add NEWS entry for pythongh-144986

    28e4533

    Merge branch 'main' into pythongh-144986-fix-atexit-memleak

    aad8a59

    Merge branch 'main' into pythongh-144986-fix-atexit-memleak

    c3152f0
  5. pablogsal
    added a commit that references this issue on Feb 19, 2026

    gh-144986: Fix memory leak in atexit.register() (#144987)

    50c1471
  6. miss-islington
    added a commit that references this issue on Feb 19, 2026

    pythongh-144986: Fix memory leak in atexit.register() (pythonGH-144987)

    2065be9
  7. brijkapadia
    added a commit that references this issue on Feb 28, 2026

    pythongh-144986: Fix memory leak in atexit.register() (python#144987)

    8e04b79
  8. kumaraditya303
    added a commit that references this issue on Mar 16, 2026

    [3.14] gh-144986: Fix memory leak in atexit.register() (GH-144987) (#…

    a1f2fef
  9. kumaraditya303
    closed this as completedon Mar 16, 2026
  10. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-144986: Fix memory leak in atexit.register() (python#144987)

    9dbbcf8
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.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesextension-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.