Files
openclaw/docs/gateway/gateway-lock.md

35 lines
1.6 KiB
Markdown
Raw Normal View History

2025-12-10 00:46:50 +00:00
---
2025-12-11 15:17:40 +00:00
summary: "Gateway singleton guard using the WebSocket listener bind"
2025-12-10 00:46:50 +00:00
read_when:
- Running or debugging the gateway process
- Investigating single-instance enforcement
title: "Gateway Lock"
2025-12-10 00:46:50 +00:00
---
2026-01-31 21:13:13 +09:00
2025-12-10 00:46:50 +00:00
# Gateway lock
2025-12-11 15:17:40 +00:00
Last updated: 2025-12-11
2025-12-10 00:46:50 +00:00
## Why
2026-01-31 21:13:13 +09:00
- Ensure only one gateway instance runs per base port on the same host; additional gateways must use isolated profiles and unique ports.
2025-12-11 15:17:40 +00:00
- Survive crashes/SIGKILL without leaving stale lock files.
- Fail fast with a clear error when the control port is already occupied.
2025-12-10 00:46:50 +00:00
## Mechanism
2026-01-31 21:13:13 +09:00
2025-12-11 15:17:40 +00:00
- The gateway binds the WebSocket listener (default `ws://127.0.0.1:18789`) immediately on startup using an exclusive TCP listener.
- If the bind fails with `EADDRINUSE`, startup throws `GatewayLockError("another gateway instance is already listening on ws://127.0.0.1:<port>")`.
- The OS releases the listener automatically on any process exit, including crashes and SIGKILL—no separate lock file or cleanup step is needed.
- On shutdown the gateway closes the WebSocket server and underlying HTTP server to free the port promptly.
2025-12-10 00:46:50 +00:00
## Error surface
2026-01-31 21:13:13 +09:00
2025-12-11 15:17:40 +00:00
- If another process holds the port, startup throws `GatewayLockError("another gateway instance is already listening on ws://127.0.0.1:<port>")`.
- Other bind failures surface as `GatewayLockError("failed to bind gateway socket on ws://127.0.0.1:<port>: …")`.
2025-12-10 00:46:50 +00:00
## Operational notes
2026-01-31 21:13:13 +09:00
- If the port is occupied by _another_ process, the error is the same; free the port or choose another with `openclaw gateway --port <port>`.
2025-12-11 15:17:40 +00:00
- The macOS app still maintains its own lightweight PID guard before spawning the gateway; the runtime lock is enforced by the WebSocket bind.