Skip to content

Migration-safe update: keep the health wait alive during migrations - #85

Merged
mistakeknot merged 6 commits into
mainfrom
aleph/migration-safe-update
Oct 27, 2026
Merged

mistakeknot merged 6 commits into
mainfrom
aleph/migration-safe-update

Conversation

@mistakeknot

@mistakeknot mistakeknot commented Oct 27, 2026 •

Copy link
Copy Markdown
Owner

AGENT GENERATED

Why

In the 0.8.0 install the launcher's 60 s health timeout expired while migrations on a 4.3 GB database took about 181-220 s. The launcher killed the server mid-migration and systemd (Restart=always) re-ran the migration. Tracked in bead mk-ooomf.1 (epic mk-ooomf).

What changed

  • @bb/config/migration-info (new): shared helpers for the <db>.migrating.lock / <db>.migrating.info markers (readActiveMigration, isProcessAlive, MIGRATION_WAIT_CAP_MS = 1 h).
  • migrate() holds the lock and info file through backup finalization (new phase finalizing). Before, the marker disappeared while a 5 GB backup was still being digested, which is what made the first rehearsal fail at 169 s. The info file is now unlinked on release.
  • Launcher: the health deadline slides (now + 60 s) while the migrating process is alive, capped at 1 h, and logs progress every 10 s. Stopping the server during a migration waits for it instead of killing after 5 s.
  • Stop safety (review fix): the server installs SIGTERM/SIGINT handlers before it opens the database (apps/server/src/startup-stop-guard.ts), so a stop during a migration lets it finish and exit cleanly. Previously systemd's SIGTERM killed it even with TimeoutStopSec=3900.
  • Marker ownership (review fix): the info file records the holder's process identity (boot id and start time from /proc on Linux). readActiveMigration ignores a marker whose pid is dead, whose identity changed (pid reuse), or that is older than the cap. The launcher also requires the marker pid to equal the server process it spawned.
  • Startup marker (second review fix): the guard publishes <db>.starting.<pid>.info (pid and process identity; each process writes and removes only its own file, and the launcher reads the file for the pid it spawned, so overlapping startups cannot clear one another) before it installs its handlers and removes it when startup settles, so a stop is visible to the launcher even before the migrating marker exists. The launcher waits for the cap when either marker is present, and rechecks at the 5 s mark so a marker that appeared after the signal still prevents SIGKILL. A server that publishes nothing is still killed at 5 s. The launcher now passes the database path on its health-failure termination path too.
  • Third review fix: per-pid startup markers (tests: two overlapping startups where the second settles and the first stays protected, at helper level and with a real child process plus the real guard). The launcher termination test uses fileURLToPath and is skipped on win32.
  • Config tests for pid reuse use a synthetic recorded identity, so they no longer depend on /proc; one Linux-only test checks the real identity reader.
  • install-machine.sh: generated unit gets TimeoutStopSec=3900.
  • FORK.md and CHANGELOG.md document the change, including the drop-in needed for already-installed units.

Verification

  • New and updated unit tests: marker helpers, lock release/finalization, health deadline including a 250 s virtual-clock migration against a real HTTP server, install script asserts TimeoutStopSec.
  • Rehearsal on a copy of the 5.3 GB pre-migration database, real launcher and real migrate() as a transient Restart=always systemd user unit with the 60 s timeout unchanged: migrate took 203.9 s, padded to 250.0 s, server healthy at 250.8 s, NRestarts=0. Control with the marker hidden failed at 60.09 s.
  • Review-fix tests: a real child process receives SIGTERM mid-"migration" and finishes it before exiting; PID-reuse, stale-age and pid-mismatch cases for the marker.
  • Second rehearsal (same setup): systemctl --user stop 65 s into the real migration. The server logged the deferred stop, finished migrating (203 s), exited 0, NRestarts=0, no leftover info file, and the next start had nothing to migrate (15 ms). The 250 s rehearsal re-passed with the new code (healthy at 251.8 s, NRestarts=0).
  • Round 2 tests: launcher termination with a real child process (marker published before the signal, marker published after the signal and before the 5 s mark, and no marker at all, which is still killed); the first two end with the work complete and exit 0. Disabling the recheck makes the second one fail. Guard tests for marker create/clear on settle and abandon; helper tests for dead, reused-identity, overdue and foreign-owned startup markers.
  • Third rehearsal (stop with the startup marker, fresh copy): marker present from start, systemctl --user stop 80 s into the migration was deferred, migration finished (250 s with padding), exit 0, NRestarts=0, no SIGKILL, startup marker removed (stop took 158 s).
  • Typecheck, lint, config, db and server startup suites pass; bb-app passes except the 4 failures below.

