fix: treat null signal as no cancellation under contention - #24
Merged
Conversation
Passing null as the AbortSignal under contention took the abortable queue path (signal !== undefined), so addEventListener threw after the waiter was enqueued. That left a zombie whose acquire() also threw on release, rejecting successful withLock work and letting later acquirers jump the FIFO queue. Treat null like undefined, and unlink the waiter if subscribe throws.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MTbj6CYXGsk6rhXx7J8SUP
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.
Bug and impact
Under contention,
lock(null)/withLock(fn, null)rejected withTypeErrorafter enqueueing a waiter. That zombie then made the holder'srelease()throw onnull.removeEventListener, so:withLockwork was reported as failed (throw infinally)isLocked()briefly looked freeRoot cause
The abortable path used
signal !== undefined, sonullentered abort setup.addEventListenerthrew afterenqueue, leaving a waiter whose patchedacquire()also assumed a real signal.Fix
nulllike "no signal" (signal != null)Validation
npm test: 20/20 passed at 100% coverageNote
Low Risk
Small, targeted change to optional AbortSignal handling in the lock implementation, with new regression tests and no API surface change.
Overview
Fixes a contention bug where
lock(null)/withLock(fn, null)incorrectly entered abort handling (signal !== undefined), which could enqueue a broken waiter, throw onaddEventListener, and later break the holder’srelease()(FIFO and error reporting).create.jsnow treatsnulllike “no signal” viasignal != nullfor abort wiring, and rejects non-AbortSignalvalues up front withTypeErrorbefore queueing or taking a slot.Regression coverage in
test/cancel.jsfornullunder contention,withLockholder release, and invalid signal arguments.Reviewed by Cursor Bugbot for commit 004bbae. Bugbot is set up for automated code reviews on this repo. Configure here.