Skip to content

Commit c15a0ba

Browse files
committed
gh-129732: Fix race in on shared->array in qsbr code under free-threading.
The read of shared->array should happen under the lock to avoid a race. Fixes #129732
1 parent 4d56c40 commit c15a0ba

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a race in _Py_qsbr_reserve under free-threading mode.

Python/qsbr.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,15 @@ _Py_qsbr_reserve(PyInterpreterState *interp)
205205
}
206206
_PyEval_StartTheWorld(interp);
207207
}
208-
PyMutex_Unlock(&shared->mutex);
209-
210-
if (qsbr == NULL) {
211-
return -1;
212-
}
213208

214209
// Return an index rather than the pointer because the array may be
215210
// resized and the pointer invalidated.
216-
return (struct _qsbr_pad *)qsbr - shared->array;
211+
Py_ssize_t index = -1;
212+
if (qsbr != NULL) {
213+
index = (struct _qsbr_pad *)qsbr - shared->array;
214+
}
215+
PyMutex_Unlock(&shared->mutex);
216+
return index;
217217
}
218218

219219
void

0 commit comments

Comments
 (0)