State across an await, promises that never settle, resources not released on every way out #127
Loading…
Reference in a new issue
No description provided.
Delete branch "flow-analyses"
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?
Based on
pass-speed(#126), and contains it: the diff shown is this piece alone.The control-flow layer was built so that three questions could be asked properly. Each of these passed
tscstrict,safe-ts check --deny-warningsand oxlint:Three rules
stale-across-await(warning). JavaScript has one thread, so nothing else runs between two suspension points; across one, anything can. The rule reports a write to shared state (a field ofthis, module state, a local captured by a callback that may run more than once) on a path where the same place was read before the lastawaitand not again, and where either the write is made from that read (a lost update, also a position:this.items[at] = value) or the read was the test of a guard that exits (check, await, act). Fine: reading again after theawait, a flag or an in-flight promise set before the firstawait, a write of the call's own answer ("latest run wins"). AMutparameter is not shared state: the loan is exclusive while the call runs, and two loans at once arealiased-mut's. Quick fix: Await first.promise-never-settles(error). On every path through anew Promiseexecutor,resolveorrejectis called, or handed to something that will call it (a timer, a listener, a function, a field), or the executor throws. A callback that runs once is asked the same question; a named settle function counts where its name goes.new Promise(() => {})is taken as written on purpose.unreleased-resource(warning). Something acquired (a file handle, a stream reader's lock, an object URL, an observer, an interval, a socket) and not released on some way out of the function, including the way out where anawaitin between rejects. Anawaitis a way out only when the failure rules say the awaited call can fail, so awaiting a function that returns aResultdoes not count. A value that is returned, stored, passed on or captured by a closure has left the function's hands. What acquires and what releases is data in the packs (acquires), demonstrated on Node and in Chromium, Firefox and WebKit; a third-party pack can declare its own. The fix istry/finally, orusing/await usingwhere the value is disposable.What the layer gained
Exception edges as an option (from every call, or only from
await/yield), so an analysis says what can throw;q(...)as an early return that runs the openfinallyblocks; an identity for member paths (this.a.b); "this callback runs once later, repeatedly, or unknown"; two hooks, at a suspension and at the origin of an exception edge. The existing analyses keep the default model. Each extension has rows in the layer's path table.Evidence, on an adopter's project
stale-across-awaitstoppingflag set after theawait, so twostop()calls both shut down; a save that can delete a world loaded again in the meantime. 1 harmless: a fake clock in testspromise-never-settlesdone/finish, which the rule now followsunreleased-resourceawaitrejects, an interval keeps the test runner hanging instead of failing. 3 harmless: awaits of promises the checker cannot look upThe
unreleased-resourcenumbers are from a mirror of that project wired to this branch's packs: its installed packs predateacquires, so on its own install the rule is silent until it upgrades them. Nothing else changes there, and the three example projects are unchanged.Review
Two defects came out of my held-out cases and are fixed. A handle closed by its own
finallyinside anothertrywas reported, which is the form the rule's own help recommends: an exception from the inner block reached the outer handler without running the innerfinally. And a rejecting call whose receiver the packs cannot settle (stream.getReader().read()) was treated as one that cannot fail; unknown now counts as a possible failure.Soundness corpus: four classes added and closed (a lost update, check-then-act, a promise that never settles, a resource not released). The harness learned two oracles for them:
run()never settling, and resources still held after it returns. 2 of 62 classes escape.Cost: graphs are built only where there is something to ask: 11, 46 and 5 on that project's largest entry for the three rules. Per-file analysis there: 168 ms before, 170 ms after; the whole-program pass is unchanged.
What stays unseen
stale-across-await: who calls a function (one that is only ever awaited in sequence is still reported), local aliases of shared state, a lock class the project wrote itself.promise-never-settles: whoever receivesresolveis trusted to call it.unreleased-resource: a resource captured by a closure that never releases it; one reached through a field (this.timer = setInterval(...)); two closes in onefinallyare reported, because the second is skipped when the first rejects.Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
Open: nothing asked whether a new Promise executor settles the promise on every path; an if without an else, or a catch that only logs, left every caller awaiting forever, and every gate passed. Closes: an every-path question on the executor's graph: resolve or reject called, or handed on (an argument, stored, returned), or a function using one handed to a call. A callback the layer knows runs once (setTimeout, .then, once, { once: true }) is asked the same question; one that runs repeatedly counts when it uses a resolver; an unknown call is trusted. A path ending in a throw rejects. new Promise(() => {}) is on purpose. Co-Authored-By: Claude Opus 5.5 <[email protected]>Open: the corpus had no class for state read before an await and written after it, for a new Promise that never settles (the runner printed no outcome when the process ran out of work with run() pending, and the harness called that a mistake in the corpus), or for a resource still held after run() returns. Closes: four classes, each escaping every gate at the starting commit and rejected now: lost-update-across-await (three spellings), check-then-act-across-await (a cache, a stop flag), promise-never-settles (an if without else, an early return, a timer settling on one path), resource-not-released (an interval, a listening server). Two oracles: settles (the runner reports { pending: true } when the process exits with run() unsettled) and released (resources held after run() returned that weren't before, from process.getActiveResourcesInfo(); the runner then exits). Co-Authored-By: Claude Opus 5.5 <[email protected]>Open: setTimeout(() => { if (ok) resolve(); }, ms) was reported, the same callback declared as function onTimeout() and passed by name was not: the declaration itself counted as handing resolve on. Closes: a function of the executor bound to a name is judged at each use of the name: called now (it must settle on every path), handed to a call that runs it once (the same), handed on another way (trusted). Fixture rows for the by-name and arrow forms of all three rules. Co-Authored-By: Claude Opus 5.5 <[email protected]>Open: an executor that settles through a named function its callbacks call (const done = (ok) => { clearTimeout(timer); resolve(ok); }, then setTimeout(() => done(false), ms)) was reported on an adopter three times: the callbacks use done, not resolve. Closes: the functions of an executor bound to a name settle for whatever uses them, asked in turn (taken to settle while asked, so a check that settles or runs itself again later counts), and passed down to the callbacks inside. Co-Authored-By: Claude Opus 5.5 <[email protected]>