std: withTimeout resolves to Failed when work throws synchronously #13
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/with-timeout"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
withTimeout(packages/std/src/async.ts) is documented as never rejecting, but it calledwork(controller.signal)inside thenew Promiseexecutor. Ifworkthrew before returningits promise (a plain function such as
(s) => fetch(buildUrl(), { signal: s })wherebuildUrlthrows), the executor threw and the returned promise rejected. The timer and thecaller's abort listener were also left in place until the timeout fired.
Now the call is wrapped in
try/catch: a synchronous throw becomes{ tag: "Failed", error }and goes through the same
finishas every other outcome, so the timer is cleared and thelistener removed. The doc comment now says this case resolves to
Failed.Tests
packages/std/test/async.test.ts: "withTimeout: Failed, not a rejection, when thework throws before returning a promise". It checks the outcome, that
clearTimeoutran once,and that the abort listener was removed. It fails before the fix (the promise rejects) and
passes after.
node --test packages/std/test/async.test.ts(with the transform loader): 9/9.npm run test:std: 116/117. The one failure,every Node callable is mapped exactly once,also fails on
mainwith the local Node v26.13.0 and is unrelated.npm run typecheck: passes.safe-ts check packages/std/src/async.ts: no problems.cargo nextest run: 94/95. The one failure,fixtures_match_annotationson thefeaturesfixture (
createDecipheriv), also fails on unmodifiedmainand is unrelated.npm run test:docs,npm run test:vite: pass.🤖 Generated with Claude Code
`withTimeout` is documented as never rejecting, but `work` ran inside the Promise executor: a function that throws before returning its promise (e.g. `(s) => fetch(buildUrl(), { signal: s })` with `buildUrl` throwing) rejected the returned promise and left the timer and abort listener behind until the timeout fired. The throw is now caught and reported as `Failed`, through the same cleanup as every other outcome. Co-Authored-By: Claude Opus 5.5 <[email protected]>