[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 b4ff8b22b3)

Co-authored-by: Peter Hawkins <phawkins@google.com>
This commit is contained in:
Sam Gross 2025-02-06 14:30:16 -05:00 committed by GitHub
parent 356a9e646c
commit f7cc862345
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 6 deletions

View file

@ -205,15 +205,15 @@ _Py_qsbr_reserve(PyInterpreterState *interp)
}
_PyEval_StartTheWorld(interp);
}
PyMutex_Unlock(&shared->mutex);
if (qsbr == NULL) {
return -1;
}
// Return an index rather than the pointer because the array may be
// resized and the pointer invalidated.
return (struct _qsbr_pad *)qsbr - shared->array;
Py_ssize_t index = -1;
if (qsbr != NULL) {
index = (struct _qsbr_pad *)qsbr - shared->array;
}
PyMutex_Unlock(&shared->mutex);
return index;
}
void