Rework Mutex#33
Merged
Merged
Conversation
rracariu
reviewed
Jun 18, 2026
rracariu
reviewed
Jun 18, 2026
rracariu
approved these changes
Jun 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix concurrent watch operations and strengthen async primitives
Motivation
The
Muteximplementation had correctness issues under concurrent load, and severalApiSubjectmethods had TOCTOU (check-then-act) races that could cause double-opens, interleaved state mutations, and stuck locks after network errors. Test coverage for the async behaviour inMutex,ApiSubject, andBatchSubjectwas also incomplete.Changes
src/util/Mutex.tsRewrote from a single-promise busy-wait loop to an explicit FIFO waiter queue:
wait()could return while a queued task was about to startacquire(): Promise<() => void>primitive that returns a one-shotreleasefunction, allowing callers to hold the lock across a multi-step critical sectionrunSequentialandwaitare now built on top ofacquire/release, removing the duplicated wait-loop logicreleaseis idempotent — calling it more than once is a no-opsrc/client/watches/ApiSubject.tsFixed six TOCTOU races caused by the
await mutex.wait()+ separaterunSequential()pattern, which left a gap where another caller could interleave between a state check and the action taken on that state:add,remove,refresh,checkClose, andpollnow callacquire()once and hold the lock across the state check and the workaddIds,open,close, andreopenare now lock-free inner functions called while the caller already holds the locktry/finallyTests added
spec/util/Mutex.spec.tsacquire/releaseordering, double-release no-op, stale handle safety,wait()when idle, concurrentwait()callsspec/client/watches/ApiSubject.spec.tsaddcalls, sequential ordering of concurrentadd+remove, mutex released after failingopen/add/removenetwork callsspec/client/watches/BatchSubject.spec.tsdisplay,pollRate,refresh,on,off,getdelegation; error propagation to all coalesced callers; subsequent ops continue after a failed op