Free threading: Data race on `tool_id` in `sys.monitoring`
Open
Nobody has claimed this yet.
interpreter-core
topic-free-threading
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
Thanks devdanzin for his fusil, this issue was reported by #153852 TSAN-0030. It;s a data race about interp->monitoring_tool_names[tool_id]
how to reproduce
run the following on a no-gil tsan build
import sys, threading
assert not sys._is_gil_enabled(), "run free-threaded: PYTHON_GIL=0"
# sys.monitoring.use_tool_id(tool_id, name) does an unsynchronized check-then-act on the
# interpreter-global registry interp->monitoring_tool_names[tool_id]:
# if (interp->monitoring_tool_names[tool_id] != NULL) { raise; } # :2190 read
# interp->monitoring_tool_names[tool_id] = Py_NewRef(name); # :2194 write
# Many threads racing to claim the SAME free tool id read the slot while one writes it.
mon = sys.monitoring
TOOL_ID = 3 # any 0..5; not reserved
NT = 8
ROUNDS = 6000
enter = threading.Barrier(NT + 1)
leave = threading.Barrier(NT + 1)
def worker():
for _ in range(ROUNDS):
enter.wait() # all workers released together onto a freshly-freed slot
try:
mon.use_tool_id(TOOL_ID, "t") # read :2190 races the winner's write :2194
except ValueError:
pass # "tool 3 is already in use" -> lost the race, expected
leave.wait()
ts = [threading.Thread(target=worker, name=f"w{i}") for i in range(NT)]
for t in ts:
t.start()
for r in range(ROUNDS):
mon.free_tool_id(TOOL_ID) # NULL the slot so the next round starts from a free id
enter.wait()
leave.wait()
for t in ts:
t.join()
print("done, no crash")
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-154459
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the sys.monitoring.use_tool_id() and free_tool_id() entry points referenced around lines 2190 and 2194, then run the supplied reproducer on a no-GIL TSAN build. Done means the repeated concurrent claim/free test no longer reports a data race while preserving the expected tool-id behavior; linked PR gh-154459 indicates work is already underway.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100