Skip to content

Comments

Fix sem_clockwait detection for cross-compiled builds#985

Open
andiradulescu wants to merge 2 commits intoastral-sh:mainfrom
andiradulescu:fix-sem-clockwait-weak-symbol
Open

Fix sem_clockwait detection for cross-compiled builds#985
andiradulescu wants to merge 2 commits intoastral-sh:mainfrom
andiradulescu:fix-sem-clockwait-weak-symbol

Conversation

@andiradulescu
Copy link

@andiradulescu andiradulescu commented Feb 22, 2026

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.

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.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.
…rom repo

All version-specific patches now follow the same naming convention.
The test script is kept locally for manual verification on devices.
@andiradulescu andiradulescu marked this pull request as ready for review February 22, 2026 22:16
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.

1 participant