Skip to content

Windows: a server's descendants are outside the launcher's job, so they outlive disposal and the owner once the server has exited #523

Open

Description

@Blind-Striker

What

On Windows, processes descended from a server started by OpenCodeServer.StartAsync can outlive
both DisposeAsync and the owner process. The launcher's job (KillOnCloseJob, limits 0x3C00)
holds only the server itself and the direct children that a libuv server (Bun, Node) assigns to its
own nested job. Every other descendant is created outside the job through SILENT_BREAKAWAY_OK:

  • the children of a server that is not a libuv program;
  • every grandchild of a libuv server, such as a process started by a shell command or the real
    node.exe of an npx MCP server, which cross-spawn starts through cmd.exe.

Disposal cannot reach these processes once the root has exited, because the ladder skips the tree
kill (taskkill finds only live processes), and the shared job is never closed.

This follows the recorded design (ADR-0032, upstream/libuv parity, re-affirmed when #294 was closed).
This issue asks whether the SDK should offer descendant containment as an option. It also reports
that ADR-0032 line 15 ("its other children end with it") is broader than the behaviour.

Environment

  • OpenCodeDotNet.Sdk 0.9.0-preview.13 (tag v0.9.0-preview.13, a024408), net10.0
  • Windows 11 Pro 26H2, build 10.0.26300
  • opencode 2.0.32 (production); the measured repro below uses a .NET stand-in server

Reproduction (measured)

  1. Start a server whose command is a .NET program (not libuv). After readiness, it starts a child
    with Process.Start(UseShellExecute = false), and that child starts a grandchild the same way.
    Neither process requests CREATE_BREAKAWAY_FROM_JOB.
  2. Let the server exit on its own (code 0) while the two descendants keep running.
  3. await server.DisposeAsync(). Then, in a second run, let the owning process exit instead.
  4. Check each pid (with its start time) for liveness and for membership of the launcher's job:
    IsProcessInJob(handle, KillOnCloseJob.ForCurrentProcess.Current).

Mechanism, not yet measured, for real opencode: through the shell API, run a command that starts a
long-lived grandchild (pwsh -NoProfile -Command "ping -n 600 127.0.0.1"), then kill the owner
hard. ping.exe is expected to survive; pwsh is expected to die.

Expected and actual

Observation (measured, .NET stand-in server) Server Child Grandchild
In the launcher's own job yes no no
Alive after DisposeAsync no yes yes
Alive after the owner exited no yes yes

Note: IsProcessInJob(h, NULL) returned true for all three, because the test host runs inside an
outer job. Check against the launcher's own job handle.

Expected: disposal or the owner's exit ends the server's whole tree, except processes that ask to
outlive it. Actual: only the server, plus the children a libuv server places in its job.

Root cause

  • src/OpenCode.Sdk/Hosting/Internal/Platform/Windows/KillOnCloseJob.cs:27-31: BREAKAWAY_OK | SILENT_BREAKAWAY_OK, so a member's children are created outside the job unless something
    assigns them.
  • src/OpenCode.Sdk/Hosting/Launcher/Internal/LauncherSeams.cs:58: one process-wide job, never
    closed, so no per-server cleanup is possible.
  • src/OpenCode.Sdk/Hosting/Launcher/Internal/WindowsServerLadder.cs:70-73: an exited root returns
    at once.
  • Upstream is identical: packages/util/src/cross-spawn-spawner.ts:442 (v2.0.32) returns on Windows
    for an exited child, and the job is Bun's libuv job with the same four limits.

Impact

  • Host killed: grandchildren survive (builds, test hosts, npx MCP servers, background jobs).
    They can hold file locks: a harness on 0.9.0-preview.13 saw a surviving descendant lock the next
    build's executable.
  • Server crash: Bun's job ends its direct children; grandchildren survive; disposal does nothing.
  • Normal disposal of a live server: taskkill /T reaches descendants by parent pid, but not
    orphans whose intermediate parent already exited (MSBuild nodes, start-launched processes).
  • A custom Command that is not a libuv program: none of its children are held.
  • POSIX has a related gap: upstream spawns every tool process detached (its own session), so the
    group kill never reaches them, and a SIGKILLed or crashed server leaves all of them running.

Proposed change and alternatives

A. An opt-in containment mode (for example on OpenCodeServerOptions).

  • A per-server job with KILL_ON_JOB_CLOSE | DIE_ON_UNHANDLED_EXCEPTION and no breakaway limits.
  • Disposal runs TerminateJobObject after the tree kill, also when the root already exited.
  • Default stays at parity.
  • Cost: detached descendants, the persistent PTY daemon among them, end with the server.
  • The job handle must stay rooted until the server has exited and disposal has run, or
    finalization would kill a server the caller leaked.

B. Containment as the default, with an opt-out for parity. This reverses ADR-0032 and the #294
decision, and needs an ADR amendment.

C. Drop SILENT_BREAKAWAY_OK on the shared job.

  • Not recommended.
  • It kills the PTY daemon on every owner crash.
  • It does nothing at disposal, because the shared job is never closed.

D. Leave the code; fix ADR-0032:15/24-25; document the consumer mitigation. The mitigation is to
run the owner inside its own kill-on-close job without breakaway, which should stop silent
breakaway at that job (needs a probe).

Every option except D adds a divergence row to the Windows table in
docs/architecture/client-runtime.md.

Test plan

  1. Windows, a .NET (non-libuv) stand-in whose child and grandchild run on after the root exits:
    assert, by pid and start time, both alive or both ended after DisposeAsync, according to the
    mode.
  2. The same test, with the owner killed (LauncherHost).
  3. A Bun stand-in whose plain child starts a non-libuv grandchild: pins today's escape and the
    new mode's containment.
  4. A detached child of a Bun stand-in: survives in parity mode, ends in containment mode.
  5. Measure the nested case: a silent breakaway from Bun's job under a parent job with
    BREAKAWAY_OK but without SILENT_BREAKAWAY_OK. Does the child stay in that parent job?
  6. Two servers in one host: disposing one does not end the other's tree.

Open questions for the maintainers

  1. Is upstream parity still the bar for descendant lifetime, or may containment diverge behind an
    option?
  2. Containment as the default or as opt-in?
  3. Should DIE_ON_UNHANDLED_EXCEPTION apply to every contained tool process?
  4. Is a POSIX counterpart in scope (a Linux subreaper or cgroup), or documented only?
  5. Does the ADR-0032:15 wording get corrected in the same PR?

Source: a Windows descendant-lifetime investigation, 2026-10-21; SDK source at a024408; ADR-0032;
docs/architecture/client-runtime.md (Launcher).

Activity

  1. added
    enhancement
    needs-triage
    on Oct 21, 2026
  2. removed
    needs-triage
    on Nov 6, 2026
  3. added this to the 1.1 milestone on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions