Skip to content

fix(openchamber): single-instance guard for the socket-activated server - #2465

Merged
dezren39 merged 2 commits into
mainfrom
queue/3677-1117-openchamber-single-instance
Oct 30, 2026
Merged

dezren39 merged 2 commits into
mainfrom
queue/3677-1117-openchamber-single-instance

Conversation

@dezren39

@dezren39 dezren39 commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Problem

Tracking issue: dezren39/darwin-nix#1117 (cross-repo set — the defect was filed there; the systemd model it names lives in this repo, home/common/default.nix. The launchd port in dezren39/nix disables self-handoff for the same failure class.)

The openchamber user unit is socket-activated (openchamber.socket owns 127.0.0.1:3000) with KillMode=process, so a stop terminates only the tracked main process. On 2026-10-29 the 12:36:40 CDT stop left the actual web server (pid 272664, started 05:22 CDT) running in the cgroup — systemd's own journal: Unit process 272664 (MainThread) remains running after unit stopped, with the tracked main an adopted process not our child. Socket activation started a new server (pid 1668515) at 12:38:44 and handed it the same listen socket (both processes hold fd 3 → socket inode 11096). Both accept() on it, so /api/version answers alternate startedAt 10:22:55Z / 17:38:45Z under one serverId and /api/session-activity flips between divergent session sets. Fleet dispatch/capacity reads over port 3000 are unreliable while this lasts.

Fix

  • openchamber-serve (the start script) now reaps leftover servers before exec: SIGTERM, up to 10 s drain (OpenChamber's documented SIGTERM drain), then SIGKILL. Because the start script's store path is the unit's deploy key, this change also triggers the activation restart that deploys it.
  • The matcher requires a node/openchamber exe plus the serve --port 3000 cmdline — verified live: it matches exactly pids 272664 + 1668515, does not match the managed OpenCode children (ports 42573/39327, which must stay up for adoption), and does not match a diagnostic shell whose argv merely contains the pattern text.
  • ExecStartPost assertion fails the start loudly unless exactly one server is on port 3000 afterwards. Verified against the live duplicate state: openchamber: expected exactly 1 server on port 3000 after start, found 2, exit 1.

Verification

🤖 Queue lane A (repo-queue-worker-shift-nix), task 3677.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

The user unit is socket-activated with KillMode=process, so a stop
terminates only the tracked main process. A server that is no longer
the tracked main (a self-handoff predecessor) survives in the cgroup
still holding the listen socket systemd handed it; the next start
passes the same socket to a new server and both accept() on it, so
/api/version answers alternate between instances (2026-10-29: pids
272664 + 1668515 on 127.0.0.1:3000, alternating startedAt).

- openchamber-serve now reaps leftover servers (SIGTERM, up to 10 s
  drain, then SIGKILL) before exec; the matcher requires a node/
  openchamber exe plus the serve cmdline so diagnostic shells whose
  argv merely contains the pattern text are never matched. The
  managed OpenCode (different port) is untouched and stays up for
  adoption, per the KillMode=process design.
- ExecStartPost assertion fails the start loudly unless exactly one
  server is on port 3000 afterwards.

Tracking issue: dezren39/darwin-nix#1117
(cross-repo set; the systemd model lives here, the launchd port in
dezren39/nix disables self-handoff for the same reason).
@dezren39
dezren39 requested a review from a team as a code owner October 29, 2026 23:43
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 6 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9e2d4ca7-e8f0-46d7-80b8-2d99ee971bd6
Commits

Reviewing files that changed from the base of the PR and between 5a34fa7 and 29d77ef.

Files selected for processing: 1
  • home/common/default.nix
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T00:18:46.591285Z 29d77ef New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bc6044f7a3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread home/common/default.nix Outdated
# The pattern text itself rides in the argv of anything inspecting
# the fleet (a grep, a diagnostic shell), so a cmdline match alone
# is not proof: the process must actually be the node server.
exe="$(readlink "$proc/exe" 2>/dev/null)" || continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Provide coreutils to both process-scanning scripts

The OpenChamber unit explicitly sets PATH to only the mcpx, git, and openssh bin directories, but both new scripts invoke unqualified coreutils commands. In particular, readlink is unavailable, and its suppressed error causes every process to be skipped: cleanup does nothing and the post-start assertion always reports zero servers, failing startup and triggering repeated restarts. Running the extracted assertion against a matching Node process reproduced this failure when coreutils was excluded from PATH. Add pkgs.coreutils to the service PATH or use absolute executable paths in both scripts.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 29d77ef — thank you, this one would have broken the service on deploy: the guard would have silently matched nothing (and the assertion failed every start). is now in the unit's PATH via the same list, with a comment naming the guard's dependency on it (readlink/cat/tr/seq/sleep).

Comment thread home/common/default.nix Outdated
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
# Fail the start loudly if the start script's single-instance
# guard left anything but exactly one server on the port.
ExecStartPost = "${openchamberSingleInstanceCheck}";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Wait for startup completion before asserting the instance count

This unit leaves Type unset and therefore uses simple; the systemd documentation specifies that post-start commands run once “the process has been started for Type=simple,” not after a startup shell reaches its final exec. Consequently, this assertion races the wrapper's process scan and up-to-ten-second drain. During duplicate recovery it can count both old servers before cleanup finishes, or zero after they exit but before the replacement starts, causing startup to fail and systemd to terminate the wrapper instead of completing recovery. Synchronize the assertion with cleanup completion and actual server startup.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 29d77ef by removing the assertion rather than synchronizing it: the script and unit line are gone. The start script now verifies synchronously after the reap loop (SIGTERM → ≤10 s drain → SIGKILL → re-check) and exits 1 — 'refusing to start a second server on port 3000' — if a leftover survives, which is race-free by construction since the same process owns the whole sequence before exec. The loud-failure property is preserved (failed start + journal message + Restart=on-failure retries the reap).

Comment thread home/common/default.nix Outdated
*) continue ;;
esac
case "$cmd" in
*"--port 3000"*) leftovers="$leftovers $pid" ;;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Match the complete port argument before terminating servers

If another OpenChamber instance runs on port 30000–30009, this substring matcher also selects it as a port-3000 leftover, so starting or restarting this service sends SIGTERM and potentially SIGKILL to an unrelated instance that cannot share the intended listener. Applying the exact matcher to a live Node process with cli.js serve --port 30001 reproduced the false match. The assertion duplicates this prefix match and also counts different-port instances. Require an argument boundary after 3000 in both matchers.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 29d77ef: the matcher now requires an argument boundary — patterns are --port 3000 and --port=3000 (cmdline words are space-separated after the NUL translation, so the trailing word is covered). Verified on sample strings: 3000 matches in both forms and both positions; 30001 and 30009 do not match in either form. The duplicate assertion matcher is gone with the ExecStartPost removal above, so this is now the only matcher.

…uard

- P1 (PATH): the unit PATH held only mcpx/git/openssh, so the guard's
  coreutils (readlink/cat/tr/seq/sleep) resolved to nothing and the
  scan silently matched no processes. Add pkgs.coreutils to the
  service PATH.
- P1 (race): ExecStartPost with Type=simple fires after fork, racing
  the guard's up-to-10s drain -- it could count 0 or 2 and fail a
  recovery start. Remove the assertion script and ExecStartPost; the
  start script now verifies synchronously after reaping and exits 1
  (refusing to start a second server) if a leftover survives.
- P2 (prefix match): "--port 3000" substring also matched ports
  30001-30009 of unrelated instances. Require an argument boundary
  ("--port 3000 " / "--port=3000 ") in the matcher.

Matcher re-verified live (only the running server matches) and on
sample strings for 3000/30001/30009 in both flag forms.
@dezren39
dezren39 added this pull request to the merge queue Oct 30, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to Branch Protection failures Oct 30, 2026
You're not authorized to push to this branch. Visit "About protected branches" for more information.
@dezren39
dezren39 added this pull request to the merge queue Oct 30, 2026
Merged via the queue into main with commit 2cb7534 Oct 30, 2026
3 checks passed
@dezren39
dezren39 deleted the queue/3677-1117-openchamber-single-instance branch October 30, 2026 00:24
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