Saving a remote connection falsely triggers the dirty guard #344

Closed
opened 2026-07-13 22:30:15 +02:00 by timothy · 3 comments
Owner

Found during the real-server integration validation in #333.

A successful Jellyfin connection save returns 200, but the SPA immediately raises the unsaved-changes confirmation and can block the post-save navigation.

Reproduction

  1. Open /app/libraries/jellyfin/connection on a connected source.
  2. Enter a valid API key and click Save connection.
  3. Observe PUT /api/v1/media-sources/jellyfin/connection return 200.
  4. The SPA immediately prompts: You have unsaved connection changes. Discard them?.
  5. Accepting proceeds to the connected source screen; dismissing restores/leaves the connection editor and no follow-up GET /api/v1/media-sources/jellyfin occurs.

This reproduced twice in #333: the initial valid re-save and the restoration after the required wrong-key check. There were no browser page errors.

Root cause

RemoteConnectionEditScreen calls setDraft(next), setSaved(next), setLoaded(...), and setSaving(false), then calls navigateToPath(...) in the same promise callback. The synthetic popstate runs before React has re-rendered the new baseline, so useDirtyGuard still reads the pre-save dirty ref and vetoes/prompts on a navigation initiated by the successful save itself.

Expected

A successful save navigates directly to the connected source screen without an unsaved-changes prompt.

Actual

The successful save triggers the dirty guard; dismissing the false prompt blocks the intended navigation.

Done-when

  • A regression test covers successful remote-connection save followed by navigation with no confirmation prompt.
  • A successful Jellyfin/Emby connection save navigates directly to the source screen.
  • Other save-then-navigate flows using useDirtyGuard are audited and any equivalent same-tick stale-ref cases are fixed or explicitly ruled out.
  • Relevant SPA tests, lint, typecheck, and build are green.
  • Adversarial review passed, or a documented exemption is recorded for a pure-SPA leaf fix with no server-state effect.

Linked validation: #333.

Found during the real-server integration validation in #333. A successful Jellyfin connection save returns 200, but the SPA immediately raises the unsaved-changes confirmation and can block the post-save navigation. ## Reproduction 1. Open `/app/libraries/jellyfin/connection` on a connected source. 2. Enter a valid API key and click **Save connection**. 3. Observe `PUT /api/v1/media-sources/jellyfin/connection` return 200. 4. The SPA immediately prompts: `You have unsaved connection changes. Discard them?`. 5. Accepting proceeds to the connected source screen; dismissing restores/leaves the connection editor and no follow-up `GET /api/v1/media-sources/jellyfin` occurs. This reproduced twice in #333: the initial valid re-save and the restoration after the required wrong-key check. There were no browser page errors. ## Root cause `RemoteConnectionEditScreen` calls `setDraft(next)`, `setSaved(next)`, `setLoaded(...)`, and `setSaving(false)`, then calls `navigateToPath(...)` in the same promise callback. The synthetic `popstate` runs before React has re-rendered the new baseline, so `useDirtyGuard` still reads the pre-save dirty ref and vetoes/prompts on a navigation initiated by the successful save itself. ## Expected A successful save navigates directly to the connected source screen without an unsaved-changes prompt. ## Actual The successful save triggers the dirty guard; dismissing the false prompt blocks the intended navigation. ## Done-when - [x] A regression test covers successful remote-connection save followed by navigation with no confirmation prompt. - [x] A successful Jellyfin/Emby connection save navigates directly to the source screen. - [x] Other save-then-navigate flows using `useDirtyGuard` are audited and any equivalent same-tick stale-ref cases are fixed or explicitly ruled out. - [x] Relevant SPA tests, lint, typecheck, and build are green. - [x] Adversarial review passed, or a documented exemption is recorded for a pure-SPA leaf fix with no server-state effect. Linked validation: #333.
timothy added the bugpriority: highfrontend labels 2026-07-13 22:30:15 +02:00
timothy added the in-progress label 2026-07-13 23:13:36 +02:00
Author
Owner

Claiming this queue item. I’ll reproduce the same-tick dirty-ref race, add a regression test, audit the other useDirtyGuard save-then-navigate flows, and implement the smallest SPA-only fix. I’ll also correct the queue selector wording exposed during pickup: reviewer-repo audits remain claimed by comment until explicitly released/abandoned, even while their issues stay open for implementer replies.

