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 — Thread.join(timeout) can block forever: ThreadHandle_join reads the clock twice through Py_MAX (3.13, 3.14) · Issue #161712 · python/cpython

Thread.join(timeout) can block forever: ThreadHandle_join reads the clock twice through Py_MAX (3.13, 3.14) #161712

New issue
Copy link
New issue
Copy link
Open
Open
Thread.join(timeout) can block forever: ThreadHandle_join reads the clock twice through Py_MAX (3.13, 3.14)#161712
Copy link
Labels
3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@areeq-hasan
areeq-hasan
opened on Nov 9, 2026
Issue body actions

Bug report

ThreadHandle_join in Modules/_threadmodule.c recomputes the time left of a timed join as

// _PyDeadline_Get will return a negative value if the deadline has
// been exceeded.
timeout_ns = Py_MAX(_PyDeadline_Get(deadline), 0);

(3.13 branch, line 517; 3.14 branch, line 544; also in v3.13.16 and v3.14.0.)

Py_MAX(x, y) is (((x) > (y)) ? (x) : (y)) (Include/pymacro.h), so it evaluates _PyDeadline_Get(deadline) twice. The first read is compared with 0 and the second is the one returned. If the deadline passes between the two reads, the first is positive, the second is negative, and a negative timeout_ns is returned. The loop then calls PyEvent_WaitTimed(..., timeout_ns, ...) with it, and _PySemaphore_PlatformWait in Python/parking_lot.c treats timeout < 0 as "no timeout" (INFINITE on Windows; the untimed sem_wait/pthread_cond_wait branch on POSIX). The if (timeout_ns) check after it does not catch a negative value. Thread.join(timeout) then never returns.

_PyMutex_LockTimed in Python/lock.c clamps correctly ("Avoid negative values because those mean block forever"), so only the timed Thread.join is exposed.

Why it needs a wait that ends early. The window is the interval between the two clock reads, after a wait that ended a little before the deadline. On Windows, wait timeouts are counted in timer ticks (15.6 ms by default) while the deadline uses the high-resolution clock, so a wait often ends a fraction of a tick early and sends the loop around again. A program that joins in a loop with a short timeout gives it many chances.

Observed. A pyqtgraph window's GUI thread (Windows 11, Python 3.13.16 from conda-forge) polled worker.join(0.02) fifty times a second. It hung repeatedly, within 0.5 to 5.5 hours of starting. Caught live, the hung process's main thread used no CPU in ten seconds, its Python stack was in Thread.join, and its native stack, read with dbghelp and the matching python313.pdb, was ThreadHandle_join -> _PyParkingLot_Park -> _PySemaphore_Wait -> WaitForSingleObjectEx, with the timeout argument 0xffffffff (INFINITE). Its parking-lot wait entry was queued on the worker thread's handle with is_unparking 0, so it was in its first wait, the one that takes the join's own timeout, and that wait had been given none. The worker thread was alive and running throughout.

I could not provoke it in a synthetic test (22 million join(1e-6 .. 0.02) calls from 8 to 16 threads on macOS, 3.13.5, no overrun beyond 40 ms), since the window is microseconds wide.

Fix. main already reads the clock once, as part of gh-157495 (ba91215, "Avoid Py_MIN()/Py_MAX() side effects", GH-157591):

timeout_ns = _PyDeadline_Get(deadline);
timeout_ns = Py_MAX(timeout_ns, 0);

That commit was a cleanup of several files and was not marked as a bug fix, so it was not backported. Could the _threadmodule.c hunk be backported to 3.13 and 3.14? It is one line and changes nothing else.

Your environment

  • CPython versions tested: 3.13.16 (conda-forge, Windows 11); the lines are the same on the 3.14 branch
  • Operating system: Windows 11

Linked PRs

  • [3.15] gh-161712: Read the clock once in ThreadHandle_join() #161771
  • gh-161712: Read the clock once in ThreadHandle_join() #161794
  • [3.14] gh-161712: Read the clock once in ThreadHandle_join() (GH-161771) #161843

Activity

  1. aisk
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 10, 2026
  2. aisk
    added this to Threading issues 🧵on Nov 10, 2026
  3. aisk
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Nov 10, 2026
  4. ZeroIntensity
    added
    3.14bugs and security fixesbugs and security fixes
    3.15bugs and security fixesbugs and security fixes
    3.16new features, bugs and security fixesnew features, bugs and security fixes
    on Nov 11, 2026
  5. ZeroIntensity commented on Nov 11, 2026

    @ZeroIntensity
    ZeroIntensity
    on Nov 11, 2026
    Member
    More actions

    3.13 is now security-only, so we can't fix it there, but we can fix it on anything newer.

  6. masenf
    added a commit that references this issue on Nov 12, 2026

    Don't wait in Thread.join(timeout) when draining supervised output (#…

    9c44f07
  7. ZeroIntensity
    added a commit that references this issue on Nov 12, 2026

    [3.15] gh-161712: Read the clock once in `ThreadHandle_join()` (GH-16…

    88d137a
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

    3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • Threading issues 🧵
      Status
      No status
      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.