You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OpenCode leaves an orphaned process when it itself is killed #11527
When I run OpenCode programatically using await Bun.spawn(["opencode"]), it spawns a sub-process moments after starting up which then becomes orphaned (it deparents from the opencode process that spawned it and its PPID becomes 1).
When I kill the OpenCode instance I started by PID, it won't kill the sub-process since it has deparented.
Bun will keep running until the orphaned process dies as it was never unref'd from Bun's PoV (only from the PoV of the Open Code process that spawned it).
This is a problem for use-cases where OpenCode is invoked programmatically (Git hooks for qualitative validations, solutions for terminal mirroring not based on SSH but on Bun's Terminal API etc.)
Plugins
none
OpenCode version
1.1.48
Steps to reproduce
Run:
console.log("Killing all OpenCode instance to ensure clean slate…");awaitBun.$`killall -KILL opencode`.nothrow();console.log("Printing running OpenCode instances…");console.log("\t(There should be none)");awaitBun.$`pgrep -fl opencode`.nothrow();console.log("Starting OpenCode…");constproc=awaitBun.spawn(["opencode"]);console.log("Started OpenCode. PID:",proc.pid);consttimeout=100;console.log(`Waiting for ${timeout} ms for the orphan (worker?) to deparent…`);awaitBun.sleep(timeout);console.log(`Killing OpenCode with PID ${proc.pid}…`);proc.kill("SIGKILL");awaitproc.exited;console.log(`Killed OpenCode with PID ${proc.pid}. ${proc.signalCode}`);console.log("Printing running OpenCode instances…");console.log("\t(There should be none, but there is one with PPID 1 - orphan)");awaitBun.$`pgrep -fl opencode`.nothrow();console.log("Killing self to prevent blockage due to ref'd deparented process");process.exit();
You will see that it starts off with no orphans, then it spawns OpenCode, waits for OpenCode to spawn the sub-process (maybe some worker?), but then the sub-process immediately becomes deparented. Next, the OpenCode instance that was started gets killed and we query running opencode processes - there should be one, but the one orphan remains.
This script is re-entrant, on the next run, it will kill the orphan, run again and leave another orphan, demonstrating the problem.
These issues collectively point to a systemic problem with process lifecycle management in OpenCode - subprocesses aren't being properly tracked or cleaned up when parent processes are killed.
Feel free to ignore if your specific case is different from these issues.
Activity
github-actions commentedon Jan 31, 2026
This issue might be a duplicate of or related to existing issues. Please check:
These issues collectively point to a systemic problem with process lifecycle management in OpenCode - subprocesses aren't being properly tracked or cleaned up when parent processes are killed.
Feel free to ignore if your specific case is different from these issues.
TomasHubelbauer commentedon Jan 31, 2026
FYI I am using no MCP servers, the orphaned process is
/Users/tom/.bun/install/global/node_modules/opencode-darwin-arm64/bin/opencode.fix: prevent orphaned worker process when killed
github-actions commentedon Apr 3, 2026
To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one.
TomasHubelbauer commentedon Apr 3, 2026
LOL
ethanhuang996-alt commentedon Jun 27, 2026
复现确认:OpenCode 在 Windows 上的孤儿子进程问题
我们在 Hermes Agent 环境下成功复现了 OpenCode 子进程孤儿问题。
根因分析
经过对 OpenCode 生成的 Python 脚本的二进制检查,确认 OpenCode 在 Windows 上 spawn 子进程时使用了
detached: true标志(已在二进制字符串中直接确认)。由于 OpenCode 是 Bun 编译的二进制文件,无法直接修改其源码。解决方案(Windows 专用):autokill.py
我们编写了一个 33 行的 watchdog 模块(
autokill.py),内置于 OpenCode 生成的 Python 脚本中。原理是用 ctypes 调用 Windows kernel32 API 的WaitForSingleObject,每 2 秒检测父进程是否存活。当父进程死亡时,脚本自动执行os._exit(1)退出,避免成为孤儿进程。实战验证
在 Hermes Agent 环境下实测:杀死 bash/opencode 进程后,Python 脚本在 2 秒内检测到父进程死亡并自动退出,不再有孤儿进程残留。
完整代码
使用方式
在任何从 OpenCode 启动的 Python 脚本开头加入:
该方案仅适用于 Windows 平台(依赖
ctypes.windll.kernel32),在其他平台上 import 会自动静默跳过。