Repository navigation
Fix package prompt cancellation and stale file-read isolation - #16
Open
UlfBissbort wants to merge 1 commit into
Open
UlfBissbort wants to merge 1 commit into
UlfBissbort wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two small lifecycle bugs in
web/package-dialog.mjs. Both have the same shape: async work that belongs to an old prompt affects a newer one.The fixes are two small guards (2 lines added, 3 lines changed). Four new regression tests fail on current
mainand pass with the fixes.Bug 1: a new request can join a prompt that is already cancelled
When the last requester of a prompt leaves,
leave()aborts the group's controller immediately. However, the group stays inthis.currentuntil the dialog's rejection reachessettle(), a few microtasks later. During that window,request()still treats the group as joinable, because it only skips settled groups:In the diagram below, the green box is the window in which B joins a group that is already cancelled:
sequenceDiagram participant A as Requester A participant P as PackagePrompts participant D as Dialog participant B as Requester B A->>P: request a.tar.zst P->>D: open prompt for group G A->>P: abort rect rgba(110, 200, 140, 0.25) Note over P: G is aborted but still current B->>P: request a.tar.zst Note over P: G is not settled, so B joins G end D-->>P: rejects with AbortError P-->>A: rejected P-->>B: rejected, B was never cancelledB is rejected with
AbortError, although nothing cancelled B. It should get a fresh prompt instead.How likely is this in practice? Probably rare today. The window only lasts a few microtasks, and package requests normally come from worker messages, which arrive as separate tasks. We did not find a current UI path that hits it, so this is best seen as a latent bug in the coordinator's contract. It would surface as soon as some caller re-requests a package in reaction to a cancellation.
The fix skips aborted groups when looking for a group to join:
B then creates its own group, which waits in the queue until the old one settles. We considered settling or removing the group directly in
leave()instead, but then B's prompt could open while the old dialog is still closing, with both using the same DOM elements. The guard preserves the existing rule that only one prompt is active at a time, and it does not change the queue order.Bug 2: a late file read can act on the replacement dialog
When the user picks a file,
openDialog()startsfile.arrayBuffer()and awaits it. When the read completes, it checks whether it should still act:The problem is that
dialog.opendescribes the shared<dialog>element, not this particular call toopenDialog(). If the user closes the dialog and another prompt opens before the read finishes,dialog.openistrueagain, so the old read acts on the new prompt (green box):sequenceDiagram participant U as User participant O as Prompt 1 participant S as Shared dialog element participant N as Prompt 2 U->>O: choose a large file O->>O: start file read U->>S: close dialog S-->>O: close event, Prompt 1 rejected N->>S: showModal, open is true again rect rgba(110, 200, 140, 0.25) O->>O: read completes Note over O: checks dialog.open, which is true O->>S: cleanup calls dialog.close end Note over N: Prompt 2 is closed by Prompt 1The
signal.abortedcheck does not help here: closing the dialog rejects the call, but it does not abort its signal. What happens to the new prompt depends on how the old read ends:cleanup()closes the new dialog.A user could hit this with two notebooks that need different packages: pick a large bundle for the first, close its dialog, and let the second notebook's prompt open before the read finishes. In a real browser,
dialog.close()fires acloseevent, so we expect the second prompt to reject itself and its run to fail as cancelled. Our tests confirm that the old read closes or modifies the new dialog; we have not yet confirmed the resultingcloseevent in a browser.The fix gives each
openDialog()call its ownfinishedflag, set by its owncleanup():arrayBuffer()cannot be cancelled, so the fix does not try to stop the read; it just makes a late result harmless. The attachment dialog already protects against the same situation (see the test "closing a pending attachment import releases the dialog and cannot affect a reopened notebook" intests/attachment-dialog.test.mjs).Tests
tests/workbench.test.mjscovers bug 1, with the first request aborted before the dialog opens, and after it opens. The second request is made before the old rejection is processed, and the test checks that it gets a new, non-aborted prompt and receives the selected bytes.tests/package-dialog.test.mjs(new) covers bug 2, with the old read succeeding, and failing. It calls the realpackageDialog.open()with a minimal fakedocumentand a file whose read the test resolves by hand.Results on
mainat8476cc5:node --test tests/*.test.mjs: 116 passed, 0 failed, 7 skipped (the local-server tests, which need the native binary).How we found these
We were trying out a model checker on Roclet (Zef-Spec, a tool we are developing, not public yet). The package prompts were a good target: a small state machine with async transitions, where the bugs come from timing rather than types.
Here is what happened, in order:
PackagePrompts, we suspected the window between abort and settle.The key modeling decision was to treat "abort", "dialog rejects" and "
settle()runs" as separate steps, and likewise "user closes the dialog", "replacement opens" and "old read completes". It is tempting to model each group of steps as a single atomic transition, but then both bugs disappear from the model.For the original logic, the checker returned these shortest counterexamples:
These models are deliberately tiny: two requesters and one URL for the queue, one old dialog and one replacement for the dialog. "Holds" means the property holds in that model, not that the whole package flow is proven correct. The JS tests are what connect the models to the actual code.
What the spec looks like
Here is the core of the queue model.
g0is the first prompt group;j0andj1are the two requesters. Theclosingphase means "aborted but not settled yet", which is exactly the window from bug 1.Each rule is an event with guards (when it may happen) and updates (what it changes).
rule,equal,one_ofandall_ofare small helpers in our script that build Zef-Spec nodes such asET.Rule,ET.Eq,ET.OrandET.And. The checker explores every order in which the events can happen, and reports the shortest sequence that breaks the invariant.If you would like to run the models yourself, I am, of course, happy to make Zef-Spec available. The tests in this PR do not need it.