Skip to content

Upload: retry finalize alone instead of re-running the flow - #237

Merged
neuromechanist merged 2 commits into
stagingfrom
fix/issue-201-finalize-retry
Aug 11, 2026
Merged

neuromechanist merged 2 commits into
stagingfrom
fix/issue-201-finalize-retry

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #201 — note: closing keywords only auto-close on merges into the default branch (main), and this PR targets staging, so the issue must be closed manually after merge, with a comment naming this PR and the merge commit.

Problem

In src/pages/upload.astro, the catch-all around the upload flow re-enabled the whole form after a finalizeDataset failure. Clicking Submit again re-ran the entire flow from createDraftDataset — creating a duplicate draft and re-uploading every byte, the opposite of what the error message tells the user to do. Since #198 gave finalize a 60s deadline, this path is reachable via a mere backend hang, not only a genuine failure.

Fix

  • After runUploadQueue completes, the page keeps the draft id in pendingFinalizeId before calling finalizeDataset.
  • On finalize failure (or timeout), the form stays locked (the files are already in storage), the Submit button becomes Retry finalizing, and the error copy says explicitly that retrying only re-runs the finalize step and nothing is re-uploaded.
  • Clicking Submit in that state calls finalizeDataset(draftId) directly — no createDraftDataset, no re-upload. A retry that fails again stays on the retry path, so it is repeatable.
  • The full create + upload run only happens when there is no uploaded draft waiting on finalize (pendingFinalizeId is null). "Drop something else" (resetPrecheck) clears the retry state and restores the original button label, which is the deliberate escape hatch back to the full flow.
  • The decision itself is a pure helper, resolveSubmitAction in src/lib/upload-client.ts, unit-tested in src/lib/upload-client.test.ts (null / id / repeated id / empty id).

Stale-finalize hardening (review follow-up)

The in-flight finalize window (up to 60s) previously left "Drop something else" clickable, and a reset during that window could race the pending promise into a duplicate draft or a stale /upload/success navigation. Three layers close this:

  • Both finalizeDataset call sites (initial and retry) now pass { signal: uploadAbort.signal }, so resetPrecheck()'s existing abort actually cancels a hanging finalize. Aborting finalize is safe — the route is idempotent and the draft stays visible on the dashboard.
  • The reset button is disabled for the whole in-flight submit/finalize window (symmetric with submit/name/description/dropzone) and re-enabled on every settle path except successful navigation. The progress label reads "Finalizing..." during the wait, so the 100%-but-waiting window is named.
  • A submitGeneration counter (bumped by resetPrecheck()) is captured at submit/retry start and re-checked after every await: a stale closure that outlives a reset bails out without navigating, without writing pendingFinalizeId, and without touching the new flow's UI — even if the abort somehow did not throw.

Why finalize-only retry is safe

The backend finalize route is explicitly idempotent — nemar-cli routes/datasets/upload.ts states "finalize is idempotent — can be called multiple times". Every byte is already in S3 when finalize runs, so repeating finalize against the same draft is safe and cheap, and needs no new backend support.

Verification

  • bun run typecheck — 0 errors, 0 warnings, 0 hints (216 files)
  • bun run lint — clean (212 files)
  • bun run test — 1037/1037 passed (53 files), including 4 new resolveSubmitAction tests

Manual QA (test.nemar.org, after this lands on staging)

  1. Sign in as [email protected] via the email-code form; the code is echoed as dev_code and prefilled on /login/verify, so no inbox is needed.
  2. On /upload, drop a tiny BIDS set (e.g. a dataset_description.json plus one sub-01 file) and submit.
  3. In devtools, switch the Network panel to Offline after the per-file rows all read "Done" but before finalize returns (or block the /api/v1/datasets/*/finalize URL), so finalize fails. While finalize is pending, the progress label reads "Finalizing..." and "Drop something else" is disabled.
  4. Confirm the button now reads "Retry finalizing" and the error text says nothing will be re-uploaded; the name/description/dropzone stay locked and "Drop something else" is enabled again.
  5. Go back online and click Retry. In the Network panel, verify the only request is POST /api/v1/datasets/<id>/finalize — no POST /api/v1/datasets (create) and no S3 PUTs — and that the page lands on /upload/success.
  6. Open /dashboard and confirm exactly one dataset was created, with no duplicate draft.
  7. Repeat step 3 twice in a row before going online, to confirm a second failed retry stays on the retry path.
  8. Reset while finalize is pending: with the network throttled so finalize hangs, confirm "Drop something else" is disabled during the wait; once the failure lands and the retry state is showing, click it. Verify the finalize request is aborted (devtools shows it cancelled), the form fully resets, and dropping folder B and submitting creates exactly one new dataset — no stale navigation to the old draft's /upload/success, no stale error over the new flow, and the old draft (still un-finalized) simply remains on the dashboard.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

@neuromechanist
neuromechanist merged commit f537c0f into staging Aug 11, 2026
5 checks passed
@neuromechanist
neuromechanist deleted the fix/issue-201-finalize-retry branch August 11, 2026 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant