Skip to content

fix(ctx_execute): prevent hang when background=true without timeout - #975

Open
ayuayue wants to merge 1 commit into
mksglu:mainfrom
ayuayue:fix/background-without-timeout-hangs
Open

fix(ctx_execute): prevent hang when background=true without timeout#975
ayuayue wants to merge 1 commit into
mksglu:mainfrom
ayuayue:fix/background-without-timeout-hangs

Conversation

@ayuayue

@ayuayue ayuayue commented Jul 19, 2026

Copy link
Copy Markdown

Problem

When ctx_execute is called with background: true but no explicit timeout, the tool call hangs forever and the session becomes stuck.

Root cause

Two conflicting code paths:

  1. server.ts appends a permanent setInterval(()=>{}, 2147483647) to keep the backgrounded process alive
  2. executor.ts only creates the timer that triggers detach when timeout is explicitly set

With background: true and no timeout:

  • The infinite interval keeps the Node process alive forever
  • No timer fires to trigger the detach logic
  • The process never exits → "close" event never fires → res() never called → tool call hangs

Fix

Only append the keepalive interval when a timeout is actually configured (effTimeout !== undefined). Without a timeout, the process exits naturally when its code finishes, the "close" event fires, and the tool call completes normally.

Changes

  • src/server.ts: Move effTimeout = resolveExecTimeout(timeout) before the instrumented code template, and gate the infinite interval on background && effTimeout !== undefined

Impact

Scenario Before After
background: true + timeout ✅ Normal (detach on timeout) ✅ Unchanged
background: true + no timeout ❌ Tool call hangs forever ✅ Process exits naturally, tool call completes
No background flag ✅ Normal ✅ Unchanged

When background=true is used without an explicit timeout, the infinite
setInterval appended to keep the process alive would prevent the Node.js
process from ever exiting (no timer to trigger the detach logic). This
caused the tool call to hang forever.

Fix: only append the infinite keepalive interval when a timeout is
actually set (effTimeout !== undefined). Without a timeout, the process
will exit naturally when its work is done, and the tool call completes
normally.

The background=true documentation already states it requires a timeout:
'Keep process running after timeout (for servers/daemons).'
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.

1 participant