uncaught-propagation: functions handed to the runtime as entry points #21

Merged
sionzee merged 2 commits from rules/entry-point-handlers into main 2026-11-04 21:28:53 +01:00
Owner

What

This extends uncaught-propagation rather than adding a rule. The existing design already has the right concept: callbacks that are "detached" from the code that registers them. It was missing two shapes.

What it already covered (checked before changing anything): every function written in place as an argument to a call that isn't an array method, unsafe, match or an IIFE. That includes setTimeout(() => ...), server.on("request", async (req) => ...), http.createServer((req, res) => ...) and addEventListener("message", (e) => ...).

What it missed:

  1. A function passed by name. Examples: setTimeout(tick), queueMicrotask(flush), process.nextTick(f), http.createServer(handle), server.on("request", handle), parentPort.on("message", onMessage), addEventListener("message", onMessage) and globalThis.setTimeout(f). handle was recorded as propagating, but since nobody calls it, nothing was reported.
  2. Event handler properties, such as worker.onmessage = onMessage and socket.onclose = () => { ... }. An arrow assigned to an on* property was "untracked", so it was neither propagated nor reported.

How:

  • CallSite gets a reference: bool. analyze.rs records one when a named function is passed to a known entry point. Since the rebase it is resolved the way main's call-following (#5) resolves calls: a function declared in the file or imported, a nested helper (also a local const onTick = () => ...), or a member (store.handle through a namespace import, an object literal's api.handle, a static Jobs.handle, this.handle). The entry points are timers (setTimeout, setInterval, setImmediate, queueMicrotask, requestAnimationFrame, requestIdleCallback, also on globalThis/window/self), process.nextTick, listeners (.on, .once, .addListener, .prependListener, .prependOnceListener, .addEventListener, and the global addEventListener), and createServer/createSecureServer. A reference is also recorded when such a function is assigned to an all-lowercase on* property (onmessage, not onChange, not once). Inline functions assigned to such properties are now detached callbacks.
  • project.rs reports a reference whose function can propagate as uncaught-propagation, with its own message ("handed to the runtime to call later") and the failure path. A reference is not counted as a call of its owner: it is kept out of fn_failure and propagation. It is also not judged by must-use-result, the cross-file unhandled-failure, or floating-rejection (an async handler is reported once, as uncaught-propagation).
  • There is no quick fix for references: the fix is a try inside the named function, and the page says so.

Kept narrow on purpose: only those named entry points are recognized. app.get("/", handle) and other framework routes are not judged, since Express, Fastify and similar frameworks catch what their handlers throw. A listener on the program's own EventEmitter is treated like other listeners, even though emit() is synchronous; that matches what the rule already did for inline listeners, and the page says so.

Docs: the uncaught-propagation page gets an entry-point section with a checked bad/good pair (a createServer(handle) server), a quick-fix note and an updated "can't see". There is one sentence in the async guide and the README row is updated. No new registrations: same rule name, same plugin entry.

