Skip to content

Rule-check log rotation can lose or drop log lines when two processes rotate at once #705

Backlog

Description

@davesheffer

Priority: low

Problem

Rotation of rule-checks.jsonl is a check-then-act sequence with no lock.

Where

src/core/lifecycle.ts:124-133 (appendRuleLog). The code stats the file, then unlinks .1, then renames the log to .1.

Failure scenario

Hooks run in parallel. Two processes both see size >= maxBytes. Process A unlinks the old .1 and renames the full log to .1. Process B, which already passed its size check, then unlinks A's fresh .1 (the full log, now lost) and tries to rename the new, nearly empty log. Or the rename throws ENOENT and B drops its entry through the outer catch. Result: up to one full 4 MiB of rule-check history is lost, or a line is skipped. This is a log only, and the function documents "never throws", so the impact is low.

Suggested fix

  • Rename over .1 directly. renameSync replaces the target, so the unlinkSync is unnecessary and the window shrinks.
  • Treat ENOENT on the rename as "someone else rotated" and carry on to the append.
  • Optionally re-stat after the rename.

Found while reviewing #701.

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions