Skip to content

Commit f7cc862

Browse files
colesburyhawkinsp
andauthored
[3.13] gh-129732: Fix race on shared->array in qsbr code under free-threading (gh-129738) (gh-129747)
The read of `shared->array` should happen under the lock to avoid a race. (cherry picked from commit b4ff8b2) Co-authored-by: Peter Hawkins <[email protected]>
1 parent 356a9e6 commit f7cc862

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed
Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 6 deletions
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)