Skip to content

fix(machine): recover orphaned jobs after a sidecar crash - #268

Merged
CameronBrooks11 merged 2 commits into
mainfrom
fix/orphaned-job-recovery
Jun 28, 2026
Merged

CameronBrooks11 merged 2 commits into
mainfrom
fix/orphaned-job-recovery

Conversation

@CameronBrooks11

Copy link
Copy Markdown
Member

Closes #200.

The machine-control sidecar owns the serial port. If it crashes mid-stream, the in-memory job is lost and the OS releases the port (DTR resets the controller). A re-attaching client then polled a job id the fresh process had never heard of and got a 404 — confusing, and the GUI client was wedged pointing at the dead port.

Backend — orphaned-job recovery

  • The machine service writes a small recovery snapshot for the active job, deleted on every clean terminal transition (complete/cancel/error) and on disconnect.
  • On startup, a snapshot still marked active means a prior process died mid-job → the job is reconstructed as orphaned, so GET /machine/jobs/{id} returns orphaned instead of 404.
  • The port is not blindly reopened (a blind re-open would DTR-reset a possibly-moving controller). Recovery is an explicit reconnect, which DTR-resets the board to a known state.
  • Snapshot lives in the temp dir (per-machine runtime state; a reboot that clears it also powers down the controller, so there's nothing to recover after one). The path is injectable for hermetic tests.

GUI — reach the respawned sidecar + surface the interruption

  • Bug fix: getApiClient() memoised the sidecar base URL for the app's lifetime, so after a crash + respawn on a new ephemeral port every later call hit the dead port forever (app restart required). The client now drops its memo on a fetch error (onError middleware), letting the next call re-resolve the respawned sidecar.
  • The streaming poll loop tolerates a few failed polls to ride through the respawn, then shows an interrupted-job notice with a reconnect prompt instead of a generic error; orphaned is handled as a terminal state that drops to disconnected.
  • Incidental: corrected two stale M0 sent/M108 sent pause/resume log strings left from the pre-0.9.0 streamer.

Verification

  • tests/api/test_machine_recovery.py (9 new): snapshot→orphaned, paused→orphaned, terminal/corrupt snapshots ignored, id-counter anti-reuse, and a persist-on-start/clear-on-complete lifecycle test via FakeTransport. Full backend suite 124 passed; mypy clean; ruff clean.
  • GUI: tsc clean, svelte-check 368/0, vitest 322 passed (+1 interrupted).
  • Real hardware (RAMPS test rig, 12V off): crash mid-stream → snapshot survives → fresh sidecar reports orphaned (not 404) → reconnect recovers the controller. All assertions passed.

Notes on the other acceptance points

The machine-control sidecar owns the serial port. If it crashes mid-stream the
in-memory job is lost and the OS releases the port (DTR resets the controller);
a re-attaching client then polled a job id the fresh process had never heard of
and got a 404.

Backend: the machine service writes a small recovery snapshot for the active
job (deleted on every clean terminal transition and on disconnect). On startup,
a snapshot still marked active means a prior process died mid-job, so the job is
reconstructed as `orphaned` — the port is NOT reopened (a blind re-open would
DTR-reset a possibly-moving controller); recovery is an explicit reconnect.
`GET /machine/jobs/{id}` then returns `orphaned` instead of 404.

GUI: the API client memoised the sidecar base URL for the app's lifetime, so
after a crash + respawn (on a new ephemeral port) every later call hit the dead
port forever. It now drops its memo on a fetch error, letting the next call
re-resolve the respawned sidecar. The streaming poll loop tolerates a few failed
polls to ride through the respawn, then surfaces an interrupted-job notice with
a reconnect prompt. Also corrects two stale "M0 sent"/"M108 sent" pause/resume
log strings left over from the pre-0.9.0 streamer.

Validated end to end on the RAMPS test rig: crash mid-stream -> snapshot
survives -> fresh sidecar reports `orphaned` (not 404) -> reconnect recovers the
controller.

Closes #200
@CameronBrooks11

Copy link
Copy Markdown
Member Author

Filed the streaming-guide rewrite as #269 (the broader marlin-streaming.md Technical Details drift referenced above).

Wrap the orphaned-job error string the way ruff format prefers; CI runs
ruff format --check, which the prior commit missed.
@CameronBrooks11
CameronBrooks11 merged commit 3ded795 into main Jun 28, 2026
21 checks passed
@CameronBrooks11
CameronBrooks11 deleted the fix/orphaned-job-recovery branch June 28, 2026 04:10
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.

build(gui): sidecar serial-crash / orphaned-job recovery

1 participant