After an await: a listener added too late, a superseded task that still writes; event rules find their receiver through every written form #133
Loading…
Reference in a new issue
No description provided.
Delete branch "after-await"
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
loans-as-facts(#132), and contains it: the diff shown is this piece alone.The study in #131 found that today's checker is silent on 72% of the bugs a reader could have seen in an adopter's code, and listed the kinds. This closes two of them. Both are one mistake seen from two sides: the world moved on during an
await, and the code after it acts as if it had not. Each of these passedtscstrict,safe-ts check --deny-warningsand oxlint:Two rules, both errors
late-listener. Some events happen at most once in an object's life, and a listener added afterwards waits for ever. The rule reports a listener for such an event added to an object the function already had before a suspension on that path (also in a callback that runs later: a timer, another listener, a.then), unless the state that says "it already happened" is tested on the way or right after.ws.onopen = fand the other handler properties count as listeners.Which events those are is data in the packs (
events.once), each with the test that says it has happened (signal.aborted,stream.closed,document.readyState === "complete"): 15 entries for Node (abort; a stream'sclose/end/finish; a child process'sexit/close/spawn; a worker'sexit/online; ...), 9 for the browser (abort; a WebSocket'sopen/close;load,DOMContentLoaded; IndexedDB's request and transaction events). Each entry is demonstrated by a test that makes the event happen, on Node and in Chromium, Firefox and WebKit: it fires once, a listener added afterwards never runs, and the test is false before and true after. Left out, with the reason in the guide: a server'slisteningand a socket'sconnect(the object can do it again), a stream'serror(Node throws when nothing listens), image and XHR loads (they fire again).Silent: a listener added before the first
await; an object made after it; a state test (if (signal.aborted) return;,throwIfAborted()); an awaited call that was given the object and listens for the same event itself; a signal from a controller the function made and has not handed out.superseded-task-write. AuseTaskorlatestcallback that changes state outliving the run (a captured object, aref, aMutit lends) after anawait, on a path where its signal was not tested since and was not handed to the awaited call. Returning the value is fine: the task drops a superseded run's answer.unused-task-signalonce reported every callback that ignored its signal and was narrowed to those with something to pass it to; this is the case the narrowing lost, as a rule of its own because it is a run-time defect and that one is idiomatic.The event rules now find their receiver
Reviewing this piece showed a gap in #130:
unknown-eventknew the class of a listener's receiver only when it was a parameter or local typed with the class. Of ten ways to write aSocketreceiver it resolved one. The class now comes from the receiver resolution the call rules use: a loop variable, an element by index, a destructured parameter, a member of a parameter, a#field, a parameter property, aforEachcallback's parameter, aMap's entries,byId.get(id)?.on(...),net.Socketthrough a namespace, and a member of a platform object (controller.signal). All ten forms are reported now, by all three event rules. On an adopter, listener sites with a known class went from 82 to 103 of 254, with no new finding.Also fixed on the way:
unused-task-signalreported any task that callsmut(...), as ifmutcould take a signal.Evidence
await, a field, a child process, one branch only, a test made before theawaitinstead of after, a destructured parameter, an element taken by index, a timer) and every correct form is silent. The first round reported 10 of those 16 and one false positive (the function's own controller); both causes are fixed. Tasks: five bugs reported (a captured object, aref, a test before theawaitonly, a secondawaitthat takes no signal, afinallythat clears the newer run's flag), six correct forms silent.abortlistener on a lifetime signal added after an awaited start-up, so stopping during it leaves an observer running; and a request'sendlistener added inside the response'sfinishlistener. Its two earlier fixes of these kinds are reported before the fix and silent after. Nothing else changes.listener-after-awaitandsuperseded-task-applies-latecome off the list, each rejected by its rule alone. 18 of 98 escape.What turns red on upgrade
await, or test the state after it; in a task, return the value, testsignal.abortedafter theawait, or hand the signal to the awaited call.unknown-event.What stays unseen
ws) have no event data.store.put(...)), for handler properties.Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests and the merge) in Claude Code.