Skip to content

fix(daemon): status publication truncates the live file in place #834

Closed

Description

@PierrunoYT

Version / branch / commit

main at 097c2652cba22d929c598347aacc6f95591c9031

OS and environment

Static persistence review; applies on all supported platforms.

Steps to reproduce

  1. Run a daemon status reader concurrently with daemon startup/status publication.
  2. Arrange for the reader to open the status file after truncation but before os.WriteFile completes, or interrupt the process after truncation.
  3. Observe empty/partial JSON, or loss of the previous valid status file.

Relevant code: internal/daemon/server.go:200-214.

writeStatusFile calls os.WriteFile directly on the final path. os.WriteFile truncates the existing file before writing the replacement.

Expected behavior

Readers should observe either the previous complete status or the new complete status. A failed/interrupted update should not destroy the previous valid document.

Actual behavior

Concurrent readers can observe invalid JSON, and interruption after truncation can leave the status file empty or partial.

Suggested fix and regression test

Write to a unique same-directory temporary file, sync/close it, atomically rename it over the final path, and sync the parent directory where supported.

Add a test with a coordinated reader during publication and fault injection before rename; assert readers never observe partial JSON and the old document survives failed publication.

Reported by PierrunoYT from a full codebase audit.

Activity

  1. Vasanthdev2004 commented on Aug 5, 2026

    @Vasanthdev2004
    Collaborator

    Confirmed on current main. writeStatusFile still calls os.WriteFile directly against s.opts.Paths.Status (internal/daemon/server.go:211), so the live file is truncated before the replacement lands and a reader in that window sees empty or partial JSON. An interrupt in the same window loses the previous valid status entirely.

    Sorry for the wait. We want the fix, and it should be the temp-file-plus-rename pattern already used elsewhere in the tree (atomicWriteFile in internal/daemon/remote/sessionlink.go:78 is the shape). Send the PR.

  2. added
    bug
    on Aug 5, 2026
  3. Vasanthdev2004 commented on Aug 12, 2026

    @Vasanthdev2004
    Collaborator

    Approved. Verified at internal/daemon/server.go:211 — the status file is written with os.WriteFile, which opens O_TRUNC on the live path. A reader arriving mid-write sees empty or partial JSON, and an interrupted process leaves no previous valid status behind.

    The fix is the usual one: write a sibling temp file and rename over the target, so readers only ever observe a complete document.

  4. added
    issue-approved
    on Aug 12, 2026
  5. self-assigned this
    on Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugissue-approved

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions