std: withTimeout resolves to Failed when work throws synchronously #13

Merged
sionzee merged 1 commit from fix/with-timeout into main 2026-11-03 21:38:02 +01:00
Owner

What

withTimeout (packages/std/src/async.ts) is documented as never rejecting, but it called
work(controller.signal) inside the new Promise executor. If work threw before returning
its promise (a plain function such as (s) => fetch(buildUrl(), { signal: s }) where
buildUrl throws), the executor threw and the returned promise rejected. The timer and the
caller'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 finish as every other outcome, so the timer is cleared and the
listener removed. The doc comment now says this case resolves to Failed.

Tests

  • New test in packages/std/test/async.test.ts: "withTimeout: Failed, not a rejection, when the
    work throws before returning a promise". It checks the outcome, that clearTimeout ran 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 main with 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_annotations on the features
    fixture (createDecipheriv), also fails on unmodified main and is unrelated.
  • npm run test:docs, npm run test:vite: pass.

🤖 Generated with Claude Code

## What `withTimeout` (`packages/std/src/async.ts`) is documented as never rejecting, but it called `work(controller.signal)` inside the `new Promise` executor. If `work` threw before returning its promise (a plain function such as `(s) => fetch(buildUrl(), { signal: s })` where `buildUrl` throws), the executor threw and the returned promise **rejected**. The timer and the caller'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 `finish` as every other outcome, so the timer is cleared and the listener removed. The doc comment now says this case resolves to `Failed`. ## Tests - New test in `packages/std/test/async.test.ts`: "withTimeout: Failed, not a rejection, when the work throws before returning a promise". It checks the outcome, that `clearTimeout` ran 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 `main` with 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_annotations` on the `features` fixture (`createDecipheriv`), also fails on unmodified `main` and is unrelated. - `npm run test:docs`, `npm run test:vite`: pass. 🤖 Generated with [Claude Code](https://claude.com/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]>
sionzee deleted branch fix/with-timeout 2026-11-03 21:38:02 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!13
No description provided.