Skip to content

fix(agent): handle SIGTERM and detect orphaned sessions - #438

Open
pando85 wants to merge 2 commits into
masterfrom
fix/orphaned-sessions-and-signal-handling
Open

fix(agent): handle SIGTERM and detect orphaned sessions#438
pando85 wants to merge 2 commits into
masterfrom
fix/orphaned-sessions-and-signal-handling

Conversation

@pando85

@pando85 pando85 commented Aug 21, 2026

Copy link
Copy Markdown
Owner

Problem

When OpenCode restarts, the Playwright MCP server process (launched via passless agent playwright-mcp) becomes orphaned but keeps running. Passless tracks this as an active session and refuses to start a new one, returning "profile 'coding' already has an active session (pid XXXX)".

Root Causes

  1. No SIGTERM handling in the client - Only SIGINT was handled via ctrlc::set_handler. When OpenCode sends SIGTERM, the client died without calling terminate_principal.
  2. No client-liveness tracking - The daemon had no concept of which admin client requested the session, so it couldn't detect when the client died.
  3. Hardcoded 1-hour session TTL - Ignored the profile's max_session_ttl config.
  4. No PR_SET_PDEATHSIG on principal - The principal wouldn't receive a signal if the daemon died.

Changes

  • Add SIGTERM signal handler in dispatch_run to properly revoke sessions when the client process receives SIGTERM
  • Track requesting client PID in ManagedPrincipalSession and detect orphaned sessions in reap_expired_sessions by checking if client is alive via /proc/{pid}/stat
  • Use profile's max_session_ttl instead of hardcoded 3600s for session deadline
  • Add PR_SET_PDEATHSIG on principal process so it receives SIGTERM if daemon dies
  • Add signal-hook dependency for portable signal handling

Testing

  • All 1465 existing tests pass
  • Build succeeds with --features agent
  • Manual testing confirms orphaned sessions are now cleaned up when client dies

- Add SIGTERM signal handler in dispatch_run to properly revoke sessions
  when the client process receives SIGTERM (e.g., when OpenCode restarts)
- Track requesting client PID in ManagedPrincipalSession and detect
  orphaned sessions in reap_expired_sessions by checking if client is alive
- Use profile's max_session_ttl instead of hardcoded 3600s for session deadline
- Add PR_SET_PDEATHSIG on principal process so it receives SIGTERM if
  daemon dies
- Add signal-hook dependency for portable signal handling

This fixes the issue where Playwright MCP sessions would become orphaned
when OpenCode restarts, blocking new sessions with 'already has an active
session' error.
- Return error instead of Ok(()) when dispatch_run exits due to signal
- Suppress dead_code warning for PassStorageAdapter::new_with_options
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