sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] (ts) sandbox timeout/abort kills only the `sh` wrapper; the `python3`/pipeline child keeps running

Open strandly-the-agent opened this issue 1 month ago • 1 comments

Human Overview

(Drafted by strandly-the-agent during review of #5041 — a human needs to fill this section per team/AI_USAGE_POLICY.md.)

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

TypeScript

Strands Version

main @ e46b0dbc (latest tag typescript/v1.20.0)

Language Runtime Version

Node.js 22.14.0

Operating System

Debian 12 (aarch64)

Installation Method

git clone

Steps to Reproduce

import { NotASandboxLocalEnvironment } from '@strands-agents/sdk/sandbox'
const sb = new NotASandboxLocalEnvironment()
const code = "import os,time; open('/tmp/pid','w').write(str(os.getpid())); time.sleep(30)"
const pending = sb.executeCode(code, 'python3', { timeout: 0.5 })
// wait for /tmp/pid to appear, read pid
await pending.catch(e => console.log(e.constructor.name))   // SandboxTimeoutError
await new Promise(r => setTimeout(r, 1500))
process.kill(pid, 0)                                         // succeeds: python3 is still alive

Same result with { signal } and controller.abort() instead of timeout (rejects with SandboxAbortError in ~1 ms, python3 still alive 1.5 s later).

Expected Behavior

On timeout or abort the whole child tree is killed, as the Python SDK does (strands-py/src/strands/sandbox/stream_process.py:26-39: start_new_session=True + os.killpg(pid, SIGKILL)).

Actual Behavior

streamProcess (strands-ts/src/sandbox/stream-process.ts:77-86) does proc.kill('SIGTERM') then SIGKILL on the spawned sh -c only. executeCode runs base64 -d <<EOF | python3 inside that sh (not-a-sandbox-local-environment.ts:50-53, posix-shell.ts:76), so the interpreter is orphaned and keeps running. Same for execute() with any command that forks (sh -c 'sleep 60'). Affects the shell tool's timeout and, after #5041, python_repl's cancellation.

The bash vended tool has the same problem through a separate code path: BashSession spawns its own persistent bash (strands-ts/src/vended-tools/bash/bash.ts:40 on main 794d37c6e) and on timeout calls this._process.kill() (:67), which signals only that shell. On main, agent.tool.bash.invoke({ mode: 'execute', command: 'sleep 30 & echo $! > /tmp/pid; wait', timeout: 0.5 }) returns Command timed out after 0.5 seconds, and 2 s later ps -p $(cat /tmp/pid) still shows sleep 30 with PPID 1.

Possible Solution

spawn(command, args, { detached: true }) and kill the group with process.kill(-proc.pid, sig) on POSIX (fall back to proc.kill on win32) — mirrors the Python implementation. Note the Python comment also explains a second symptom: surviving children hold the pipe write-end open, so close never fires and stdio handles leak.

Additional Context

Repro output
[timeout path, sandbox.executeCode directly]
timeout: SandboxTimeoutError | python alive 1.5s after: true

[abort path, via python_repl tool at #5041 head 547ca72f]
python pid 3137 alive before abort: true
rejected after 1 ms with SandboxAbortError "Execution aborted"
python alive 1.5s after abort: true

Related Issues

Found while reviewing #5041.

strandly-the-agent avatar Oct 09 '26 18:10 strandly-the-agent

updated the description: the bash vended tool leaves background jobs running on timeout too. It spawns its own shell in strands-ts/src/vended-tools/bash/bash.ts:40 and stops it with this._process.kill() (:67), so it doesn't go through streamProcess, and #5184 doesn't cover it. Repro on main 794d37c6e is in the description

arronbailiss avatar Nov 20 '26 17:11 arronbailiss