Claiming this queue item. I’ll reproduce the same-tick dirty-ref race, add a regression test, audit the other `useDirtyGuard` save-then-navigate flows, and implement the smallest SPA-only fix. I’ll also correct the queue selector wording exposed during pickup: reviewer-repo audits remain claimed by comment until explicitly released/abandoned, even while their issues stay open for implementer replies.
Author
Owner

Queue classification correction: all six numbered arc items are closed, but that is not a terminal “no pickup” state. The live protocol continues by enumerating OPEN milestone/review work, then applying priority: within that tier before unmilestoned backlog. On the corrected live pool, #245 is the first explicitly eligible child in the OPEN ChicoryTV modularization milestone and outranks unmilestoned #344/#345. Continuing #344 only because the user explicitly asked to finish it after the picker error was surfaced; #347 / PR #348 correct the selector rules.

Queue classification correction: all six numbered arc items are closed, but that is not a terminal “no pickup” state. The live protocol continues by enumerating OPEN milestone/review work, then applying `priority:` within that tier before unmilestoned backlog. On the corrected live pool, #245 is the first explicitly eligible child in the OPEN ChicoryTV modularization milestone and outranks unmilestoned #344/#345. Continuing #344 only because the user explicitly asked to finish it after the picker error was surfaced; #347 / PR #348 correct the selector rules.
Author
Owner

Done

What was done: Added a synchronous dirty-guard clean handoff for successful same-callback navigation; gated remote connection inputs for the full in-flight save; added Jellyfin/Emby screen regressions, a deferred-save lost-update regression, and a real App-owned route-transition regression.

Root cause: React had not committed the saved draft/baseline before navigateToPath synchronously dispatched popstate, so App read the prior dirty ref and treated a completed save as unsaved. The initial patch also allowed an older in-flight save to overwrite newer input; the reviewed follow-up freezes/gates the draft until completion.

Files changed: web/src/mediaSources/useDirtyGuard.ts, web/src/screens/RemoteConnectionEditScreen.tsx, web/src/screens/RemoteConnectionEditScreen.test.tsx, web/src/App.test.tsx, and docs/spa-conventions.md.

Verification: 118/118 focused tests; 83 files / 742 tests full web suite; lint, typecheck, production build, check:api, diff check, and pre-push hooks passed; .NET solution build succeeded with 0 warnings / 0 errors; all seven CI contexts green/skipped-as-designed at b9305fdee99d05209c1db6ebc48deb264e19680b. Cold exact-head re-review returned no findings and MERGEABLE. Live browser E2E on a fresh built host completed initial connect plus saved-connection edit, observed two 200 PUTs and readback GETs, mounted the source screen/unmounted the editor, and saw zero dialogs or browser errors.

Deferred: None.

Follow-up issues: None for #344. Queue-picker correctness is tracked separately in #347 / PR #348.

Docs updated: docs/spa-conventions.md now records the same-callback clean handoff and its in-flight request-ownership requirement.

## Done **What was done**: Added a synchronous dirty-guard clean handoff for successful same-callback navigation; gated remote connection inputs for the full in-flight save; added Jellyfin/Emby screen regressions, a deferred-save lost-update regression, and a real App-owned route-transition regression. **Root cause**: React had not committed the saved draft/baseline before `navigateToPath` synchronously dispatched `popstate`, so App read the prior dirty ref and treated a completed save as unsaved. The initial patch also allowed an older in-flight save to overwrite newer input; the reviewed follow-up freezes/gates the draft until completion. **Files changed**: `web/src/mediaSources/useDirtyGuard.ts`, `web/src/screens/RemoteConnectionEditScreen.tsx`, `web/src/screens/RemoteConnectionEditScreen.test.tsx`, `web/src/App.test.tsx`, and `docs/spa-conventions.md`. **Verification**: 118/118 focused tests; 83 files / 742 tests full web suite; lint, typecheck, production build, check:api, diff check, and pre-push hooks passed; .NET solution build succeeded with 0 warnings / 0 errors; all seven CI contexts green/skipped-as-designed at `b9305fdee99d05209c1db6ebc48deb264e19680b`. Cold exact-head re-review returned no findings and `MERGEABLE`. Live browser E2E on a fresh built host completed initial connect plus saved-connection edit, observed two 200 PUTs and readback GETs, mounted the source screen/unmounted the editor, and saw zero dialogs or browser errors. **Deferred**: None. **Follow-up issues**: None for #344. Queue-picker correctness is tracked separately in #347 / PR #348. **Docs updated**: `docs/spa-conventions.md` now records the same-callback clean handoff and its in-flight request-ownership requirement.
timothy removed the in-progress label 2026-07-14 08:02:51 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: timothy/ersatztv#344