Not verified

  • bb-app test/index.test.ts has 4 config-command failures; they exercise code this diff does not touch, but I could not compare against a clean baseline (stash was blocked by the destructive-git guard).
  • One db test (migration-restore) timed out at 15 s under host load and passed on re-run alone.
  • The rehearsal harness lives outside the repo.
  • The patch ledger (paths.tsv) is not updated.
  • Process identity is Linux-only; other platforms rely on the launcher pid match and the age cap.
  • The stop rehearsal used a stand-in server entry that wraps the real migrate() and the real stop guard, not the full start-server.ts.
  • The launcher-level tests use a fixture process that mimics the guard's contract (defer SIGTERM, finish work, exit); the real guard is covered by its own tests and the rehearsal.
  • The unmarked window before the guard installs (Node bootstrap) is not protected: a signal there gets default handling.
  • The stop rehearsal was not re-run after the marker became per-pid (only the file name changed).
  • Cross-lab re-review of the fifth fix commit (a85977d) is pending. That commit removes the reader-side deletion of dead or overdue startup markers (a reused pid could lose a fresh marker; accumulation is documented in the fork notes as a known limitation), makes the stop-guard child helper reject on child error or early exit, and skips every test that sends POSIX signals on win32.

🤖 Generated with Claude Code

mistakeknot and others added 6 commits October 27, 2026 10:05
The 0.8.0 install lost its first start because the launcher's 60 s health
deadline expired while migrations ran for 181 to 220 s on a 4.3 GB database.
The launcher killed the server mid-migration and systemd restarted it to
repeat the work.

- The health deadline slides while the migrating lock's info file names a
  live process, so it expires 60 s after the migration ends, never during it.
  Progress is logged every 10 s and the wait is capped at one hour.
- migrate() holds the lock and info file through backup finalization
  (phase finalizing), which otherwise ran unmarked for over a minute.
- The lock removes its info file on release; the helpers moved to
  @bb/config/migration-info so bb-app can read them.
- Stopping during a migration waits for it instead of killing after 5 s.
- The generated systemd unit sets TimeoutStopSec=3900.

Tracked in bead mk-ooomf.1.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
A SIGTERM during a migration killed it even with a long TimeoutStopSec,
because systemd signals the whole control group and the server only
installed its handlers after the database opened. The server now records
SIGTERM/SIGINT before it opens the database, finishes the migration, and
exits cleanly before listening.

A migration marker left by a crashed process could look active when its pid
was reused, extending the health wait for up to an hour. The marker now
records the holder's process identity (boot id plus start time on Linux);
readActiveMigration rejects dead pids, changed identities and markers older
than the cap, and the launcher requires the marker pid to equal the server
process it spawned.

Bead mk-ooomf.1.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The server now publishes a startup marker before it installs its stop
guard, so the launcher can see a deferred stop even when SIGTERM lands
before the migrating marker is written. The launcher also rechecks the
database state at the 5 s mark and waits instead of escalating to
SIGKILL when a marker appeared after it signalled. Marker liveness
checks are shared between migration and startup markers.

The pid-reuse tests in the config package now use a synthetic recorded
identity so they pass without /proc.

Bead: mk-ooomf.1

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Startup markers were one shared file per database, so a second startup
could overwrite or remove the first's marker and leave the launcher
without protection at its 5 s recheck. Each process now writes and
removes only <db>.starting.<pid>.info, and the launcher reads the file
for the pid it spawned. The launcher termination test uses
fileURLToPath and is skipped on win32, where SIGTERM is a hard kill.

Bead: mk-ooomf.1

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The overlap test now resolves the child script and cwd with fileURLToPath,
is skipped on win32 (POSIX signals), and rejects its startup wait when the
child errors or exits early. readActiveStartup deletes a per-pid startup
marker whose owner is dead or overdue; a live owner's marker is kept.

Bead: mk-ooomf.1

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Reverts the reader-side deletion in readActiveStartup: unlinking after a
read could remove a fresh marker published by a reused pid. Dead or overdue
markers are ignored as before, and the possible accumulation is documented in
FORK.md as a known non-blocking limitation.

runStoppedChild now rejects when the child errors or exits before "started",
and both tests that send POSIX signals are skipped on win32.

Bead: mk-ooomf.1

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@mistakeknot
mistakeknot merged commit 701b154 into main Oct 27, 2026
2 checks passed
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