Bug: Web UI "Connecting to session..." infinite spinner when switching sessions
Version: kimi-cli 1.48.0 (Homebrew, macOS 26.4, arm64)
Component: kimi web — Web UI (Technical Preview)
Browser: Chrome 150.0.7871.187
Description
When switching between sessions in the Web UI (http://127.0.0.1:5494), the UI shows an infinite "Connecting to session..." spinner. The session never connects. This happens consistently on every session switch — not just occasionally.
Restarting the kimi web server temporarily resolves the issue, but it returns after the first session switch.
Steps to Reproduce
- Start
kimi web - Open the web UI in Chrome
- Have 2+ existing sessions
- Click on a different session in the sidebar
- Result: "Connecting to session..." spinner appears and never resolves
- All subsequent session switches also fail
Observed Behavior
lsof -i :5494showsCLOSE_WAITTCP connections accumulating — the server doesn't properly close WebSocket connections after the client disconnects- The healthcheck endpoint (
/healthz) responds{"status":"ok"}— the server process itself is alive - Only 1 active
ESTABLISHEDconnection exists while others are stuck inCLOSE_WAIT
Root Cause Analysis
Traced through the source code (sessions.py, process.py, frontend JS):
1. end_replay() livelock under message throughput
end_replay() in process.py loops acquiring _ws_lock, copying buffered messages, and sending them. If _read_loop is actively broadcasting, the buffer refills between iterations, causing the loop to spin indefinitely. This blocks send_status_snapshot() from ever executing.
2. Frontend 15-second timeout → infinite reconnect loop
The frontend waits up to 15 seconds for a session_status message after history_complete. If send_status_snapshot() is blocked (by issue #1), the timeout fires, triggering reconnect(), which starts the cycle over: disconnect → 50ms delay → connect → replay → timeout → reconnect...
3. _broadcast() blocks on dead WebSockets
_broadcast() iterates all websockets in _websockets set and calls await ws.send_text() sequentially. A dead/closing WebSocket blocks delivery to all other connected clients.
4. remove_websocket() doesn't call ws.close()
When a WebSocket is removed from the set, the TCP connection isn't explicitly closed. The transport only closes when the handler function returns, but the handler may be stuck in receive_text().
Expected Behavior
Session switching should work seamlessly — disconnect from current session's WebSocket, connect to new session's WebSocket, replay history, and show the session.
Workaround
Restart the web server:
kill $(lsof -ti :5494); sleep 2; kimi web --no-open &
Related
- PR #1669: "Improve WebSocket reconnection experience in Web UI" — addresses flickering but not the core CLOSE_WAIT / livelock issue
Environment
- macOS 26.4 (Apple Silicon)
- kimi-cli 1.48.0 (Homebrew)
- Chrome 150.0.7871.187
- Multiple sessions (5-6 workers active)
Small confirmation from reading the source: the code paths in this analysis are unchanged on current main (cbc15c07, 1.49.0). src/kimi_cli/web/runner/process.py has not been modified since #1890, so the behaviour described against 1.48.0 still applies to HEAD rather than having been fixed since.
For whoever picks this up, the specific spots are _broadcast at process.py:535, add_websocket_and_begin_replay at 570, and end_replay at 579. _broadcast does already send outside _ws_lock and prune sockets that fail or are not CONNECTED, so points 3 and 4 look partially mitigated already; the replay-buffer drain loop in end_replay is the part I could not talk myself out of, since a buffer that refills while a chunk is being sent keeps the loop going.
I did not try a fix. I cannot reproduce the session switch reliably here, and this looked like the kind of concurrency change that is easy to get subtly wrong without a repro in hand.