From c45a7c7660e644513667d4aa2f0f766e9b444b8c Mon Sep 17 00:00:00 2001 From: Vadim Skipin Date: Thu, 7 May 2026 13:46:57 +0000 Subject: [PATCH] Explain why it is safe to use isFiberRunning --- src/fibers/mutex.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/fibers/mutex.cpp b/src/fibers/mutex.cpp index 7bbcae4..9622bb1 100644 --- a/src/fibers/mutex.cpp +++ b/src/fibers/mutex.cpp @@ -45,6 +45,13 @@ void FiberMutex::lock() noexcept // Spin briefly before suspending: if the owner is on another CPU and releases // within ~500 ns, we avoid the full scheduler wakeup path. // Skip if there are already waiters in the queue. + // + // The owner pointer is loaded from a stale snapshot of state, so by the time + // we call isFiberRunning the original owner may have unlocked, returned, and + // been recycled by the fiber pool into a different fiber. The pool never + // unmaps Fiber memory, so the load on owner->state is always safe; the worst + // case is a spurious 500 ns spin against the wrong fiber's state, after which + // lockHelper takes the slow path. No correctness consequence. if (!currentState.hasWaiters) { Fiber * owner = reinterpret_cast(currentState.owner);