GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — threading Thread.join should call the OS join API · Issue #110829 · python/cpython

threading Thread.join should call the OS join API #110829

New issue
Copy link
New issue
Copy link
Closed
Closed
threading Thread.join should call the OS join API#110829
Copy link
Labels
type-featureA feature request or enhancementA feature request or enhancement

Description

@gpshead
gpshead
opened on Oct 13, 2023
Issue body actions

Feature or enhancement

Proposal:

threading.Thread.join() only waits for the CPython internals to wash its hands of the underlying thread. It doesn't actually wait for the OS thread itself to exit, which in theory happens rapidly as its final internal code completes quickly - but we have no good wait to determine.

Why finally do this now? Now that we're encouraging people to notice and avoid threading existing when os.fork is called in 3.12, a use case has come up for deterministically knowing when the thread is done at the OS level so that the code can proceed with os.fork.

#110510 could use this in an atfork before fork handler for example.

POSIX has pthread_join, we should be able to expose and use via _thread. Windows presumably has an equivalent concept API.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

  • GH-110829: Ensure Thread.join() joins the OS thread #110848
Reacted by Antoine Pitrou and sunmy2019
Reacted by Łukasz Langa

Activity

  1. gpshead
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Oct 13, 2023
  2. gpshead
    added this to Threading issues 🧵on Oct 13, 2023
  3. pitrou commented on Oct 13, 2023

    @pitrou
    pitrou
    on Oct 13, 2023
    Member
    More actions

    On Windows, we start the thread with _beginthreadex, so we should be able to call WaitForSingleObject to ensure that the thread has properly terminated. See second example here: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/beginthread-beginthreadex?view=msvc-170#examples

    But, of course, on Windows the entire fork() problem doesn't exist, so it's less of a problem if we don't provide lesser guarantees.

    Note that right now we detach the thread as soon as it is started, so we may want to expose a different internal API that doesn't detach the thread.

  4. pitrou
    added 4 commits that reference this issue on Oct 13, 2023

    pythonGH-110829: Ensure Thread.join() joins the OS thread

    d78e969

    pythonGH-110829: Ensure Thread.join() joins the OS thread

    ef411ef

    pythonGH-110829: Ensure Thread.join() joins the OS thread

    41b9cba

    pythonGH-110829: Ensure Thread.join() joins the OS thread

    1ee76f6
  5. ericsnowcurrently commented on Oct 25, 2023

    @ericsnowcurrently
    ericsnowcurrently
    on Oct 25, 2023
    Member
    More actions

    FWIW, the gap between Python-thread-ends and OS-thread-exits bit me after I added the per-interpreter GIL (PEP 684). We ended up with crash due to that interval (a race in the GIL's drop_gil(), which I fixed in gh-105109. The linked PRs on gh-104341 include a couple other approaches I tried out to solve the race (with extra complexity), and IIRC at least one of them would have (partially?) dealt with closing the above gap.

  6. pitrou commented on Oct 25, 2023

    @pitrou
    pitrou
    on Oct 25, 2023
    Member
    More actions

    The API added here is only for the threading module, though, because it's quite inflexible (see comments in the .h file).

    Reacted by Eric Snow
  7. pitrou
    added 4 commits that reference this issue on Oct 27, 2023

    pythonGH-110829: Ensure Thread.join() joins the OS thread

    48482e9

    pythonGH-110829: Ensure Thread.join() joins the OS thread

    e6cad0f

    pythonGH-110829: Ensure Thread.join() joins the OS thread

    4074b59

    GH-110829: Ensure Thread.join() joins the OS thread (#110848)

    0e9c364
  8. hugovk commented on Nov 9, 2023

    @hugovk
    hugovk
    on Nov 9, 2023
    Member
    More actions

    Closing as the PR has been merged.

  9. hugovk
    closed this as completedon Nov 9, 2023
  10. github-project-automation
    moved this to Done in Threading issues 🧵on Nov 9, 2023
  11. pitrou commented on Nov 9, 2023

    @pitrou
    pitrou
    on Nov 9, 2023
    Member
    More actions

    Oops, sorry for forgetting to close this issue!

    Reacted by Hugo van Kemenade
  12. aisk
    added a commit that references this issue on Feb 11, 2024

    pythonGH-110829: Ensure Thread.join() joins the OS thread (python#110848

    a9d9778
  13. Glyphack
    added a commit that references this issue on Sep 2, 2024

    pythonGH-110829: Ensure Thread.join() joins the OS thread (python#110848

    ed1118f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Threading issues 🧵
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.