jcode icon indicating copy to clipboard operation
jcode copied to clipboard

Windows: closing the window that started the shared server disconnects all the other windows

Open Aether0Pneuma opened this issue 2 weeks ago • 1 comments

I usually have 3-4 jcode windows open in Windows Terminal. Whenever I close one of them, the others drop for a second and show "Reconnected successfully". Most of the time it's just annoying, but if a swarm is running, the workers' turns get killed and the coordinator never wakes up again (the await_members it was waiting on is just gone). Happened 8 times in 3 days before I started digging.

Turns out it's not any window. It's the one that started the server.

If there's no server running, the first client window spawns builds\shared-server\jcode.exe --provider auto serve as its own child process. You can see it in ~/.jcode/servers.json: the registered pid's parent is that window's jcode.exe. So when that window closes, Windows Terminal takes the whole process tree down, server included. The other windows log this:

RemoteConnection::next_event: peer closed (session_id=…)
handle_disconnect: … reason=Some(PeerClosed), detail=server closed the connection
Reconnect: no live server after 3 attempts; spawning a replacement shared server

and the replacement ends up as a child of whichever window spawned it, so the whole thing repeats next time. The server never logs a shutdown, it's just killed from outside.

Repro

  1. Make sure no server is running.
  2. Open window A, then window B.
  3. Check servers.json: the pid is a child of A.
  4. Close A. B disconnects right away.

Control. I started the server myself outside of any terminal (via WMI, Win32_Process.Create with jcode.exe --provider auto serve and stdout redirected to a file). Then opened A and B, killed A's whole process tree. Server pid stayed the same, B didn't notice anything, no peer closed in the log. So that's my workaround for now.

I'd expect the client to spawn the shared server detached on Windows (DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, maybe CREATE_BREAKAWAY_FROM_JOB too), so it doesn't belong to whatever window happened to be first.

You'd think server keepalive would cover this, but on my machine every serve it spawns hangs before it starts listening, so a window always ends up owning the server anyway. I left the details on #1158 since it looks like the same loop.

Probably related: #1651 (idle workers aren't restored after the restart this causes).

Windows 11 Enterprise LTSC 2024 (26100), Windows Terminal 1.24, jcode v0.99.1 (7206efd0c).

Aether0Pneuma avatar Nov 15 '26 00:11 Aether0Pneuma

I reproduced the shared-server lifetime failure on Windows 11 Pro build 26300 with current master 7ec81dca5. I built jcode with cargo build --locked --no-default-features --bin jcode and used a separate JCODE_HOME, runtime directory and socket, with telemetry disabled.

The test creates a Windows job with KILL_ON_JOB_CLOSE | BREAKAWAY_OK, assigns the isolated launcher to it, and runs jcode --no-update --socket <private-socket> server start --json. Startup returned {"status":"running"} and the private named pipe was listening. Closing the job killed the server and the pipe stopped listening. This simulates the job-lifetime mechanism; I did not close a real Windows Terminal tab or run a swarm session.

An independent native Windows process probe found:

  • Ordinary children and DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP children died when that job closed.
  • Adding CREATE_BREAKAWAY_FROM_JOB let the child survive when the job allowed breakaway.
  • Under a job without the breakaway limit, a launch requesting that flag failed with Windows error 5 (ACCESS_DENIED).

So just adding detach flags does not cover both job policies, and unconditional breakaway can turn startup into an error. The reporter's WMI workaround is useful, but preserving Jcode's process environment, startup errors, readiness detection and restricted-host expectations needs a design decision; I have not implemented or verified a WMI-based Jcode launcher.

Would the project prefer a best-effort breakaway with a clearly reported fallback when a host forbids it, or a separate Windows daemon-launch mechanism for the restricted case? I can continue with a scoped implementation and native Windows lifecycle tests once the intended behavior is clear.

Follow-up experiments: A local best-effort breakaway prototype kept the actual isolated Jcode server listening after an allow-breakaway job closed, but the fallback server still died when a no-breakaway job closed. Separately, an isolated WMI Win32_Process.Create test forwarded the test environment and launched the actual server outside a no-breakaway job; its listener survived the job closing and jcode server stop --force cleaned it up. That WMI probe did not exercise the normal Jcode launcher or preserve its startup stderr, so it is not a complete implementation. No real terminal tab or active swarm was tested.

Aafimalek avatar Nov 16 '26 08:11 Aafimalek