Skip to content

defense: LockManager.signalOther should guard against signaling self/parent PID #637

Closed

Description

@admarble

Context

Spin-off from #633 / PR #634 — the spec's Phase 1 Option C row recommended a defense-in-depth self-PID guard in LockManager.signalOther but filed it as a separate follow-up to keep #633's fix scope-minimal (test-only).

The #633 flake was caused by a test fixture (pid: 9999, hostname: os.hostname()) that assumed PID 9999 was dead — on CI runners with process churn it can be alive, and signalOther would then SIGTERM whatever real process owned that PID, killing the spawned CLI's parent/sibling and producing result.status === null. PR #634 fixed the test by routing around process.kill via a sentinel cross-host hostname; this issue covers the production-code weakness that allowed the flake in the first place.

Proposal

In src/lib/locks/lock-manager.ts:287 (signalOther), add a self-PID / parent-PID guard at the top of the function:

signalOther(holder: LockFile, signal: NodeJS.Signals = "SIGTERM"): boolean {
  if (this.orchestratorMode) return false;
  if (holder.hostname !== this.hostname) return false;
  // Defense-in-depth: never signal ourselves or our parent. A malformed
  // or PID-recycled lock file should not be able to SIGTERM the manager.
  if (holder.pid === this.pid || holder.pid === process.ppid) return false;
  if (!this.isPidAlive(holder.pid)) return false;
  try {
    process.kill(holder.pid, signal);
    return true;
  } catch {
    return false;
  }
}

Why this is worth doing

  • Zero behavior change for valid input — a real lock file's pid should never be the current process's own PID or its parent's, by construction.
  • Narrowed failure mode — if a stale lock file's PID is reused by the OS (POSIX recycles PIDs aggressively under low pid_max), this prevents the manager from accidentally SIGTERMing itself or its shell parent.
  • Production users — the bug(test): locks-cli.integration.test.ts:201 flake — signal-other takeover returns null status #633 flake was a test artifact, but the underlying weakness (signalOther trusts the lock file's claimed PID unconditionally once same-host + alive checks pass) is real production code.

