Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
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
- gh-126775: make linecache.checkcache threadsafe and GC re-entrency safe #126776
- [3.13] gh-126775: make linecache.checkcache threadsafe and GC re-entrency safe (GH-126776) #127778
- [3.12] gh-126775: make linecache.checkcache threadsafe and GC re-entrency safe (GH-126776) #127779
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