Skip to content

fix(semaphore): use absolute deadline for POSIX TimedWait#17

Open
Rakdos8 wants to merge 1 commit into
carbonengine:mainfrom
Rakdos8:fix/h2-semaphore-timedwait-posix
Open

fix(semaphore): use absolute deadline for POSIX TimedWait#17
Rakdos8 wants to merge 1 commit into
carbonengine:mainfrom
Rakdos8:fix/h2-semaphore-timedwait-posix

Conversation

@Rakdos8
Copy link
Copy Markdown

@Rakdos8 Rakdos8 commented May 15, 2026

Problem

CcpSemaphore::TimedWait (generic POSIX path) passed a relative duration to sem_timedwait, which expects an
absolute deadline against CLOCK_REALTIME. The computed timespec (tv_sec = timeoutMs/1000, i.e. a few seconds
after the epoch) is always far in the past, so the wait timed out immediately without ever blocking on Linux/Android —
TimedWait returned false instantly regardless of the requested timeout. The pre-existing test
CcpSemaphore.DefaultConstructorCreatesNonSignaledSemaphore, which expects a ~1000 ms wait, was already failing on
the generic POSIX path because of this. macOS (mach semaphore) and Windows paths are not affected.

Fix

TimedWait now derives an absolute deadline from clock_gettime(CLOCK_REALTIME), adds the requested timeout, and
normalizes the nanosecond overflow before calling sem_timedwait. Added #include <time.h>.

Tests

Added three tests in tests/CcpSemaphore.cpp (GoogleTest): TimedWaitWaitsAndReturnsFalseOnTimeout blocks ~500 ms
and returns false (direct regression test for the relative/absolute confusion),
TimedWaitReturnsTrueWhenAlreadySignaled returns true immediately, and
TimedWaitReturnsTrueWhenSignaledBeforeTimeout returns true when signaled from another thread before the deadline.
Reviewed manually; not compiled locally as the vcpkg toolchain is unavailable in this environment.

Scope

Generic POSIX branch only (#else). macOS and Windows code paths are unchanged. No public API change.

sem_timedwait takes an absolute CLOCK_REALTIME deadline, but the code
passed a relative duration, placing the deadline near the epoch so the
wait always timed out instantly without blocking on Linux/Android.
Compute the deadline from clock_gettime(CLOCK_REALTIME). Add tests
covering timeout, already-signaled and signaled-before-timeout cases.
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