Known limitations

  • This guard does NOT cover the sibling-vitest-worker case (a separate PID from both this.pid and process.ppid). That is the harder bug, and it's the one fix(#633): eliminate locks-cli takeover test flake via cross-host sentinel #634 actually fixed by avoiding process.kill entirely in the integration test. So this is a narrowed guard, not a full fix.
  • Strict equality on process.ppid is the right call (don't try to walk up the process tree — flaky and platform-dependent).

Verification

  1. Unit test in src/lib/locks/lock-manager.test.ts: construct a LockManager, build a holder with pid: process.pid and same hostname, call signalOther(holder), assert it returns false without invoking the stubbed isPidAlive (use a spy or counter).
  2. Same for pid: process.ppid.
  3. Existing same-host-alive test should still pass unchanged.

Out of scope

References

Activity

  1. added
    enhancement
    on May 13, 2026
  2. admarble commented on May 13, 2026

    @admarble
    ContributorAuthor

    Complexity: Simple (3 ACs, 1 directory)

    AC Quality Check

    Issue body already includes a verification section with concrete test cases; no vague/unmeasurable language. AC formally extracted below.


    Scope Assessment

    Non-Goals: Walking process ancestry beyond process.ppid; process-group checks; refactoring the #634 test.

    Metric Value Status
    Feature count 1 (guard clause) ✅
    AC items 3 ✅
    Directory spread 1 (src/lib/locks/) ✅

    Verdict: ✅ SCOPE_OK — narrow defense-in-depth follow-up to #633/#634.


    Acceptance Criteria

    AC-1: signalOther returns false when holder.pid equals this.pid, without invoking isPidAlive

    Verification: Unit Test
    Scenario: Given a LockManager with pid: 1 and a spy isPidAlive → When signalOther({ pid: 1, hostname: <same-host>, ... }) is called → Then return value is false AND the isPidAlive spy is not invoked (call count = 0).
    Assumptions: Test seam pid constructor option already exists.

    AC-2: signalOther returns false when holder.pid equals process.ppid, without invoking isPidAlive

    Verification: Unit Test
    Scenario: Given a LockManager constructed with pid: 999 (any value different from process.ppid) and a spy isPidAlive → When signalOther({ pid: process.ppid, hostname: <same-host>, ... }) is called → Then return value is false AND the spy is not invoked.
    Assumptions: process.ppid is read at call time inside signalOther; we compare against the live process.ppid. Test does not stub process.ppid.

    AC-3: Existing same-host-alive signalOther test passes unchanged

    Verification: Unit Test
    Scenario: The current signalOther only signals same-host alive PIDs test in lock-manager.test.ts:294 continues to pass — its holder PID (9999) is neither this.pid (1) nor process.ppid, so the new guard does not engage.


    Implementation Plan

    Phase 1: Production code

    1. Edit src/lib/locks/lock-manager.ts:287 (signalOther). After the existing orchestratorMode and hostname checks, insert a self/parent PID guard before the isPidAlive probe:
      if (holder.pid === this.pid || holder.pid === process.ppid) return false;
      Order matters: guard goes between the hostname check (line 289) and the isPidAlive check (line 290). This ensures the spy assertion in AC-1/AC-2 holds.

    Phase 2: Tests

    1. Add two test cases to the existing LockManager — forceAcquire / signalOther describe block in src/lib/locks/lock-manager.test.ts (around line 333):
      • "signalOther refuses to signal the manager's own PID" — construct manager with pid: 1, build holder with pid: 1 same host, spy on isPidAlive, assert returns false and spy not called.
      • "signalOther refuses to signal the manager's parent PID" — construct manager with pid: 999 (≠ process.ppid), holder pid: process.ppid same host, same assertions.

    Phase 3: Verify

    1. Run npx vitest run src/lib/locks/lock-manager.test.ts — all existing tests + 2 new must pass.
    2. Run npm run build — TypeScript clean.

    Design Review

    1. Where does this logic belong? LockManager.signalOther itself — single chokepoint for any process.kill call originating from the lock manager. No upstream caller (e.g. --force flow in bin/cli.ts) needs to know about this guard.

    2. What existing pattern does this follow? Defense-in-depth guards within signal-sending code paths. The function already short-circuits on orchestrator mode and cross-host; self/parent PID is the third short-circuit in the same shape.


    Open Questions

    None — the issue body provides a complete implementation and verification recipe.


    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled (single-file change, very small surface)
    Reasoning: No UI; defense-in-depth narrow-scope fix; verification is pure unit-test.


    Label Review

    Current: enhancement
    Recommended: enhancement (keep as-is)
    Reason: Adding simple-fix would be accurate but isn't required for routing.

  3. admarble commented on May 13, 2026

    @admarble
    ContributorAuthor

    QA Verdict: READY_FOR_MERGE

    Diff: 74 lines (5-line guard insert + 2 unit tests). Reviewed inline (under the 600-line worktree-agent threshold).

    AC Coverage

    • AC-1 — self-PID guard returns false without probing isPidAlive. Test asserts probed === 0.
    • AC-2 — parent-PID guard returns false without probing isPidAlive. Test constructs manager with pid: process.pid (POSIX guarantees process.pid !== process.ppid) and holder pid: process.ppid.
    • AC-3 — existing signalOther only signals same-host alive PIDs test (line 294) passes unchanged. Full suite: 45/45 ✅.

    Quality

    Adversarial pass

    • process.pid === process.ppid impossible on POSIX → AC-2 test is well-formed.
    • Existing test's holder.pid (9999) colliding with process.ppid in CI → vanishingly unlikely; if it ever happened the test would signal correct new behavior, not regression.
  4. admarble commented on May 13, 2026

    @admarble
    ContributorAuthor

    /fullsolve Complete

    Issue: #637
    Status: READY_FOR_MERGE
    PR: #639

    Summary

    • AC: 3/3 met
    • Tests: 45/45 passing (43 existing + 2 new)
    • QA: All checks passed, inline review

    Iterations

    • Spec: 1/1
    • Exec: 1/1
    • QA: 1/2

    Ready for human review and merge.

  5. added a commit that references this issue on May 14, 2026
  6. added a commit that references this issue on May 14, 2026
  7. admarble commented on May 14, 2026

    @admarble
    ContributorAuthor

    Documentation Updated

    This was an internal hardening fix with one user-visible change (more specific --force --signal-other log lines). Existing feature doc already covered the command, so updated in-place rather than generating a new template.

    File: docs/features/concurrency-locks.md (--force --signal-other section)

    Added

    • Table of the five log lines --signal-other can print, one per refusal/success branch
    • Note on the new self-or-parent guard's "Refused to signal" line being a heads-up for malformed/recycled-PID locks

    PR: #641

  8. added a commit that references this issue on May 14, 2026
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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions