BUG: free-threaded build crashes when dtype.names is assigned while other threads use the same structured dtype
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
Start by reproducing with the free-threaded interpreter (GIL disabled) using the script in the report; the crash is quick and deterministic. Read arraydescr_names_set in descriptor.c, which swaps names/fields without synchronization, then the borrowed-reference readers it races with: _get_field_view in mapping.c, _buffer_format_string in buffer.c, and get_fields_traverse_function in dtype_traversal.c (the crashing frame). The question to settle first is the intended contract for mutating a dtype in use — locking around the swap versus dropping the borrowed-ref assumptions at those call sites. Done means the reproducer exits cleanly across all three reader variants with the GIL off, plus the dtype test suite passes.
索引モデルが issue の本文から書いたものです。
説明
Describe the issue:
On the free-threaded build (GIL disabled), if one thread repeatedly assigns dtype.names on a structured dtype while other threads use an array of that same dtype, the process crashes.
Observed with three ordinary reader operations:
arr["a"]->Fatal Python error: PyMutex_Unlock: unlocking mutex that is not lockedmemoryview(arr)-> segfaultarr[...] = 0-> segfault
The reproducer below crashed in 9/9 runs on 2.5.5 (3 per operation), each within ~0.1 s. Without the writer thread, 9/9 runs exit cleanly. With the GIL forced on (PYTHON_GIL=1), 9/9 runs exit cleanly.
Suspected cause (from reading the source): arraydescr_names_set (descriptor.c) replaces self->names and self->fields and drops the old objects without any locking, while readers use borrowed references to them, e.g. _get_field_view (mapping.c), _buffer_format_string (buffer.c) and get_fields_traverse_function (dtype_traversal.c); several of these sites are marked // noqa: borrowed-ref OK. In the backtrace below, get_fields_traverse_function passes a NULL result of the fields lookup to PyArg_ParseTuple (fault address 0x18).
I realize mutating a dtype that other threads are using is unusual, but dtype.names assignment is plain Python and the borrowed-reference assumption at these sites doesn't hold once it happens.
Reproduce the code example:
import threading
import numpy as np
dt = np.dtype([("a", np.int32), ("b", np.float64)])
arr = np.zeros(2, dtype=dt)
stop = threading.Event()
def writer():
for i in range(200_000):
dt.names = ("c", "d") if i % 2 == 0 else ("a", "b")
stop.set()
def reader():
while not stop.is_set():
memoryview(arr).release() # also crashes with: arr["a"] (catch ValueError), arr[...] = 0
threads = [threading.Thread(target=writer)] + [threading.Thread(target=reader) for _ in range(4)]
for t in threads: t.start()
for t in threads: t.join()
Error message:
lldb, `arr[...] = 0` variant, numpy 2.5.5 (crashing thread only):
* thread #4, name = 'Thread-3 (reader)', stop reason = EXC_BAD_ACCESS (code=1, address=0x18)
* frame #0: python`PyArg_ParseTuple + 56
frame #1: _multiarray_umath.cpython-314t-darwin.so`get_fields_traverse_function + 220
frame #2: _multiarray_umath.cpython-314t-darwin.so`npy_get_zerofill_void_and_legacy_user_dtype_loop + 340
frame #3: _multiarray_umath.cpython-314t-darwin.so`PyArray_NewFromDescr_int + 1044
frame #4: _multiarray_umath.cpython-314t-darwin.so`PyArray_NewFromDescr + 36
frame #5: _multiarray_umath.cpython-314t-darwin.so`PyArray_CopyObject + 256
frame #6: _multiarray_umath.cpython-314t-darwin.so`array_assign_subscript + 1012
frame #7: python`_TAIL_CALL_STORE_SUBSCR + 112
Python and NumPy Versions:
2.5.5
3.14.6 free-threading build (main, Jul 18 2026, 17:07:52) [Clang 22.1.3 ]
(sys._is_gil_enabled() == False after importing numpy)
Runtime Environment:
[{'numpy_version': '2.5.5',
'python': '3.14.6 free-threading build (main, Jul 18 2026, 17:07:52) [Clang 22.1.3 ]',
'uname': uname_result(system='Darwin', release='27.2.0', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP', 'ASIMDDP'],
'not_found': ['ASIMDFHM', 'SVE']}},
{'ignore_floating_point_errors_in_matmul': True}]
Official wheel numpy-2.5.5-cp314-cp314t-macosx_14_0_arm64.whl, macOS arm64 (native). Also reproduces on 2.5.4.
How does this issue affect you or how did you find it:
Found while testing code under free-threaded CPython; not blocking anything for me, reporting because it is a hard crash reachable from pure Python.
- 主要言語
- Python
- スター
- 33.5k
- フォーク
- 13.1k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 143
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
numpy/numpy のほかの issue
-
00 - Bug component: numpy.linalg
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
00 - Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
00 - Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
numpy/numpy#33422 · コメント 6 件 ·
メンテナーはふだん 1 日以内に返信
-
BUG: f2py treats a backslash as an escape inside character literals対応中かも @HaoZeke が 17 日前に担当しました。 オープン00 - Bug component: numpy.f2py
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
numpy/numpy#33220 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
DOC: use CITATION.cff file format for citation information対応中かも @JaroCamphuijsen が 58 日前に担当しました。 オープン04 - Documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
numpy/numpy#32817 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
521xueweihan/HelloGitHub#4543 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
emeryberger/CSrankings#15700 ·
メンテナーはふだん 2 日以内に返信
-
Bug | 工作不正常 Triage | 待检查
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
OneDragon-Anything/ZenlessZoneZero-OneDragon#3084 ·
メンテナーはふだん 1 日以内に返信
-
Task select patterns with uppercase letters never match対応中かも @manobhisriram が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信