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.
This commit is contained in:
Peter Hawkins 2025-02-06 13:49:29 -05:00 committed by GitHub
parent 78377c788e
commit b4ff8b22b3
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