Skip to content

Use atomic write for state file to prevent corruption on crash#195

Open
AnyCPU wants to merge 1 commit intobasecamp:mainfrom
AnyCPU:feature/atomic_state_1
Open

Use atomic write for state file to prevent corruption on crash#195
AnyCPU wants to merge 1 commit intobasecamp:mainfrom
AnyCPU:feature/atomic_state_1

Conversation

@AnyCPU
Copy link
Contributor

@AnyCPU AnyCPU commented Feb 16, 2026

saveStateSnapshot() uses os.Create which truncates the state file to zero bytes before writing. If the process is killed (OOM, SIGKILL, power loss) between truncate and write completion, the state file is left empty or with partial JSON.

On restart, RestoreLastSavedState fails to decode the corrupted file and kamal-proxy starts with zero services — dropping all routing for every service behind the proxy.

Fix

Write to a temp file in the same directory, fsync, then atomically rename over the state file. This also fixes a missing f.Close() on the state file handle.

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