Fix counter underflow race between semaphore fast-path CAS and slow-path fetch_sub
While stress-testing the semaphore under high contention, I identified a race between the lock-free fast path and the mutex-protected slow path in _try_acquire_or_wait that silently corrupts the token counter.
The race
When _cur_value is 1, two threads can both observe count > 0 before either has decremented it:
Thread A (fast path): loads cur = 1, enters CAS loop
Thread B (slow path): fast-path loop exits (cur refreshed to 0 via spurious CAS),
acquires _mtx, reloads cur with relaxed ordering,
sees cur = 1, calls fetch_sub(1, relaxed)
If Thread A's CAS and Thread B's fetch_sub both succeed on the same count-1 slot, the counter wraps to SIZE_MAX (unsigned underflow). Both threads return true. Both tasks run past the semaphore simultaneously. The concurrency limit is violated. In the test suite this shows up as counter > N in the critical section test under 2+ workers.
A secondary issue: the slow-path reload used memory_order_relaxed, so it could read a value that did not reflect a concurrent fast-path decrement that had already committed. This widened the race window unnecessarily.
The fix
Replace fetch_sub(1, relaxed) in the slow path with a compare_exchange_weak loop, matching the fast path. The CAS atomically validates that the value has not changed since the load. If a concurrent fast-path CAS already claimed the slot, the slow-path CAS fails, reloads, and either retries (count still > 0) or parks in _waiters (count now 0). Underflow is structurally impossible with this approach.
The slow-path reload is changed to memory_order_acquire to synchronize with any fast-path decrement or _release increment that completed before the mutex was taken.
The fast-path CAS success ordering is changed from memory_order_acquire to memory_order_acq_rel. The release half is necessary so that the decrement store is visible to acquire-loads on other threads. Without it a releaser reading the counter with an acquire-load could observe a stale value.
The _release increment is moved inside the mutex. Previously the counter was incremented with a lock-free CAS before _mtx was acquired to drain _waiters. This opened a window where a fast-path acquire stole the new token, then _release also woke all parked waiters, giving both sides a concurrent hold on the semaphore. Incrementing inside the lock closes that window: no fast-path acquire can see the new count until the drain completes.
The acquire fast path remains lock-free. The mutex is only taken when the count is exhausted.
Testing
All 34 existing semaphore tests pass (80388 assertions), including the high-thread-count critical section tests and the linear chain tests that accumulate large waiter lists and expose the contention patterns most likely to trigger this race.
@Mattbusel this is an interesting contribution. Since we are introducing the atomic variable, I wonder if there is a chance to completely remove the mutex and just leverage this atomic variable. Any thoughts?
@tsung-wei-huang I actually took a full run at this in #771, tried three separate lock-free approaches before concluding the mutex (or at minimum a spinlock) is structurally required on the write path:
- CAS on
_bottomwith re-read of_arrayin steal, resize installs a new array after the CAS but before the write, copies null for the claimed slot, item is invisible to stealers forever. - Resize spinning on null slots before copying, pusher re-reads
_arrayafter resize publishes the new one, writes to the new array, resize spins on the old array waiting for a write that went somewhere else. Deadlock. - Stable-write loop (write, check if
_arraychanged, rewrite), a second resize between the check and the rewrite creates a third array that copied null from the second. Same invisible-item problem.
The root cause is that resize and multi-producer push share the array pointer. Any read-then-write through that pointer can be invalidated by a concurrent resize. The steal path doesn't have this problem and stays fully lock-free.
In #771 I landed on replacing the std::mutex with an atomic_flag spinlock, held for one array write plus one atomic store, nanoseconds on an uncontended cache line, which gets the kernel overhead out of the hot path while keeping correctness. But fully removing serialization on the push side, I don't see a path.
https://github.com/taskflow/taskflow/pull/771
If you see an angle in there I missed, happy to revisit.