Skip to content
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

tracemalloc aborts when run from threads in no-gil #126315

Open
devdanzin opened this issue Nov 1, 2024 · 1 comment
Open

tracemalloc aborts when run from threads in no-gil #126315

devdanzin opened this issue Nov 1, 2024 · 1 comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-free-threading type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@devdanzin
Copy link
Contributor

devdanzin commented Nov 1, 2024

Crash report

What happened?

It's possible to get two different aborts from failed assertions, only with PYTHON_GIL=0, using the code below:

from threading import Thread
import _tracemalloc

alive = [
    Thread(target=_tracemalloc.start, args=(
        (memoryview(bytearray(b"abc\xe9\xff")),))),
    Thread(target=_tracemalloc.start, args=()),
    Thread(target=_tracemalloc.get_traceback_limit, args=()),
    Thread(target=_tracemalloc.start, args=()),
    Thread(target=_tracemalloc.stop, args=()),
    Thread(target=_tracemalloc.reset_peak, args=()),
    Thread(target=_tracemalloc.get_tracemalloc_memory, args=())
]

for obj in alive:
    print('START', obj)
    try:
        obj.start()
    except Exception:
        pass

The errors are:

python: Python/tracemalloc.c:445: tracemalloc_remove_trace: Assertion `tracemalloc_config.tracing' failed.
Aborted

And:

python: Python/tracemalloc.c:469: tracemalloc_add_trace: Assertion `tracemalloc_config.tracing' failed.
Aborted

Found with fusil by @vstinner.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.14.0a1+ experimental free-threading build (heads/main:d467d9246c, Nov 1 2024, 09:05:56) [GCC 11.4.0]

@devdanzin devdanzin added the type-crash A hard crash of the interpreter, possibly with a core dump label Nov 1, 2024
@rruuaanng
Copy link
Contributor

Maybe we can add HEAD lock to tracemalloc_config.tracing. This is most likely caused by concurrent operations on a global without locking. If possible, I'll make a PR.

@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-free-threading type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

3 participants