Skip to content

fix(processes): fix multiple clawx processes running concurently#589

Merged
su8su merged 18 commits intomainfrom
cursor/clawx-e5a2
Mar 20, 2026
Merged

fix(processes): fix multiple clawx processes running concurently#589
su8su merged 18 commits intomainfrom
cursor/clawx-e5a2

Conversation

@hazeone
Copy link
Contributor

@hazeone hazeone commented Mar 19, 2026

Summary

This PR addresses the issue of multiple ClawX processes running concurrently and causing port conflicts, particularly after abnormal application termination. It hardens the application's process lifecycle management to ensure reliable single-instance operation and proper cleanup of child processes.

Key changes include:

  • Deterministic Quit Lifecycle: Ensures the gateway process is properly stopped and cleaned up during application exit, with a bounded timeout.
  • Robust Single-Instance Lock: Implements a file-based fallback lock mechanism in addition to Electron's requestSingleInstanceLock() to prevent multiple instances from starting, even if the lock file is malformed or stale.
  • Improved Gateway Termination: Enhances the supervisor to use process-tree termination on Windows (taskkill /F /T) for owned gateway processes and adds a post-cleanup port release wait.
  • Enhanced Observability: Adds clearer logging for process ownership, instance outcomes, and updates documentation regarding Electron's multi-process nature and troubleshooting port conflicts.
  • New Unit Tests: Adds tests for quit lifecycle, gateway supervisor termination, and process instance lock robustness.

Related Issue(s)

Addresses the root cause of multiple ClawX processes and port conflicts described in the initial task.

Type of Change

  • Bug fix
  • Refactor
  • New feature
  • Documentation
  • Other

Validation

The following checks were performed:

  • Unit Tests: New and existing unit tests for process-instance-lock, main-quit-lifecycle, and gateway-supervisor were run and passed.
  • Type Checking: pnpm run typecheck passed.
  • Manual Validation:
    • Confirmed only one listener on 127.0.0.1:18789 after startup.
    • Verified immediate exit of duplicate instances on second launch.
    • Validated correct creation, recovery from stale/malformed lock files, and release of the clawx.instance.lock file.
    • Tested SIGINT/SIGTERM handling to ensure proper lock release and gateway shutdown.

Checklist

  • I ran relevant checks/tests locally.
  • I updated docs if behavior or interfaces changed.
  • I verified there are no unrelated changes in this PR.
Open in Web Open in Cursor 

cursoragent and others added 6 commits March 19, 2026 11:26
Co-authored-by: Haze <hazeone@users.noreply.github.com>
Co-authored-by: Haze <hazeone@users.noreply.github.com>
Co-authored-by: Haze <hazeone@users.noreply.github.com>
Co-authored-by: Haze <hazeone@users.noreply.github.com>
Co-authored-by: Haze <hazeone@users.noreply.github.com>
@cursor
Copy link

cursor bot commented Mar 19, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@hazeone hazeone changed the title ClawX 进程管理 fix(processes): fix multiple clawx processes running concurently Mar 19, 2026
cursoragent and others added 8 commits March 19, 2026 04:36
Co-authored-by: Haze <hazeone@users.noreply.github.com>
Co-authored-by: Haze <hazeone@users.noreply.github.com>
Co-authored-by: Haze <hazeone@users.noreply.github.com>
Co-authored-by: Haze <hazeone@users.noreply.github.com>
Co-authored-by: Haze <hazeone@users.noreply.github.com>
Co-authored-by: Haze <hazeone@users.noreply.github.com>
@hazeone hazeone mentioned this pull request Mar 19, 2026
su8su and others added 4 commits March 20, 2026 14:58
Address residual process-leak and port-conflict risks without changing normal gateway restart behavior.

Changes:
- Use Windows process-tree termination (taskkill /T) in known-PID path of agent-deletion gateway restart.
  - Prevents child-process residue when a stale gateway is force-restarted.
- Add GatewayManager.forceTerminateOwnedProcessForQuit() for quit-time timeout fallback.
  - before-quit now attempts an owned-process force termination if graceful stop times out.
  - Scope is intentionally narrow: only owned process is eligible.
- Harden process instance lock release semantics.
  - release() now verifies current lock owner before deleting lock file.
  - Avoids deleting a lock that was re-acquired by another process during handover races.
- Add regression tests:
  - agents route test verifies Windows known-PID restart path uses taskkill /T.
  - process-instance-lock test verifies release does not remove lock after ownership change.

Why this is minimal and safe:
- No public API changes.
- No behavioral changes to normal reload/restart orchestration.
- Focused only on leak-prone timeout and Windows kill semantics.

Validation:
- Ran vitest for:
  - tests/unit/agents-routes.test.ts
  - tests/unit/process-instance-lock.test.ts
  - tests/unit/gateway-supervisor.test.ts
  - tests/unit/main-quit-lifecycle.test.ts
  - tests/unit/signal-quit.test.ts
- Result: 5 test files passed, 14 tests passed.
@su8su su8su merged commit 9b503b5 into main Mar 20, 2026
4 checks passed
@su8su su8su deleted the cursor/clawx-e5a2 branch March 20, 2026 10:34
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.

4 participants