python / python/cpython

`tracemalloc` aborts when run from threads in no-gil

Open
#126,315 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.13 3.14 3.15 interpreter-core topic-free-threading type-crash
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied threaded example with an experimental free-threading build and PYTHON_GIL=0. Inspect Python/tracemalloc.c around tracemalloc_remove_trace and tracemalloc_add_trace, then verify that the example no longer aborts when concurrent tracemalloc operations are used.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.