Skip to content

Commit b4ff8b2

Browse files
authored
gh-129732: Fix race on shared->array in qsbr code under free-threading (gh-129738)
The read of `shared->array` should happen under the lock to avoid a race.
1 parent 78377c7 commit b4ff8b2

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`` in the free threading build.

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)