Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

linecache.checkcache() is not threadsafe or GC finalizer re-entrancy safe #126775

Closed
Labels
3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@graingert

Description

@graingert

Bug report

Bug description:

importlinecacheimportweakrefdefgen_func(n):func_code="""def func():    pass"""g= {}exec(func_code,g,g)func=g['func']filename=f"<generated-{n}>"linecache.cache[filename]= (len(func_code),None,func_code.splitlines(True),filename)defcleanup_linecache(filename):def_cleanup():iffilenameinlinecache.cache:dellinecache.cache[filename]return_cleanupweakref.finalize(func,cleanup_linecache(filename))returnfuncdefmain():n=0whileTrue:func=gen_func(n)delfunclinecache.checkcache()n+=1ifn%100000==0:print(n)if__name__=='__main__':main()

This crashes with a KeyError every time for me on 3.12 and 3.13, but some people report that it never crashes on CPython:

 ✘  graingert@conscientious  ~/projects/weakref-func-cycle-never-gc   main  python demo.pyTraceback (most recent call last):  File"/home/graingert/projects/weakref-func-cycle-never-gc/demo.py", line38, in<module>    main()  File"/home/graingert/projects/weakref-func-cycle-never-gc/demo.py", line32, inmain    linecache.checkcache()  File"/usr/lib/python3.12/linecache.py", line64, incheckcache    entry= cache[filename]~~~~~^^^^^^^^^^KeyError:'<generated-147>' ✘  graingert@conscientious  ~/projects/weakref-func-cycle-never-gc   main  phyt  ✘  graingert@conscientious  ~/projects/weakref-func-cycle-never-gc   main  python3.13 demo.py Traceback (most recent call last):  File"/home/graingert/projects/weakref-func-cycle-never-gc/demo.py", line38, in<module>    main()~~~~^^  File"/home/graingert/projects/weakref-func-cycle-never-gc/demo.py", line32, inmain    linecache.checkcache()~~~~~~~~~~~~~~~~~~~~^^  File"/usr/lib/python3.13/linecache.py", line59, incheckcache    entry= cache[filename]~~~~~^^^^^^^^^^KeyError:'<generated-2637>'

The script seems to run "forever" on Python3.9, 3.10 and 3.11 3.14.0a1+ (heads/main:ba088c8f9cf.

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

No response

Linked PRs

important meta issue:

This reproducer absolutely should not reproduce on cpython!!

There does seem to be another issue, because this function should be deleted instantly because its refcount drops to 0, and never run the finalizer during the linecache.checkcache call

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp