Skip to content

Server: a second receiver on the same state folder no longer deletes the first one's run.json - #270

Open
connorburgess wants to merge 3 commits into
spacesheep-dev:mainfrom
connorburgess:fix/run-json-second-receiver
Open

connorburgess wants to merge 3 commits into
spacesheep-dev:mainfrom
connorburgess:fix/run-json-second-receiver

Conversation

@connorburgess

@connorburgess connorburgess commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Updated against v0.8.19 / main e8bfe999 on 2026-12-05. Current validation and refresh details.

What

When two receivers share one state folder (the second started with another --port), stopping the second one no longer deletes run.json while the first still runs. The stopping receiver writes back the entry it replaced at listen, so service status and the desktop app (which reads run.json after probing the default port) still find the first receiver.

Why

server/app.ts supports two receivers on one state folder, and server/runfile.ts already says a second receiver "must not delete the first one's" file. But the second receiver overwrites run.json when it listens (server/cli.ts, writeRunFile), and on a clean stop removeRunFile deletes it because the file now names the second receiver's pid. Steps on main (1a475e8):

  1. node dist/cli.js --state S --port 47811 --quiet --demo (receiver A)
  2. node dist/cli.js --state S --port 47812 --quiet --demo (receiver B): S/run.json now names B
  3. SIGTERM to B: S/run.json is gone, while GET http://127.0.0.1:47811/api/ui/info still answers 200. sessionpipe-gui service status prints run.json none.

Fix

  • server/runfile.ts: new releaseRunFile(stateDir, previous). If run.json still names this process and the receiver it replaced (previous) is still alive, it writes that entry back atomically through writeRunFile. Otherwise it removes our own file, exactly as removeRunFile does. If the file names someone else, it leaves it alone. removeRunFile keeps its contract and return value for its other caller, the Windows service stop in server/service/index.ts.
  • server/cli.ts: at listen, read the live run.json (readRunFile) before overwriting it. The signal shutdown and the exit handler both call releaseRunFile. When exit runs after the shutdown, the file no longer names us, so it does nothing.
  • writeRunFile now removes its temporary file when the write or the rename fails, instead of leaving run.json.<pid>.tmp in the state folder.

What it does not cover:

  • Only one level is remembered. With three receivers A, B, C, if B stops while C holds the file, C's later stop removes the file even though A still runs.
  • Nothing locks run.json, on main or here. A receiver that starts in the instant another is stopping can still lose its entry, the same window removeRunFile's read-then-delete has on main.
  • If the first receiver's pid has been reused by another process, its old entry is written back. Readers already trust pid liveness for run.json.

Original proof (before the v0.8.18 refresh)

Two new lifecycle tests in server/runfile.test.ts (a second receiver stopped by SIGTERM/SIGINT gives run.json back to the first, still running), next to the existing SIGTERM/SIGINT/SIGHUP ones. Each starts two CLI receivers on one state folder, stops the second, and checks that run.json equals the first one's entry and that the first still answers. A control test stops the first one first; the second one's file stays and is removed when it stops. Unit tests cover releaseRunFile's branches (previous alive, previous gone, no previous, file not ours, write-back failing) and the temporary file on a failed rename.

The two lifecycle tests on main's code (test file only), output trimmed:

 ❯ server/runfile.test.ts (14 tests | 2 failed | 11 skipped)
     × a second receiver stopped by SIGTERM gives run.json back to the first, still running
     × a second receiver stopped by SIGINT gives run.json back to the first, still running
 FAIL  server/runfile.test.ts > the receiver's run.json lifecycle > a second receiver stopped by SIGTERM gives run.json back to the first, still running
AssertionError: expected null to deeply equal { port: 61293, pid: 19637, …(3) }
+ Received:
null
 ❯ server/runfile.test.ts:187:36
      Tests  2 failed | 1 passed | 11 skipped (14)

On this branch:

$ npx vitest run server/runfile.test.ts
 Test Files  1 passed (1)
      Tests  18 passed (18)
$ npx vitest run server/runfile.test.ts server/service
 Test Files  2 passed (2)
      Tests  88 passed (88)
$ npm run typecheck
> tsc -p tsconfig.json --noEmit && tsc -p web/tsconfig.json --noEmit
$ npm run lint
Checked 983 files. No fixes applied.   (existing warnings only, none in the changed files)
$ npm test
 Test Files  248 passed (248)
      Tests  3374 passed | 2 skipped (3376)
