Skip to content

fix(unix): detach Codex-launched gateways reliably - #845

Merged
btsouth merged 1 commit into
mainfrom
fix/sbs-1063-codex-session-detach
Aug 23, 2026
Merged

fix(unix): detach Codex-launched gateways reliably#845
btsouth merged 1 commit into
mainfrom
fix/sbs-1063-codex-session-detach

Conversation

@btsouth

@btsouth btsouth commented Aug 23, 2026

Copy link
Copy Markdown
Owner

What and why

Codex starts MCP children as process-group leaders. That made Toolport's setsid() fail, leaving the gateway in Codex's terminal session. The login-shell PATH probe could then receive SIGTTIN and stop the whole gateway before its first response.

Normal Unix startup now handles that launch shape and detaches before startup I/O or PATH discovery. Early diagnostic modes keep their existing terminal behavior.

Closes SBS-1063.

Testing

  • Added a subprocess regression using process_group(0)
  • cargo clippy --manifest-path src-tauri/Cargo.toml --no-default-features --all-targets
  • cargo test --manifest-path src-tauri/Cargo.toml --no-default-features --lib --bins --tests
  • Codex-style live launch with D-Bus variables removed answered status and fetched Linear

Notes

No Omarchy or client configuration changes are required.


Note

Medium Risk
Touches process-group and session leadership at gateway startup via unsafe POSIX calls. Failure modes are fail-open to the previous best-effort detach, but a bad detach could still affect job-control or child spawn on Unix.

Overview
Unix gateways launched as process-group leaders (Codex’s MCP spawn) now leave the client’s terminal session instead of failing setsid with EPERM and stopping on SIGTTIN during the PATH probe.

detach_from_client_session briefly rejoins the parent group and retries setsid before session-bus restore or any child spawn. --selftest-secrets still skips detach so that diagnostic path keeps its old terminal behavior. A Unix subprocess test launches the binary with process_group(0) and asserts it becomes its own session.

Reviewed by Cursor Bugbot for commit 31bb5e5. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Fix gateway stall by robustly detaching from client terminal session on Unix

  • Replaces the single inline setsid() call with a new detach_from_client_session() function in toolport-gateway.rs that handles the case where the gateway is already a process-group leader: it temporarily reattaches to the parent's group via setpgid(0, parent_group) then calls setsid() again.
  • Moves the detach earlier in startup — before environment restoration and PATH probing — so downstream spawns and probes run without a controlling terminal. Detach is skipped when the binary is invoked with --selftest-secrets.
  • Adds a Unix-only integration test in gateway_session_detach.rs that spawns the gateway as a process-group leader and polls until its session id equals its pid.
  • Behavioral Change: all normal Unix startup now detaches from the client's terminal session before any probes or downstream spawns; non-Unix platforms are unchanged.

Macroscope summarized 31bb5e5.

@cursor

cursor Bot commented Aug 23, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_239517ed-bb9e-412e-9043-fce4c14b5cdf)

@coderabbitai

coderabbitai Bot commented Aug 23, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 07321d10-e6e0-4825-953d-e441aacc8b0d

📥 Commits

Reviewing files that changed from the base of the PR and between c2afe3b and 31bb5e5.

⛔ Files ignored due to path filters (1)
  • CHANGELOG.md is excluded by !**/*.md
📒 Files selected for processing (2)
  • src-tauri/src/bin/toolport-gateway.rs
  • src-tauri/tests/gateway_session_detach.rs

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

The gateway now uses a dedicated session-detachment helper. Normal startup detaches from the client session, while --selftest-secrets skips detachment. A Unix regression test validates process-group-leader startup and session separation.

Changes

Gateway session detachment

Layer / File(s) Summary
Detachment helper and startup wiring
src-tauri/src/bin/toolport-gateway.rs
The gateway adds platform-specific session detachment, skips it for --selftest-secrets, reuses the computed self-test flag, and removes the previous inline setsid block.
Session detachment regression test
src-tauri/tests/gateway_session_detach.rs
A Unix-only test launches the gateway as a process-group leader, checks that it remains running, and polls for a separate session within two seconds.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 31bb5

This PR changes Unix gateway startup so Codex-launched gateways detach before startup I/O and adds regression coverage; no actionable merge-blocking risk remains beyond normal checks and review.

Suggested reviewers: bharadwajkanneveti, forever-ivy

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the Unix gateway detachment fix.
Description check ✅ Passed The description explains the Unix detachment issue, implementation, behavior, testing, and related issue.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/sbs-1063-codex-session-detach

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.

@btsouth
btsouth merged commit 7e3c146 into main Aug 23, 2026
19 of 20 checks passed
@btsouth
btsouth deleted the fix/sbs-1063-codex-session-detach branch August 23, 2026 21:52
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