Skip to content

Commit 768cbe1

Browse files
author
Yoshihiro Takahara
committed
fix: improve process communication handling with proper timeout
- Add communicate_with_timeout helper function - Ensure proper process cleanup on errors - Fix stalled stderr issue
1 parent 0dc2df7 commit 768cbe1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/mcp_shell_server/shell_executor.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,24 @@ async def execute(
501501
try:
502502
# Send input if provided
503503
stdin_bytes = stdin.encode() if stdin else None
504+
505+
async def communicate_with_timeout():
506+
try:
507+
return await process.communicate(input=stdin_bytes)
508+
except Exception as e:
509+
try:
510+
process.kill()
511+
await process.wait()
512+
except Exception:
513+
pass
514+
raise e
515+
504516
if timeout:
505517
stdout, stderr = await asyncio.wait_for(
506-
process.communicate(input=stdin_bytes), timeout=timeout
518+
communicate_with_timeout(), timeout=timeout
507519
)
508520
else:
509-
stdout, stderr = await process.communicate(input=stdin_bytes)
521+
stdout, stderr = await communicate_with_timeout()
510522

511523
# Close file handle if using file redirection
512524
if isinstance(stdout_handle, IO):

0 commit comments

Comments
 (0)