Tests

  • New fixture fixtures/entry_points/ (two files, cross-file import). It has 11 reported cases: timers, nextTick, createServer, .on/.once, global addEventListener, globalThis.setTimeout, onmessage = f, an inline function assigned to onmessage, and a try around the registration (still reported). Accepted cases: a non-propagating function, a handler that catches inside, a camelCase hooks.onChange = f.
  • Rebased onto main at 1fe2d1d (head 5f39d94). That main includes #5 (call-following) and #11 (rejections vs throws, narrowing catches), both of which rewrote the code this PR touches. What changed:
    • CallSite keeps main's rejects/tries and this PR's reference. A reference site has no try that applies, and guarded, awaited and promise_handled are all false, so main's per-kind is_caught never counts it as caught.
    • Reporting in project.rs follows main's new split into caught and open kinds: a reference reports every open kind (thrown or rejected) as uncaught-propagation before main's floating/thrown split, so an async handler is still reported once and not as floating-rejection. fn_failure (now a loop over all failure sources) skips references. Main's propagation pass already skips detached sites, and references are marked detached.
    • The cross-file unhandled-failure check no longer has !call.guarded on main (it uses throw_caught/rejection_caught); it keeps !call.reference.
    • record_reference builds the callee with main's call_target and resolves local helpers through main's symbol_calls, instead of a bare name. This is the behaviour change in the "How" section above. The fixture has four new reported cases: a nested helper, handlers.route through a namespace import, an object literal method and a static method.
    • Docs: main's narrowing-catch example on the uncaught-propagation page is kept ahead of the entry-point section. The two "can't see" paragraphs are merged, and the old "class methods and nested helpers are not followed" (no longer true on main) is dropped.
  • After the rebase:
    • cargo nextest run --workspace: 104 passed, 0 failed.
    • npm run test:std: 130 passed, 0 failed.
    • npm run test:vite, npm run typecheck, npm run test:docs: passed.
    • safe-ts check on examples/demo (22 problems, the demo's intended ones, none of them "handed to the runtime") and examples/vue (none).
    • Checked by hand: setTimeout(onTick) with a local arrow and setTimeout(this.tick) inside a class are both reported now.

Left out / uncertain

  • A function passed by name is followed only as far as calls are (see the rule page): a parameter, a value read from a field, or a method on a receiver whose class isn't visible is not.
  • setTimeout(this.tick) is reported for what tick propagates; that this is unbound there is a separate problem the rule doesn't mention.
  • export default { fetch(request) { ... } } (Bun, Deno, Workers) and other object-method entry points are not covered. Like class methods, they stay untracked.
  • new Promise(executor), .then(fn) by name, and worker_threads new Worker(...) are unchanged.

🤖 Generated with Claude Code

## What This **extends `uncaught-propagation`** rather than adding a rule. The existing design already has the right concept: callbacks that are "detached" from the code that registers them. It was missing two shapes. What it already covered (checked before changing anything): every function **written in place** as an argument to a call that isn't an array method, `unsafe`, `match` or an IIFE. That includes `setTimeout(() => ...)`, `server.on("request", async (req) => ...)`, `http.createServer((req, res) => ...)` and `addEventListener("message", (e) => ...)`. What it missed: 1. **A function passed by name.** Examples: `setTimeout(tick)`, `queueMicrotask(flush)`, `process.nextTick(f)`, `http.createServer(handle)`, `server.on("request", handle)`, `parentPort.on("message", onMessage)`, `addEventListener("message", onMessage)` and `globalThis.setTimeout(f)`. `handle` was recorded as propagating, but since nobody *calls* it, nothing was reported. 2. **Event handler properties**, such as `worker.onmessage = onMessage` and `socket.onclose = () => { ... }`. An arrow assigned to an `on*` property was "untracked", so it was neither propagated nor reported. How: - `CallSite` gets a `reference: bool`. analyze.rs records one when a named function is passed to a known entry point. Since the rebase it is resolved the way main's call-following (#5) resolves calls: a function declared in the file or imported, a nested helper (also a local `const onTick = () => ...`), or a member (`store.handle` through a namespace import, an object literal's `api.handle`, a static `Jobs.handle`, `this.handle`). The entry points are timers (`setTimeout`, `setInterval`, `setImmediate`, `queueMicrotask`, `requestAnimationFrame`, `requestIdleCallback`, also on `globalThis`/`window`/`self`), `process.nextTick`, listeners (`.on`, `.once`, `.addListener`, `.prependListener`, `.prependOnceListener`, `.addEventListener`, and the global `addEventListener`), and `createServer`/`createSecureServer`. A reference is also recorded when such a function is assigned to an all-lowercase `on*` property (`onmessage`, not `onChange`, not `once`). Inline functions assigned to such properties are now detached callbacks. - project.rs reports a reference whose function can propagate as `uncaught-propagation`, with its own message ("handed to the runtime to call later") and the failure path. A reference is not counted as a call of its owner: it is kept out of `fn_failure` and propagation. It is also not judged by `must-use-result`, the cross-file `unhandled-failure`, or `floating-rejection` (an async handler is reported once, as `uncaught-propagation`). - There is no quick fix for references: the fix is a `try` inside the named function, and the page says so. **Kept narrow on purpose:** only those named entry points are recognized. `app.get("/", handle)` and other framework routes are not judged, since Express, Fastify and similar frameworks catch what their handlers throw. A listener on the program's own `EventEmitter` is treated like other listeners, even though `emit()` is synchronous; that matches what the rule already did for inline listeners, and the page says so. Docs: the `uncaught-propagation` page gets an entry-point section with a checked bad/good pair (a `createServer(handle)` server), a quick-fix note and an updated "can't see". There is one sentence in the async guide and the README row is updated. No new registrations: same rule name, same plugin entry. ## Tests - New fixture `fixtures/entry_points/` (two files, cross-file import). It has 11 reported cases: timers, nextTick, `createServer`, `.on`/`.once`, global `addEventListener`, `globalThis.setTimeout`, `onmessage = f`, an inline function assigned to `onmessage`, and a `try` around the registration (still reported). Accepted cases: a non-propagating function, a handler that catches inside, a camelCase `hooks.onChange = f`. - Rebased onto `main` at 1fe2d1d (head 5f39d94). That `main` includes #5 (call-following) and #11 (rejections vs throws, narrowing catches), both of which rewrote the code this PR touches. What changed: - `CallSite` keeps main's `rejects`/`tries` and this PR's `reference`. A reference site has no `try` that applies, and `guarded`, `awaited` and `promise_handled` are all false, so main's per-kind `is_caught` never counts it as caught. - Reporting in project.rs follows main's new split into caught and open kinds: a reference reports every open kind (thrown or rejected) as `uncaught-propagation` before main's floating/thrown split, so an async handler is still reported once and not as `floating-rejection`. `fn_failure` (now a loop over all failure sources) skips references. Main's propagation pass already skips `detached` sites, and references are marked detached. - The cross-file `unhandled-failure` check no longer has `!call.guarded` on main (it uses `throw_caught`/`rejection_caught`); it keeps `!call.reference`. - `record_reference` builds the callee with main's `call_target` and resolves local helpers through main's `symbol_calls`, instead of a bare name. This is the behaviour change in the "How" section above. The fixture has four new reported cases: a nested helper, `handlers.route` through a namespace import, an object literal method and a static method. - Docs: main's narrowing-catch example on the `uncaught-propagation` page is kept ahead of the entry-point section. The two "can't see" paragraphs are merged, and the old "class methods and nested helpers are not followed" (no longer true on main) is dropped. - After the rebase: - `cargo nextest run --workspace`: 104 passed, 0 failed. - `npm run test:std`: 130 passed, 0 failed. - `npm run test:vite`, `npm run typecheck`, `npm run test:docs`: passed. - `safe-ts check` on `examples/demo` (22 problems, the demo's intended ones, none of them "handed to the runtime") and `examples/vue` (none). - Checked by hand: `setTimeout(onTick)` with a local arrow and `setTimeout(this.tick)` inside a class are both reported now. ## Left out / uncertain - A function passed by name is followed only as far as calls are (see the rule page): a parameter, a value read from a field, or a method on a receiver whose class isn't visible is not. - `setTimeout(this.tick)` is reported for what `tick` propagates; that `this` is unbound there is a separate problem the rule doesn't mention. - `export default { fetch(request) { ... } }` (Bun, Deno, Workers) and other object-method entry points are not covered. Like class methods, they stay untracked. - `new Promise(executor)`, `.then(fn)` by name, and `worker_threads` `new Worker(...)` are unchanged. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sionzee force-pushed rules/entry-point-handlers from 235d7f4135 to 5f39d943a7
All checks were successful
Benchmarks / bench (pull_request) Successful in 6m52s
2026-11-03 23:30:21 +01:00
Compare
Merge main into rules/entry-point-handlers
Some checks failed
Benchmarks / bench (pull_request) Failing after 3m36s
fd9b3c906e
Resolve conflicts with the PRs merged before this one: both sides kept
(rule lists, std exports, package checks, CLI usage, README).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch rules/entry-point-handlers 2026-11-04 21:28:54 +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!21
No description provided.