Skip to content

Your lightweight open-source personal agent.

fix(shell): kill subprocess on CancelledError to prevent orphan processes

Lingao Meng0e617c32cd58
diff --git a/nanobot/agent/tools/shell.py b/nanobot/agent/tools/shell.py
index e6e9ac0f5..085d74d1c 100644
--- a/nanobot/agent/tools/shell.py
+++ b/nanobot/agent/tools/shell.py
@@ -128,6 +128,19 @@ class ExecTool(Tool):
except (ProcessLookupError, ChildProcessError) as e:
logger.debug("Process already reaped or not found: {}", e)
return f"Error: Command timed out after {effective_timeout} seconds"
+ except asyncio.CancelledError:
+ process.kill()
+ try:
+ await asyncio.wait_for(process.wait(), timeout=5.0)
+ except asyncio.TimeoutError:
+ pass
+ finally:
+ if sys.platform != "win32":
+ try:
+ os.waitpid(process.pid, os.WNOHANG)
+ except (ProcessLookupError, ChildProcessError) as e:
+ logger.debug("Process already reaped or not found: {}", e)
+ raise
output_parts = []