$ npm run build
✓ built

Run on macOS only. Not run on Windows.

Checklist

  • Every commit is signed off (git commit -s): the DCO is the only paperwork.
  • npm run typecheck && npm run lint && npm test pass; npm run test:e2e for a UI change. (No UI change.)
  • New source files start with // SPDX-License-Identifier: Apache-2.0. (No new files.)
  • No secret, real address, hostname or home path in code, fixtures or screenshots (screenshots come from --demo).
  • A change to shared/* names the wire-contract change in the description. (No shared/* change.)
  • A line under Unreleased in CHANGELOG.md if a person would notice.

…the first one's run.json

Symptom: with receiver A running, a second receiver B started with the same
--state and another --port, then stopped with Ctrl-C or SIGTERM, left no
run.json although A still answered on its port. `service status` then
printed "run.json none", and the desktop app, which reads run.json after
probing the default port, no longer found an A that was not on 7373.

Cause: B overwrote run.json when it started listening, and on a clean stop
removeRunFile deleted it because the file named B's pid. Nothing remembered
the entry B had replaced.

Fix: at listen the receiver reads the live run.json it is about to replace.
On a clean stop (signal or exit), if run.json still names it and the receiver
it replaced still runs, it writes that entry back atomically instead of
removing the file; otherwise it removes its own as before. removeRunFile is
unchanged for its other callers. writeRunFile no longer leaves its temporary
file behind when the write or the rename fails.

Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
@connorburgess

Copy link
Copy Markdown
Contributor Author

The second receiver run.json restoration fix still applies to SessionPipe GUI v0.8.18. Merged current main (d16a2c01) into this branch; the refreshed head is 53a46f71. The affected files have not changed upstream, and the original change is preserved.

Resolved the changelog conflict by retaining the new release sections and this fix under Unreleased.

Validation on this head, macOS / Node 22.23.2:

  • npm run typecheck, npm run lint, and npm run build passed. Lint has the same 135 warnings and one informational diagnostic as current main, with no errors.
  • npx vitest run --maxWorkers=2 server/runfile.test.ts: 18 tests passed.

GitHub CI run 47453784594 for head 53a46f71 is not green. Passed: typecheck · lint · unit · build, e2e (desktop 2/3), e2e (desktop 1/3), e2e (phone · duo), e2e (control channel). Failed: e2e (desktop 3/3), typecheck · lint · unit · build · e2e.

The desktop shard 3 failure is test/e2e/split.spec.ts:662, “covering the page locks both and

with zero background scrollbar and restores window.scrollY on close”: the restored scroll position differs by more than 2 px. Neither this PR nor its refresh changes that test or the split-panel UI. On unmodified main d16a2c01, npx playwright test test/e2e/split.spec.ts --project=desktop --grep "covering the page locks" --repeat-each=6 --workers=2 reproduced four first-attempt failures; all four passed on retry, with two other repetitions passing initially. This establishes baseline flakiness, but does not turn the failed PR run green.

A maintainer needs to rerun the failed job (and aggregate gate), or resolve the baseline flake, before merging. The job-rerun API rejected this account with HTTP 403: “Must have admin rights to Repository.”

Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
@connorburgess

Copy link
Copy Markdown
Contributor Author

Merged current main (e8bfe999, v0.8.19) into this branch; the head is now 0458d524. The merge needed one resolution, in CHANGELOG.md: this PR's Unreleased entry stays above the new 0.8.19 section. No source file of this PR changed upstream.

CI run 47497350179 for head 0458d524 passed every job: typecheck · lint · unit · build, e2e (desktop 1/3), e2e (desktop 2/3), e2e (desktop 3/3), e2e (phone · duo), e2e (control channel) and the aggregate typecheck · lint · unit · build · e2e gate.

The earlier run 47453784594 on 53a46f71 failed e2e (desktop 3/3) at split.spec.ts:662 ("restores window.scrollY on close", scroll offset 52–120 px against <= 2). That test also failed on main in run 46543174302 and in run 46536894782 on the unrelated branch fix/workflow-stale-claim-hijack. It passes in the new run. This PR changes only server/cli.ts and server/runfile.ts.

Locally on 0458d524 (macOS, Node 22.23.2): npm run typecheck, npm run lint (135 warnings, no errors, as on main) and npm run build passed; npx vitest run server/runfile.test.ts: 18 tests passed.

This branch has not been deployed

No deployments
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