Skip to content

fix: Windows handle inheritance leak - #1408

Closed
OnePaiGu wants to merge 1 commit into
vercel-labs:mainfrom
OnePaiGu:fix/Windows-handle-inheritance-leak
Closed

OnePaiGu wants to merge 1 commit into
vercel-labs:mainfrom
OnePaiGu:fix/Windows-handle-inheritance-leak

Conversation

@OnePaiGu

@OnePaiGu OnePaiGu commented Jun 2, 2026

Copy link
Copy Markdown

Root Cause

Windows handle inheritance leak.

When the CLI process spawns the daemon via Command::spawn(), the Rust standard library on Windows defaults to bInheritHandles = TRUE. This means the daemon child process inherits all handles marked as "inheritable" from the parent process, including:

  • The stdout pipe handle created by PowerShell for $output = capture
  • The stderr pipe handle created by PowerShell for 2>&1 redirection

Timeline:

PowerShell                    CLI Process                  Daemon Process
    |                             |                             |
    |-- create pipe (read+write)->|                             |
    |   (write end -> CLI stdout) |                             |
    |                             |                             |
    |   $output = & CLI ...       |                             |
    |   (waiting for pipe read    |                             |
    |    end to close)            |                             |
    |                             |-- spawn(daemon) ----------->|
    |                             |   bInheritHandles=TRUE      |
    |                             |                             |
    |                             |-- exit, close own handles ->|
    |                             |                             |
    |   pipe write end still held by daemon!                    |
    |   -> PowerShell keeps waiting --------------------------->|
    |                             |                             |
    |   until daemon also closes  |<-- close kills daemon ------|
    |   -> PowerShell returns     |                             |

Proposed Fix

Use the Windows CreateProcessW API directly in cli/src/connection.rs to spawn the daemon with bInheritHandles = FALSE , preventing the daemon from inheriting any handles from the parent process.

Key changes:

  1. New spawn_daemon_no_inherit function : Calls CreateProcessW directly with bInheritHandles = FALSE and explicit creation flags ( CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS | CREATE_BREAKAWAY_FROM_JOB | CREATE_UNICODE_ENVIRONMENT ).
  2. New build_env_block function : Constructs a complete environment block (parent env vars + custom vars like AGENT_BROWSER_DAEMON=1 ), since passing a non-null lpEnvironment means the child only uses the provided variables.
  3. New WindowsDaemonChild struct : Replaces std::process::Child on Windows. Only holds the process handle for detecting early daemon exit, without any pipe handles.
  4. Modified ensure_daemon function : Uses spawn_daemon_no_inherit instead of Command::spawn() on Windows.

Verification

Scenario Before Fix After Fix
Manual open --headed Exits normally Exits normally
$output = & open --headed Hangs, no exit Exits normally, $LASTEXITCODE=0
close then open --headed (script) Hangs, no exit Exits normally
open when daemon already running Exits normally Exits normally
Daemon startup failure detection Reads stderr for error info Shows exit code

Environment

  • OS : Windows (Server 2022, also reproducible on Windows 10/11)
  • agent-browser version : 0.27.0
  • Rust : 1.96.0 (stable)
  • PowerShell : 5.1 (also affects PowerShell 7.x)

@vercel

vercel Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

@OnePaiGu is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

Comment thread cli/src/connection.rs
}
}

#[cfg(windows)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows daemon startup loop returns a spurious "Daemon process exited during startup" error when a concurrent CLI invocation wins the bind race, because it never polls daemon_ready() before erroring on early process exit.

Fix on Vercel

Comment thread cli/src/connection.rs
cmd,
CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS | CREATE_BREAKAWAY_FROM_JOB,
)
.map_err(|e| format!("Failed to start daemon: {}", e))?,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows daemon spawn uses CREATE_BREAKAWAY_FROM_JOB with no fallback, so CreateProcessW fails with ERROR_ACCESS_DENIED in job-controlled environments, breaking daemon startup.

Fix on Vercel

@OnePaiGu

OnePaiGu commented Jul 2, 2026

Copy link
Copy Markdown
Author

This PR fixes #1407 ,which addresses the Windows handle-inheritance leak.

solovskiy added a commit to solovskiy/ai-orchestrator that referenced this pull request Aug 7, 2026
…mpt gotchas

- README.md / Capability: add browser vs testing comparison table with
  MCP servers, Chrome profiles, and use cases; document the mcp→tools
  conversion in deploy-agent.js (opencode silently ignores mcp: in
  markdown frontmatter); document agent-browser cold-start hang on
  Windows (vercel-labs/agent-browser#1408 handle-inheritance bug) and
  run-job.sh workaround (CLI warmup before MCP access); document
  per-job session isolation via AGENT_BROWSER_SESSION

- README.md / Installation: add Windows troubleshooting — local MCP
  commands in ~/.config/opencode/opencode.json must be wrapped in
  cmd /c, otherwise spawn() fails with ENOENT (npm .cmd shims are not
  resolved without a shell) and opencode hangs silently

- docs/workflow.md: add 'Writing agent system prompts — practical
  gotchas' section with three rules discovered during testing agent
  development but applicable to ANY MCP-using agent:
    1. MCP tool names double the server prefix (agent-browser_agent_browser_open, not agent-browser_navigate)
    2. Background server launch requires stdout/stderr redirection (& alone hangs forever)
    3. All paths must be relative — sandbox silently rejects absolute paths

No code changes — documentation only. npm test: 7/7 suites pass.
@lNimien

lNimien commented Sep 15, 2026

Copy link
Copy Markdown

Independent confirmation that this is still worth merging: the leak reproduces on 0.37.1, so it has survived from 0.27.0 through roughly ten minor releases.

Measured on Windows 11 Pro 10.0.26200 with PowerShell 5.1, full numbers in my comment on #1407: a cold open with output captured never returns (killed at 75s on 0.37.1), the same call wrapped in cmd /c '... > log 2>&1' completes in 1.05s, and a warm call with the daemon already up returns in 0.39s. That isolates the hang to the daemon spawning invocation, exactly as described here.

One detail that argues for fixing it at this layer rather than in the npm wrapper: on 0.37.1 the node shim still spawns the native binary with stdio: 'inherit' (bin/agent-browser.js:115), so it only forwards whatever handles the shell handed it. Setting bInheritHandles = FALSE at the CreateProcessW call is the only place that actually stops the daemon from capturing the caller's pipe.

@lahfir

lahfir commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Thanks @OnePaiGu. #1781 fixes the same handle leak with a smaller change (no raw CreateProcessW) and also covers the MCP path, so closing this in favor of it.

@lahfir lahfir closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants