Skip to content

Memory leak in initialization of readline during OOM #152398

Description

@brijkapadia

Bug report

Bug description:

This is a sub-issue of #146102 with the gist here.


I think that this might be a false positive in the umbrella. When I ran the reproducer, I got no segfault.

However, when I ran this modified reproducer, ASan detected a memory leak:

Modified Reproducer

import sys
import _testcapi

for n in range(500):
    sys.modules.pop("readline", None)

    try:
        _testcapi.set_nomemory(n, 0)
        import readline

    except MemoryError:
        pass

    finally:
        _testcapi.remove_mem_hooks()

ASan Report


=================================================================
==1644389==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 10370 byte(s) in 305 object(s) allocated from:
    #0 0x7ff1ebd2506e in strdup (/usr/lib/libasan.so.8+0x12506e) (BuildId: ee5fbab73143ab257a66a33afe0f038a4af7a74e)
    #1 0x7ff1eb6dbc96 in setup_readline Modules/readline.c:1407
    #2 0x7ff1eb6de59d in PyInit_readline Modules/readline.c:1689
    #3 0x5620e9ae1f76 in _PyImport_RunModInitFunc Python/importdl.c:436
    #4 0x5620e9ad934a in reload_singlephase_extension Python/import.c:1982
    #5 0x5620e9ad9b31 in import_find_extension Python/import.c:2043
    #6 0x5620e9adb595 in _imp_create_dynamic_impl Python/import.c:5468
    #7 0x5620e9adbb3c in _imp_create_dynamic Python/clinic/import.c.h:489
    #8 0x5620e9825587 in cfunction_vectorcall_FASTCALL Objects/methodobject.c:449
    #9 0x5620e97704ed in _PyVectorcall_Call Objects/call.c:273
    #10 0x5620e97709bd in _PyObject_Call Objects/call.c:348
    #11 0x5620e97709ff in PyObject_Call Objects/call.c:373
    #12 0x5620e9a09460 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:2831
    #13 0x5620e9a410c9 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:122
    #14 0x5620e9a41436 in _PyEval_Vector Python/ceval.c:2141
    #15 0x5620e976c905 in _PyFunction_Vectorcall Objects/call.c:413
    #16 0x5620e976cdca in _PyObject_VectorcallTstate Include/internal/pycore_call.h:144
    #17 0x5620e976f058 in object_vacall Objects/call.c:823
    #18 0x5620e976f333 in PyObject_CallMethodObjArgs Objects/call.c:960
    #19 0x5620e9ad5c3c in import_find_and_load Python/import.c:4133
    #20 0x5620e9ade62a in PyImport_ImportModuleLevelObject Python/import.c:4249
    #21 0x5620e99f8305 in _PyEval_ImportNameWithImport Python/ceval.c:2996
    #22 0x5620e99f858d in _PyEval_ImportName Python/ceval.c:2975
    #23 0x5620e9a1dd40 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:7128
    #24 0x5620e9a410c9 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:122
    #25 0x5620e9a41436 in _PyEval_Vector Python/ceval.c:2141
    #26 0x5620e9a416ef in PyEval_EvalCode Python/ceval.c:679
    #27 0x5620e9b4929e in run_eval_code_obj Python/pythonrun.c:1369
    #28 0x5620e9b4961c in run_mod Python/pythonrun.c:1472
    #29 0x5620e9b4a51c in pyrun_file Python/pythonrun.c:1296

Direct leak of 34 byte(s) in 1 object(s) allocated from:
    #0 0x7ff1ebd2506e in strdup (/usr/lib/libasan.so.8+0x12506e) (BuildId: ee5fbab73143ab257a66a33afe0f038a4af7a74e)
    #1 0x7ff1eb6dbc96 in setup_readline Modules/readline.c:1407
    #2 0x7ff1eb6de59d in PyInit_readline Modules/readline.c:1689
    #3 0x5620e9ae1f76 in _PyImport_RunModInitFunc Python/importdl.c:436
    #4 0x5620e9ad9e0a in import_run_extension Python/import.c:2167
    #5 0x5620e9adb9ff in _imp_create_dynamic_impl Python/import.c:5529
    #6 0x5620e9adbb3c in _imp_create_dynamic Python/clinic/import.c.h:489
    #7 0x5620e9825587 in cfunction_vectorcall_FASTCALL Objects/methodobject.c:449
    #8 0x5620e97704ed in _PyVectorcall_Call Objects/call.c:273
    #9 0x5620e97709bd in _PyObject_Call Objects/call.c:348
    #10 0x5620e97709ff in PyObject_Call Objects/call.c:373
    #11 0x5620e9a09460 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:2831
    #12 0x5620e9a410c9 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:122
    #13 0x5620e9a41436 in _PyEval_Vector Python/ceval.c:2141
    #14 0x5620e976c905 in _PyFunction_Vectorcall Objects/call.c:413
    #15 0x5620e976cdca in _PyObject_VectorcallTstate Include/internal/pycore_call.h:144
    #16 0x5620e976f058 in object_vacall Objects/call.c:823
    #17 0x5620e976f333 in PyObject_CallMethodObjArgs Objects/call.c:960
    #18 0x5620e9ad5c3c in import_find_and_load Python/import.c:4133
    #19 0x5620e9ade62a in PyImport_ImportModuleLevelObject Python/import.c:4249
    #20 0x5620e99f8305 in _PyEval_ImportNameWithImport Python/ceval.c:2996
    #21 0x5620e99f858d in _PyEval_ImportName Python/ceval.c:2975
    #22 0x5620e9a1dd40 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:7128
    #23 0x5620e9a410c9 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:122
    #24 0x5620e9a41436 in _PyEval_Vector Python/ceval.c:2141
    #25 0x5620e9a416ef in PyEval_EvalCode Python/ceval.c:679
    #26 0x5620e9b4929e in run_eval_code_obj Python/pythonrun.c:1369
    #27 0x5620e9b4961c in run_mod Python/pythonrun.c:1472
    #28 0x5620e9b4a51c in pyrun_file Python/pythonrun.c:1296
    #29 0x5620e9b4d366 in _PyRun_SimpleFileObject Python/pythonrun.c:518

SUMMARY: AddressSanitizer: 10404 byte(s) leaked in 306 allocation(s).

I'm not sure how important this is given that it only happens during OOM.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Activity

  1. StanFromIreland commented on Jun 27, 2026

    @StanFromIreland
    Member

    These are known, see 28b63d3. Also #150536.

  2. added and removed
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Jun 27, 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 dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions