Skip to content

Commit

Permalink
pythongh-129732: Fix race on shared->array in qsbr code under free-…
Browse files Browse the repository at this point in the history
…threading (pythongh-129738)

The read of `shared->array` should happen under the lock to avoid a race.
  • Loading branch information
hawkinsp authored Feb 6, 2025
1 parent 78377c7 commit b4ff8b2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a race in ``_Py_qsbr_reserve`` in the free threading build.
12 changes: 6 additions & 6 deletions Python/qsbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b4ff8b2

Please sign in to comment.