Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Fix race condition where threads created by PyGILState_Ensure() could get a
duplicate id.

This affects consumers of tstate->id like the contextvar caching machinery,
which could return invalid cached objects under heavy thread load (observed
in embedded scenarios).
3 changes: 1 addition & 2 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,13 +606,12 @@ new_threadstate(PyInterpreterState *interp, int init)
tstate->context = NULL;
tstate->context_ver = 1;

tstate->id = ++interp->tstate_next_unique_id;

if (init) {
_PyThreadState_Init(tstate);
}

HEAD_LOCK(runtime);
tstate->id = ++interp->tstate_next_unique_id;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, is the reason for the problem that the increment was done unlocked?

Copy link
Contributor Author

@skrah skrah Mar 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The location is unfortunate, but _PyThreadState_Init(tstate) does not use this field.

tstate->prev = NULL;
tstate->next = interp->tstate_head;
if (tstate->next)
Expand Down