Skip to content

Recover from a zeroed config.json instead of failing every start - #47

Merged
mastacontrola merged 1 commit into
mainfrom
fix/config-json-durable-write
Nov 13, 2026
Merged

mastacontrola merged 1 commit into
mainfrom
fix/config-json-durable-write

Conversation

@mastacontrola

Copy link
Copy Markdown
Member

Problem

A Windows host was powered off about 28 seconds after a self-update (0.1.19 to 0.1.20) rewrote config.json. The file came back as 651 zero bytes. Every service start failed with enroll: config.json: invalid character '\x00'. The MSI reinstall failed too (1722, then 1603), because its setup step reads the same file. The rollback left no service.

Cause

enroll.writeFile wrote a temp file and renamed it without a flush. NTFS committed the rename and the length before the data.

Fix

  • writeFile flushes before the rename, as update.writeFileSync already does. This also covers key.pem and cert.pem.
  • Load falls back to config.json.prev when config.json cannot be parsed. The service start, setup, renew and update paths log the recovery, keep the damaged file as config.json.damaged, and write config.json back. update repairs before update.Arm copies config.json over .prev.
  • With no usable .prev, the error says to delete config.json and run setup again with --server.
  • Design 0015's failure table gains the row.

Known limit: .prev is the copy from the last update. If the host enrolled again after that update, .prev can name an older host id. The log line says to check the server URL and host id.

Verification

  • New tests fail without the fix with the exact production error, and pass with it.
  • go vet (linux, windows, darwin) and go test ./... pass.
  • On the affected VM: the fixed MSI installed over the real zeroed file and exited 0. It logged the recovery, kept host 211 and its certificate, and polled. The server shows the host as ok.

🤖 Generated with Claude Code

A lab VM was powered off about 28 seconds after a self-update rewrote
config.json. writeFile renamed the temp file without flushing it, so NTFS
kept the new length and lost the data: config.json came back as 651 zero
bytes. Every service start then died on "invalid character '\x00'", and
the MSI's setup step hit the same error, so a reinstall failed with 1603
and rolled back to no service at all.

- writeFile flushes the temp file before the rename, as the update
  package's writeFileSync already did. This covers the key and
  certificate too.
- Load falls back to config.json.prev when config.json cannot be parsed.
  The service start, setup, renew and update paths log the recovery, keep
  the damaged file as config.json.damaged and write config.json back.
  update repairs before update.Arm copies config.json over .prev.
- With no usable .prev, the error says to delete config.json and run
  setup again with --server.

Proven on the VM: the fixed MSI installed over the zeroed file, logged
the recovery, kept host 211 and its certificate, and polled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mastacontrola
mastacontrola merged commit 176b007 into main Nov 13, 2026
2 checks passed
@mastacontrola
mastacontrola deleted the fix/config-json-durable-write branch November 13, 2026 12:31
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