Skip to content

Ensure current thread state is cleared before deleting it in _PyThreadState_DeleteCurrentย #116515

Closed
@colesbury

Description

@colesbury

Feature or enhancement

In general, when _PyThreadState_GET() is non-NULL then the current thread is "attached", but there is a small window during PyThreadState_DeleteCurrent() where that's not true: tstate_delete_common() is called when the thread is detached, but before current_fast_clear():

cpython/Python/pystate.c

Lines 1689 to 1691 in 601f3a7

tstate_set_detached(tstate, _Py_THREAD_DETACHED);
tstate_delete_common(tstate);
current_fast_clear(tstate->interp->runtime);

I think it's worth swapping the order of the calls so that we call current_fast_clear before tstate_delete_common. This would also make the order of operations in PyThreadState_DeleteCurrent() more similar to the order used when calling PyThreadState_Delete().

See also: #116483

cc @ericsnowcurrently

Linked PRs

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions