Skip to content

run-switch.sh: concurrent switches clobber the shared ~/switch-current.logpath pointer (wrong-log exit attribution) #3753

Closed

Description

@dezren39

Problem

~/run-switch.sh records its log location in a single shared pointer file, ~/switch-current.logpath. When two switches run concurrently, the second invocation overwrites the pointer, and the first operator's monitoring reads the WRONG run's log — exit-code attribution for a system switch then depends on each operator happening to track their own log filename privately.

Evidence (2026-11-01)

During the flake-dedupe activation switch (generation system-524, SWITCH_EXIT=0, verified from the operator's own log file), another actor launched ~/run-switch.sh twice from a separate worktree effort (08:59:29 — failed instantly, no sudo password; 08:59:51 — ran to completion on a newer main). The second launch clobbered ~/switch-current.logpath mid-flight; the first operator's final tail read the other run's log. Both switches built from the same main lineage so the system stayed consistent — this time. A wrong-log read on a FAILED switch could equally mask a failure as a success.

Ask

  • Per-invocation logpath: write the pointer to a unique file per run (e.g. ~/switch-<timestamp>-<pid>.logpath, or print the log path on stdout only and drop the shared pointer), and/or
  • A switch lock (flock on a lockfile) so a second concurrent switch refuses with a clear message instead of racing — two NixOS switches racing is wasteful even when harmless.

Either change is small; the lock is the stronger guarantee.

Acceptance criteria

  • Two concurrent run-switch.sh invocations cannot clobber each other's log attribution: each run's log path is unique to the invocation, or a flock-style lock makes a second concurrent invocation refuse with a clear message.
  • A failed switch is never read as a success (or vice versa) because of another run's log: the recorded log path points at the operator's own run from start to finish.
  • Single-invocation behaviour is unchanged apart from the pointer/lock mechanics.

Activity

  1. developing-today commented on Nov 1, 2026

    @developing-today
    Contributor
  2. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    priority/normalOrdinary priority (the default)
    effort/sSmall: expected under 2 hours of work
    area/infraInfrastructure, deployment, and operations
    meta/enrichedIntake review done: acceptance criteria, links, and normalized labels added
    on Nov 1, 2026
  3. dezren39 commented on Nov 1, 2026

    @dezren39
    MemberAuthor

    Intake review

    Fleet monitor intake pass (protocol §5), 2026-11-01.

    Related: None found in a merged-PR search for this issue (2026-11-01).

    Notes: Filed with no labels and no acceptance criteria; the problem statement and 2026-11-01 evidence (generation system-524 switch, two clobbering launches at 08:59) are clear and reproduced in the body.

    Risks: Low: ~/run-switch.sh is an operator script, not shipped code; the risk being fixed (wrong-log exit attribution masking a failed switch) is the larger one.

    Suggested approach: Prefer the flock lock (the stronger guarantee the issue itself names), plus a per-invocation logpath as belt-and-braces.

  4. added
    source/creatorFiled by the new-issue-creator job
    task/triageQueue packet kind: verify, reproduce, and route this item
    on Nov 3, 2026
  5. developing-today commented on Nov 3, 2026

    @developing-today
    Contributor

    agent-note:v1
    state: triaged (coverage pass)
    Coverage pass 2026-11-03: reviewed in the fleet-wide per-issue pass (drift / overlap / already-handled / stale / blockers / ordering). Verdict: still live; queued in repo-health-code-pilot as triage at priority 40 (priority/normal). If this is already handled at current main, the triage closes it with evidence; if it needs splitting, the triage files the split.
    Found via: coverage pass 2026-11-03

  6. dezren39 commented on Nov 3, 2026

    @dezren39
    MemberAuthor

    Triage (task 8266, lane code-e): STILL LIVE — verified against the actual script on the laptop, 2026-11-03.

    • The script named in this issue is the laptop operator script ~/run-switch.sh (not versioned in any repo). Read verbatim today, it is exactly the reported shape:
      • LOG=/home/user/switch-$(date +%Y%m%d-%H%M%S).log
      • echo "$LOG" > /home/user/switch-current.logpath — one shared pointer, overwritten unconditionally at start.
      • No flock / lock of any kind; the rebuild then runs and appends SWITCH_EXIT=$? to $LOG.
    • Extra finding beyond the issue body: the log filename itself has only second resolution, so two runs started in the same second share not just the pointer but the same log file — both runs' output and SWITCH_EXIT lines interleave in one file.
    • Dedup re-verified fresh: no PR in developing-today/code or dezren39/nix references run-switch.sh: concurrent switches clobber the shared ~/switch-current.logpath pointer (wrong-log exit attribution) #3753 or run-switch, and neither repo contains switch-current.logpath or a run-switch script (gh code search, 2026-11-03). Nothing has landed for this.

    Fix path scoped (enqueued as follow-up fix task in repo-health-code-pilot):

    1. Serialize switches: flock -n on a lockfile (e.g. ~/switch.lock); a second concurrent invocation refuses immediately with a clear message naming the holder's log.
    2. Per-invocation attribution: log name gains the PID (switch-<stamp>-$$.log) and the shared pointer is either dropped in favour of the per-run path printed on stdout, or kept only as a convenience "most recent start" record that no operator flow depends on for exit attribution.
    3. Version the script: the canonical copy should live in a repo (this issue's repo, developing-today/code, is the natural home — e.g. under infra/ alongside the fleet tooling) with the laptop ~/run-switch.sh deployed from it, so the next change is a PR, not an unversioned home-dir edit.
    4. Regression harness: stub two concurrent invocations; the second refuses, the first's log + SWITCH_EXIT line are untouched.

    No blockers; nothing needs Drew. Single-invocation behaviour otherwise unchanged.

  7. 14 remaining items

  8. dezren39 commented on Nov 8, 2026

    @dezren39
    MemberAuthor

    Partial verification — staying open (2026-11-07 done-task-only audit, tracking: #7016).

    Queue task(s) [8266, 8705] were marked done, and some work did land:

    • Direct commits referencing this issue: da477e4

    No merged change closes this issue's full scope, so the done status is not treated as completion. Remainder re-queued as triage task 11673.

  9. dezren39 commented on Nov 8, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · handed off

  10. added
    queue/claimedQueue worker lifecycle state
    work/assignedQueue worker lifecycle state
    and removed
    status/triagedTriaged and routed (see agent note)
    on Nov 8, 2026
  11. dezren39 commented on Nov 8, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · picked up

  12. dezren39 commented on Nov 8, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done

    Outcome — fixed and merged (task 12629, 2026-11-08)

    • PR fix: serialize run-switch and give each invocation its own log #5146 merged: merge commit b5ddb3974454cde92b0afa0ee52f98baa42a7959; its Closes #3753 closed this issue. The PR had no labels (no held / user/owned / do-not-merge / do-not-auto-edit), no reviews and no Codex findings to disposition.
    • Fresh verification on the laptop before merging (worktree at PR head da477e46238f): bash lib/run-switch.test.sh — all tests passed (concurrent invocation refused without creating or contaminating a log; single invocation prints a unique log path and preserves the rebuild exit status); bash -n pass; shellcheck pass. The canonical script preserves the old operator semantics (same flake /home/user/code#amd, same sudo -S nixos-rebuild switch, per-run SWITCH_EXIT) and changes only the pointer/lock mechanics: non-blocking flock, per-invocation UTC-timestamp+PID log path printed on stdout, and no shared ~/switch-current.logpath at all.
    • Deployed: laptop ~/git/code fast-forwarded to the merge commit and lib/run-switch.sh installed to ~/run-switch.sh; sha256 of both files is 11cc1019d0986510fa2c98a7b0cae41c8048f0647b97023f160aad74ec49ed9a (match verified). The previous script is kept at ~/run-switch.sh.pre-5146.bak. This closes the deployment/checksum remainder task 8705 left open.
    • Acceptance: two concurrent invocations can no longer clobber each other's log attribution (the second is refused with the active log named), and a failed switch's SWITCH_EXIT is recorded only in its own run's log.
  13. dezren39 commented on Nov 8, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done (already complete at pickup)

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

Metadata

Metadata

Assignees

Labels

area/infraInfrastructure, deployment, and operationseffort/sSmall: expected under 2 hours of workkind/bugSomething isn't working (prefixed form of the classic bug label)meta/enrichedIntake review done: acceptance criteria, links, and normalized labels addedpriority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobtask/triageQueue packet kind: verify, reproduce, and route this item

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions