ovis icon indicating copy to clipboard operation
ovis copied to clipboard

machines.json can be left as NUL bytes after a crash, which blocks startup and rolls back managed updates

Open mattmarcin opened this issue 1 month ago • 0 comments

What happened

To be clear up front: the crashes are not caused by Ovis or herdr. The PC had an unrelated driver or hardware problem that caused repeated bugchecks, which I am fixing separately. The bug is that Ovis does not recover from an unclean shutdown: one interrupted write leaves it unable to start, loses the saved remote PCs, and rolls back the installed version.

After an unexpected Windows shutdown (a bugcheck), ~/.config/herdr-web-ui/machines.json was the right length (353 bytes) but every byte was 0x00. On the next start the server exited in the MachineManager constructor, so Ovis never came up.

That led to three problems:

  1. Ovis does not start until the file is moved away by hand.
  2. Moving it away loses every manually added SSH PC, because the registrations live in that file. Their keys under ssh/ are left orphaned.
  3. The supervisor treats the failure as a bad release: it deletes current.json and falls back to the source checkout (server/supervisor.ts:194-201 in 0.4.18). The checkout then crashes on the same file. Once the file is gone, the install has dropped from 0.4.18 to the 0.4.12 plugin checkout, and a remote PC already on bridge v37 refuses to connect: "This PC uses a newer bridge (v37); this app requires v31".

Likely cause

  • MachineManager.persist() (server/machines.ts:418-426 in 0.4.18, 436-437 on main) does writeFileSync(tmp), then renameSync(tmp, machines.json), with no fsync. On NTFS, an unclean shutdown can keep the rename and the file size but not the data, which leaves a NUL-filled file.
  • The window is wide. Every successful remote refresh calls saveSoon(), so with a remote PC connected the file is rewritten on the 5 s poll and after remote pane events, even when nothing changed. In both cases here, the last write was 20 to 30 seconds before the crash.
  • writePrivateState (server/private-state.ts, #1568) already handles this, but #1568 left machine snapshots out as recomputable. The same file also holds the PC registrations, which cannot be recomputed.
  • On read, JSON.parse(readFileSync(this.statePath)) (machines.ts:229 in 0.4.18, 243 on main) is not caught, unlike the read of machine-settings.json just above it. So the "Invalid machines.json; registrations were preserved" path is never reached for unparsable bytes. herdr-profile-state.json is read the same way from the same constructor.

Suggested fix

  1. Write machines.json (and herdr-profile-state.json) with writePrivateState, and skip the write when the serialized data has not changed, so the fsync does not run every few seconds. Splitting the snapshot cache out of the registration file would also keep the data that cannot be recomputed out of the frequent writes.
  2. On a parse failure, rename the file to machines.json.corrupt-<timestamp> and start with no PCs (or recover from a .bak kept on each successful write), and show the problem in Settings instead of throwing from the constructor.
  3. Consider not falling back to the source checkout (and not deleting current.json) when the child fails because of the state directory rather than the release, since the checkout reads the same files.

Repro

Put a NUL-filled machines.json (for example 353 zero bytes) in the state directory and start the server: it exits at the line in the log below. In isolation, JSON.parse(readFileSync(p, "utf8")) on such a file under Bun 1.4.2 throws SyntaxError: JSON Parse error: Unrecognized token ' '.

How often

Sometimes (twice in three days, Oct 24 and Oct 26; both were crashes of the PC itself, not a normal restart)

Ovis version

0.4.18 (7ab4bde, managed update); after the failure it fell back to the 0.4.12 plugin checkout (1ed80e7)

How it runs

herdr plugin

Server PC

Windows 11 (10.0.26200) x64, Intel Core i9-14900K, NTFS, herdr 0.9.4-preview.2026-10-14, Bun 1.4.2

Device and browser

Not relevant: the server fails before any device connects

How the device reaches the server

tailscale serve

How it signs in

Tailscale login

Agent and PC

One remote SSH PC (macOS, bridge v37)

Screenshots or logs

Plugin server.log at startup (v0.4.18 release, then the fallback to the checkout):

SyntaxError: JSON Parse error: Unrecognized token ''
      at new MachineManager (...\release-NlplWr\server\machines.ts:229:35)
      at createStatusHub (...\server\core\status.ts:181:56)
      at createCore (...\server\core\server-core.ts:79:139)
      at createServer (...\server\index.ts:245:16)
Saved release failed; starting the source checkout.
error: The new bridge failed its startup health check.
SyntaxError: JSON Parse error: Unrecognized token ''
      at new MachineManager (...\checkout\server\machines.ts:180:35)
      at createServer (...\checkout\server\index.ts:1122:56)
start: the server stopped (exit 1) before it answered http://127.0.0.1:7317/api/health

mattmarcin avatar Oct 26 '26 23:10 mattmarcin