Skip to content

gh-115035: Mark ThreadHandles as non-joinable earlier after forking - #115042

Merged
colesbury merged 2 commits into
python:mainfrom
colesbury:gh-115035-thread-handle
Feb 6, 2024
Merged

colesbury merged 2 commits into
python:mainfrom
colesbury:gh-115035-thread-handle

Conversation

@colesbury

@colesbury colesbury commented Feb 5, 2024 •

Copy link
Copy Markdown
Contributor

This marks dead ThreadHandles as non-joinable earlier in PyOS_AfterFork_Child() before we execute any Python code. The handles are stored in a global linked list in _PyRuntimeState because fork() affects the entire process.

This marks dead ThreadHandles as non-joinable earlier in
`PyOS_AfterFork_Child()` before we execute any Python code. The handles
are stored in a global linked list in `_PyRuntimeState` because `fork()`
affects the entire process.
@colesbury colesbury changed the title gh-115035: Mark ThreadHandles as non-joinable earlier Feb 5, 2024
@colesbury

Copy link
Copy Markdown
Contributor Author

This doesn't call PyThread_update_thread_after_fork(). As far as I can tell, that call did not have any effect: we determine the current thread using PyThread_get_thread_ident_ex() (via get_ident) 1 so there's no way we could have a thread with a stale identifier. And if we don't find a matching thread, we create a new _MainThread() with the correct current identifier (and no handle).

Footnotes

  1. https://github.com/python/cpython/blob/c32bae52904723d99e1f98e2ef54570268d86467/Lib/threading.py#L1725-L1731 ↩

@pitrou

pitrou commented Feb 6, 2024

Copy link
Copy Markdown
Member

As far as I can tell, that call did not have any effect: we determine the current thread using PyThread_get_thread_ident_ex() (via get_ident) 1 so there's no way we could have a thread with a stale identifier.

That's a good point, but then we can probably remove the PyThread_update_thread_after_fork function?

Comment thread Include/internal/pycore_pythread.h Resolved
Comment thread Modules/_threadmodule.c Resolved
@colesbury
colesbury requested a review from pitrou February 6, 2024 15:51

@pitrou pitrou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Neat, thank you @colesbury !

@pitrou

pitrou commented Feb 6, 2024

Copy link
Copy Markdown
Member

Note: you're a core developer now ;-)

@colesbury
colesbury merged commit b6228b5 into python:main Feb 6, 2024
@colesbury
colesbury deleted the gh-115035-thread-handle branch February 6, 2024 19:45
@gpshead

gpshead commented Feb 7, 2024

Copy link
Copy Markdown
Member

nice refactoring, thanks! happy first-ish merge!

fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this pull request Feb 14, 2024
…king (python#115042)

This marks dead ThreadHandles as non-joinable earlier in
`PyOS_AfterFork_Child()` before we execute any Python code. The handles
are stored in a global linked list in `_PyRuntimeState` because `fork()`
affects the entire process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants