Skip to content

Fix sem_clockwait detection - #985

Merged
geofft merged 4 commits into
astral-sh:mainfrom
andiradulescu:fix-sem-clockwait-weak-symbol
Mar 9, 2026
Merged

geofft merged 4 commits into
astral-sh:mainfrom
andiradulescu:fix-sem-clockwait-weak-symbol

Conversation

@andiradulescu

@andiradulescu andiradulescu commented Feb 22, 2026 •

Copy link
Copy Markdown
Contributor

When building against glibc headers older than 2.30 (e.g., Debian Stretch's glibc 2.24), configure cannot detect sem_clockwait (added in glibc 2.30). This causes CPython to fall back to sem_timedwait with CLOCK_REALTIME, making threading.Event.wait() hang indefinitely when the system clock jumps backward (e.g., NTP sync).

This PR adds patches that declare sem_clockwait as a weak symbol on Linux when configure doesn't detect it. At runtime, the weak symbol resolves to the real function on glibc 2.30+ or to NULL on older glibc. The patched code checks this at runtime and falls back to sem_timedwait gracefully.

Version-specific patches for CPython 3.10, 3.11, 3.12, 3.13+ (including 3.14) and 3.15 (currently in alpha) account for differences in the threading internals across versions.

The issue was first discovered in commaai/openpilot#33993 (comment)

Test results

All patches verified by building on Debian Stretch (glibc 2.24, where sem_clockwait doesn't exist in headers) and running on Debian Bookworm (glibc 2.36):

Python Build glibc Runtime glibc Weak symbol resolves Event.wait(0.1)
3.10.19 2.24 (Stretch) 2.36 (Bookworm) real function 0.101s
3.11.14 2.24 (Stretch) 2.36 (Bookworm) real function 0.103s
3.12.12 2.24 (Stretch) 2.36 (Bookworm) real function 0.103s
3.13.12 2.24 (Stretch) 2.36 (Bookworm) real function 0.102s
3.14.3 2.24 (Stretch) 2.36 (Bookworm) real function 0.102s
3.15.0a6 2.24 (Stretch) 2.36 (Bookworm) real function 0.101s
3.12.12 2.24 (Stretch) 2.24 (Stretch) NULL → sem_timedwait fallback 0.101s

test-sem-clockwait.py

When cross-compiling against old glibc headers (e.g., Debian Stretch's
glibc 2.24), configure cannot detect sem_clockwait (added in glibc 2.30).
This causes CPython to fall back to sem_timedwait with CLOCK_REALTIME,
making threading.Event.wait() hang indefinitely when the system clock
jumps backward (e.g., NTP sync).

Add patches that declare sem_clockwait as a weak symbol on Linux when
configure didn't detect it. At runtime, the weak symbol resolves to the
real function on glibc 2.30+ or to NULL on older glibc. The patched code
checks this at runtime and falls back to sem_timedwait gracefully.

Version-specific patches for CPython 3.10, 3.11, 3.12, and 3.13+ account
for differences in the threading internals across versions.
@andiradulescu
andiradulescu marked this pull request as ready for review February 22, 2026 22:16
@jjhelmus

Copy link
Copy Markdown
Contributor

Looks like the patch needs to be modified to apply cleanly on 3.15 (currently in alpha).

@andiradulescu
andiradulescu force-pushed the fix-sem-clockwait-weak-symbol branch from a688da3 to 8ac6af1 Compare February 23, 2026 17:59
In 3.15, the semaphore lock code moved from thread_pthread.h to
parking_lot.c.
@andiradulescu
andiradulescu force-pushed the fix-sem-clockwait-weak-symbol branch from 8ac6af1 to 4f545d7 Compare February 23, 2026 18:02
@andiradulescu

Copy link
Copy Markdown
Contributor Author

Added the 3.15 patch. The 3.15 one is simpler than the others.

@jjhelmus

Copy link
Copy Markdown
Contributor

Cross compiling is not really the issue here. The project build both natively and cross against an older glibc for compatibility. It would be good if the comments described this more clearly. Perhaps something like:
"When building against glibc versions that do not include sem_clockwait ...."

@andiradulescu andiradulescu changed the title Fix sem_clockwait detection for cross-compiled builds Fix sem_clockwait detection Feb 26, 2026
@andiradulescu

andiradulescu commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor Author

You are right, cross compiling is not the issue here. Updated the patch comments and PR title/description.

@geofft geofft left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These changes look good to me, thanks very much for the PR! I'm slowly working on doing this kind of thing in general to all the newer symbols and I might end up reworking this one a bit in the process, but I think the argument about hanging (and the provided patch :) ) is a good one to do this one first.

@geofft
geofft merged commit 6d586fc into astral-sh:main Mar 9, 2026
970 of 981 checks passed
@geofft

geofft commented Mar 9, 2026

Copy link
Copy Markdown
Collaborator

We'll be doing a release this week anyway for CPython 3.15.0a7 so this should be available shortly.

greatgitsby pushed a commit to greatgitsby/openpilot that referenced this pull request Mar 10, 2026
…260310

Update requires-python from >=3.12.3 to >=3.12.13 to match the CPython
3.12.13 release in python-build-standalone 20260310.

This build contains the time patch needed to work on comma devices with
older glibc.

References:
- astral-sh/python-build-standalone#985
- commaai#33993 (comment)

https://claude.ai/code/session_01J4RLFmvFZibVpt1Tjgfxqv
greatgitsby added a commit to greatgitsby/openpilot that referenced this pull request Mar 10, 2026
…260310

Update requires-python from >=3.12.3 to >=3.12.13 to match the CPython
3.12.13 release in python-build-standalone 20260310.

This build contains the time patch needed to work on comma devices with
older glibc.

References:
- astral-sh/python-build-standalone#985
- commaai#33993 (comment)
greatgitsby added a commit to greatgitsby/openpilot that referenced this pull request Mar 10, 2026
…0310

Add .python-version pinning Python to 3.12.13 to match the CPython
3.12.13 release in python-build-standalone 20260310. The pyproject.toml
floor stays at >=3.12.3.

This build contains the time patch needed to work on comma devices with
older glibc.

References:
- astral-sh/python-build-standalone#985
- commaai#33993 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants