Skip to content

fix: do not lose a sync fetch result when its signal is already aborted - #437

Open
waw666waw666 wants to merge 1 commit into
isaacs:mainfrom
waw666waw666:fix/pre-aborted-sync-fetch
Open

waw666waw666 wants to merge 1 commit into
isaacs:mainfrom
waw666waw666:fix/pre-aborted-sync-fetch

Conversation

@waw666waw666

Copy link
Copy Markdown

With ignoreFetchAbort and allowStaleOnFetchAbort, a pre-aborted
signal makes the abort handler rebind res to call cb directly.
A synchronous fetchMethod then reaches cb while
const p = new Promise(pcall) is still being constructed, so cb
reads p in its temporal dead zone. The Promise constructor swallows
that ReferenceError after the abort path already resolved, the value
is never written to the cache, and the key stays attached to the
settled background-fetch promise, so later fetch() calls, including
forceRefresh, keep returning undefined without calling fetchMethod
again.

Declare p before the callbacks, and if cb runs before p
exists, defer it a microtask, when p does exist. The asynchronous
path is unchanged: cb still runs synchronously once p exists,
and the first fetch still resolves to undefined, as it does for an
asynchronous fetchMethod.

Fixes #436.

Testing: test/fetch.test.ts 29/29; test/tracing.test.ts + test/fetch.test.ts 122/122; the only stable failure in the full suite on this machine is the pre-existing max above 2**16 timeout, which fails on the baseline too and is unrelated to this change.

With `ignoreFetchAbort` and `allowStaleOnFetchAbort`, a pre-aborted
signal makes the abort handler rebind `res` to call `cb` directly.
A synchronous fetchMethod then reaches `cb` while
`const p = new Promise(pcall)` is still being constructed, so `cb`
reads `p` in its temporal dead zone. The Promise constructor swallows
that ReferenceError after the abort path already resolved, the value
is never written to the cache, and the key stays attached to the
settled background-fetch promise, so later fetch() calls, including
forceRefresh, keep returning undefined without calling fetchMethod
again.

Declare `p` before the callbacks, and if `cb` runs before `p`
exists, defer it a microtask, when `p` does exist. The asynchronous
path is unchanged: `cb` still runs synchronously once `p` exists,
and the first fetch still resolves to undefined, as it does for an
asynchronous fetchMethod.

Fixes isaacs#436.

This branch has not been deployed

No deployments
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.

Pre-aborted synchronous fetch leaves a settled promise that blocks later fetches

1 participant