Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
graingert opened this issue Nov 13, 2024 · 2 comments
Closed
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@graingert
Copy link
Contributor

graingert commented Nov 13, 2024

Bug report

Bug description:

import linecache
import weakref


def gen_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)

    def cleanup_linecache(filename):
        def _cleanup():
            if filename in linecache.cache:
                del linecache.cache[filename]
        return _cleanup

    weakref.finalize(func, cleanup_linecache(filename))

    return func

def main():
    n = 0
    while True:
        func = gen_func(n)
        del func
        linecache.checkcache()
        n += 1
        if n % 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.py
Traceback (most recent call last):
  File "/home/graingert/projects/weakref-func-cycle-never-gc/demo.py", line 38, in <module>
    main()
  File "/home/graingert/projects/weakref-func-cycle-never-gc/demo.py", line 32, in main
    linecache.checkcache()
  File "/usr/lib/python3.12/linecache.py", line 64, in checkcache
    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", line 38, in <module>
    main()
    ~~~~^^
  File "/home/graingert/projects/weakref-func-cycle-never-gc/demo.py", line 32, in main
    linecache.checkcache()
    ~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/linecache.py", line 59, in checkcache
    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

@graingert graingert added the type-bug An unexpected behavior, bug, or error label Nov 13, 2024
graingert added a commit to graingert/cpython that referenced this issue Nov 13, 2024
@mattip
Copy link
Contributor

mattip commented Nov 13, 2024

Originally reported on pypy pypy/pypy#5109 and found in SymPy
cc @asmeurer

@graingert
Copy link
Contributor Author

oh I totally forgot to link to the pypy issue! I thought I had done that, sorry

@picnixz picnixz added the stdlib Python modules in the Lib dir label Nov 14, 2024
@graingert graingert added 3.12 only security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Dec 4, 2024
cfbolz pushed a commit that referenced this issue Dec 10, 2024
…fe (#126776)


Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Bartosz Sławecki <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 10, 2024
…ncy safe (pythonGH-126776)

(cherry picked from commit 2233c30)

Co-authored-by: Thomas Grainger <[email protected]>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Bartosz Sławecki <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Dec 10, 2024
…ncy safe (pythonGH-126776)

(cherry picked from commit 2233c30)

Co-authored-by: Thomas Grainger <[email protected]>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Bartosz Sławecki <[email protected]>
cfbolz pushed a commit that referenced this issue Dec 10, 2024
…ency safe (GH-126776) (#127779)

gh-126775: make linecache.checkcache threadsafe and GC re-entrency safe (GH-126776)

(cherry picked from commit 2233c30)

Co-authored-by: Thomas Grainger <[email protected]>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Bartosz Sławecki <[email protected]>
cfbolz pushed a commit that referenced this issue Dec 10, 2024
…ency safe (GH-126776) (#127778)

gh-126775: make linecache.checkcache threadsafe and GC re-entrency safe (GH-126776)

(cherry picked from commit 2233c30)

Co-authored-by: Thomas Grainger <[email protected]>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Bartosz Sławecki <[email protected]>
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 8, 2025
…ncy safe (python#126776)


Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Bartosz Sławecki <[email protected]>
@hugovk hugovk closed this as completed Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants