agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

bug(runtime-conpty): leaked stderr-copy goroutine + pipe FD per live session (cmd.Wait never called)

Open fireddd opened this issue 4 months ago • 5 comments

Found by code inspection (not runtime-observed) against main @ cdb0d1f. Windows-only.

Summary

defaultSpawnHost in internal/adapters/runtime/conpty/spawn_windows.go spawns the detached pty-host child, waits for its READY line, then intentionally releases the handle (cmd.Process.Release()) so the daemon can exit while the child keeps running. cmd.Wait() is therefore never called on any path.

Because cmd.Stderr is assigned a custom io.Writer (*boundedBuffer) rather than an *os.File, os/exec creates an internal pipe and starts a background goroutine to io.Copy from the child's stderr into that writer (writerDescriptor in os/exec). That goroutine is normally reaped by cmd.Wait(). With no Wait(), it stays blocked on Read until the child's stderr write end closes — i.e. until the pty-host process itself exits, which for a live interactive session can be hours.

Net effect: one leaked goroutine + one held pipe FD per live conpty session, for the lifetime of that session. It's bounded by concurrent session count (not unbounded growth), and after READY nothing ever reads the captured stderr again — so it's pure waste, not a correctness bug.

Relevant code

internal/adapters/runtime/conpty/spawn_windows.go (around lines 102–170):

stdout, err := cmd.StdoutPipe()      // *os.File — no wrapper goroutine
// ...
stderr := &boundedBuffer{max: maxCapturedStderr}
cmd.Stderr = stderr                  // custom io.Writer -> os/exec spawns pipe + copy goroutine
// ...
cmd.Start()
// ... read READY with timeout ...
case r := <-readyC:
    // ...
    stdout.Close()
    cmd.Process.Release()            // detach; Wait() never called -> stderr copy goroutine never reaped
    return r.addr, cmd.Process.Pid, nil

Suggested fix

Mirror the existing StdoutPipe() handling for stderr: use cmd.StderrPipe(), drain it into boundedBuffer with an owned goroutine, and Close() it on every exit path. A *os.File on Stdout/Stderr gets no os/exec wrapper goroutine, so this removes the leak entirely rather than relying on Wait().

Related minor (same function)

stdout.Close() only runs on the READY success branch. The startup-timeout and ctx.Done() branches call cmd.Process.Kill() but never close stdout, leaving the read-end FD to a GC finalizer (indeterminate timing) rather than a deterministic close. Worth closing on all three paths for consistency with the happy path.

Notes

  • Linux/macOS use the tmux runtime, which does not hit this path.
  • I have not run a profiler against a live Windows daemon; this is from reading the code and the os/exec semantics. Happy to be corrected if the detach path handles this somewhere I missed.

fireddd avatar Jul 17 '26 17:07 fireddd

Thanks for contributing to Agent Orchestrator.

This issue is being picked up by the current external contributor on-call pair:

  • @neversettle17-101
  • @somewherelostt

If someone is already working on this, please continue as usual. The on-call pair is added for visibility, tracking, and support, not to take over the work. If you need help with review, direction, reproduction, or next steps, please tag @neversettle17-101 and @somewherelostt here.

For faster context or live questions, you can also join the AO Discord.

Join the session here: https://discord.gg/H6ZDcUXmq

Come by if you want to see what is being built, ask questions, or just hang around with the community.

somewherelostt avatar Jul 18 '26 08:07 somewherelostt

@codebanditssss Narrowing this to Windows detached-host cleanup: timeout/cancellation paths kill the child without explicitly closing stdout, and the CommandContext/I/O lifecycle needs explicit cleanup when releasing the process without Wait.

A stderr-copy goroutine while the host is alive is not, by itself, proof of a permanent leak—it can finish at EOF. No Windows runtime measurements currently establish the impact.

Please validate repeated successful start/exit and startup timeout/cancel cycles, recording goroutine and handle counts before and after settling. Confirm they return to baseline, startup diagnostics remain available, and a successfully detached host survives cancellation of its originating request.

Vaibhaav-Tiwari avatar Oct 27 '26 15:10 Vaibhaav-Tiwari

i checked the windows conpty launch code and found the cleanup gap described here. i have not measured a running daemon’s resource use, so i cannot say how much it affects performance.

codebanditssss avatar Oct 29 '26 16:10 codebanditssss

18. i checked the windows conpty launch code and found the cleanup gap described here. i have not measured a running daemon’s resource use, so i cannot say how much it affects performance.

are you taking up the issue then?

Vaibhaav-Tiwari avatar Oct 30 '26 10:10 Vaibhaav-Tiwari

23. > 18. i checked the windows conpty launch code and found the cleanup gap described here. i have not measured a running daemon’s resource use, so i cannot say how much it affects performance.

 are you taking up the issue then?

yes

codebanditssss avatar Oct 30 '26 15:10 codebanditssss