Crash in free-threaded build: concurrent `BytesIO.write()` and replacing the shared `BytesIO(<existing bytes>)` from several threads · Issue #163204 · python/cpython
Repository navigation
Crash in free-threaded build: concurrent BytesIO.write() and replacing the shared BytesIO(<existing bytes>) from several threads #163204
New issue
New issue
Backlog
Backlog
Crash in free-threaded build: concurrent
BytesIO.write() and replacing the shared BytesIO(<existing bytes>) from several threads #163204
Labels
extension-modules
topic-IO
topic-free-threading
type-crash
Description
@espressolee's profile espressolee opened on Dec 5, 2026Summary
On a free-threaded build of current main, four threads that share one io.BytesIO slot can crash the interpreter. Each thread randomly either calls write() on whatever BytesIO the slot holds, or replaces the slot with a new io.BytesIO(data) built from one existing bytes object. In 10 of 10 runs on a release build the process died with SIGSEGV, and in 10 of 10 runs on a debug build it aborted (_PyBytes_CheckOverflow or a mimalloc free-list check). The same script does not crash with -X gil=1 (0 of 10 per build). I understand that sharing a mutable object across threads without a lock is not guaranteed to give meaningful results, but I would expect a crash-free BytesIO on free-threaded builds, so I am reporting it. If this use is unsupported, please say so and I will close the issue.
Environment
- CPython
mainat commit730a529bcdbcf977581f93cfc771fdb73d6b299d(built from the GitHub source archive for that commit). - macOS 27, arm64, Apple clang 21.
- Release build:
./configure --disable-gil && make -j8
3.16.0a2+dev free-threading build (main, Dec 5 2026, 12:14:06) [Clang 21.0.0 (clang-2100.4.1.1)] - Debug build:
./configure --disable-gil --with-pydebug && make -j8
3.16.0a2+dev free-threading build (main, Dec 5 2026, 12:16:37) [Clang 21.0.0 (clang-2100.4.1.1)] - On both,
sys._is_gil_enabled()isFalseby default andsysconfig.get_config_var("Py_GIL_DISABLED") == 1.
Reproducer
Standard library only. Run as python -I -B -X faulthandler repro.py SEED from an empty directory (SEED is any integer; I used 501 to 510).
import io
import random
import sys
import threading
import time
SEED = int(sys.argv[1]) if len(sys.argv) > 1 else 1
DURATION = 20.0
NTHREADS = 4
DATA = bytes(range(256)) * 4 # an existing bytes object, shared by all threads
shared = [io.BytesIO()]
deadline = time.monotonic() + DURATION
def worker(tid):
rng = random.Random(SEED * 1000 + tid)
while time.monotonic() < deadline:
try:
if rng.randrange(2):
shared[0].write(b"x" * rng.randrange(1024))
else:
shared[0] = io.BytesIO(DATA)
except Exception:
pass
threads = [threading.Thread(target=worker, args=(i,)) for i in range(NTHREADS)]
for t in threads:
t.start()
for t in threads:
t.join()
print("DONE")
Reduction: starting from a script that picked uniformly among write, read, seek, truncate, getvalue, getbuffer().release(), replace-with-BytesIO() and replace-with-BytesIO(data), I removed one operation at a time and re-ran (6 runs per step). Only write and replace-with-BytesIO(data) were needed: removing either one gave 0 of 6 crashes. With those two operations, 3 threads or more crashed 6 of 6 and 2 threads crashed 2 of 6. (The earlier eight-operation script crashed 6 of 6 as well.)
Results
Each run lasts at most 20 seconds and counts as a crash if the process died from a signal. 10 runs per cell, seeds 501 to 510.
Build (main 730a529 ) |
default (GIL disabled) | -X gil=1 |
|---|---|---|
release, --disable-gil |
10/10 SIGSEGV | 0/10 |
debug, --disable-gil --with-pydebug |
10/10 SIGABRT | 0/10 |
Crash messages, first lines (paths removed). Debug builds fail in different places from run to run:
mimalloc: error: thread 0x...: corrupted free list entry of size 1280b at 0x...: value 0x...
Fatal Python error: Aborted
Fatal Python error: _PyBytes_CheckOverflow: Buffer overflow detected in bytes object 0x... at position 1024
Innermost C frames on the release build (faulthandler, offsets omitted), two different runs:
_mi_malloc_generic
bytes_alloc
resize_buffer_lock_held
write_bytes_lock_held
_io_BytesIO_write
_PyEval_EvalFrameDefault
_mi_malloc_generic
bytes_alloc
write_bytes_lock_held
_io_BytesIO_write
_PyEval_EvalFrameDefault
On the debug build one mimalloc abort was reached from bytes_alloc called by unshare_buffer_lock_held.
Other runs
- A longer random-operation script (the eight operations above, 4 threads, up to 60 s, seeds 301 to 310) crashed 10/10 on the release build (SIGSEGV) and 10/10 on the debug build (SIGABRT); 0/10 with
-X gil=1on each build. - A different script (8 threads, 10 s: write 100000 bytes into the shared
BytesIO, then replace the slot with a freshBytesIO; seeds 301 to 320) crashed only 1/20 on each build (SIGSEGV on release,_PyBytes_CheckOverflowabort on debug), 0/20 with-X gil=1. It is much less reliable than the reproducer above.
Suspected area
Only from the stacks, not confirmed: the crashes are heap corruption that surfaces in bytes_alloc or in the bytes deallocation check, with BytesIO.write (write_bytes_lock_held, resize_buffer_lock_held) and unshare_buffer_lock_held on the stack. The operations needed (write, plus constructing a BytesIO from an existing bytes object that other instances also use) suggest the path where a BytesIO initially shares its argument as its buffer and later copies or resizes it. I have not located the faulty line.
Related issues
- Segfault/abort from calling
BytesIOunshare_bufferin threads on a free-threaded build #132551 / gh-132551: make BytesIO object free-thread safe #132616 (makeBytesIOfree-thread safe) were merged in 2025. This report is on a latermain, so it may be a path that change did not cover, or a regression. - Data race between
resize_buffer_lock_heldand_Py_DecRefSharedon a sharedBytesIObuffer #151640 / gh-151640: Track sharing of BytesIO buffer in free-threaded builds #151651 (track sharing of theBytesIObuffer in free-threaded builds) covered a buffer shared throughread(); here the sharing comes from theBytesIO(bytes)constructor, and the crash still happens on thismain. - Segfault between
_io_StringIO___init___impland_io_StringIO_truncate_implwith free-threading #150695 (StringIO.__init__vsStringIO.truncate) is the same class of problem in another module.
Linked PRs
Metadata
Metadata
Assignees
No one assignedLabels
extension-modules
topic-IO
topic-free-threading
type-crash