Skip to content

Commit 725dc61

Browse files
committed
PR fixes
1 parent c289efa commit 725dc61

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/mcp/client/stdio.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,14 @@ async def stdin_writer():
168168
try:
169169
yield read_stream, write_stream
170170
finally:
171-
# Clean up process
171+
# Clean up process to prevent any dangling orphaned processes
172172
try:
173173
process.terminate()
174174
if sys.platform == "win32":
175+
# On Windows, terminating a process with process.terminate() doesn't
176+
# always guarantee immediate process termination.
177+
# So we give it 2s to exit, or we call process.kill()
178+
# which sends a SIGKILL equivalent signal.
175179
try:
176180
with anyio.fail_after(2.0):
177181
await process.wait()
@@ -199,20 +203,21 @@ def _get_executable_command(command: str) -> str:
199203
else:
200204
# For Windows, we need more sophisticated path resolution
201205
# First check if command exists in PATH as-is
202-
command_path = shutil.which(command)
203-
if command_path:
206+
if command_path:= shutil.which(command):
204207
return command_path
205208

206209
# Check for Windows-specific extensions
207210
for ext in [".cmd", ".bat", ".exe", ".ps1"]:
208211
ext_version = f"{command}{ext}"
209-
ext_path = shutil.which(ext_version)
210-
if ext_path:
212+
if ext_path:= shutil.which(ext_version):
211213
return ext_path
212214

213215
# For regular commands or if we couldn't find special versions
214216
return command
215217
except Exception:
218+
# If anything goes wrong, just return the original command
219+
# shutil.which() could raise exceptions if there are permission
220+
# issues accessing directories in PATH
216221
return command
217222

218223

@@ -245,12 +250,13 @@ async def _create_platform_compatible_process(
245250

246251
return process
247252
except Exception:
248-
# Don't raise, let's try to create the process using the default method
249-
process = None
250-
251-
# Default method for creating the process
252-
process = await anyio.open_process(
253-
[command, *args], env=env, stderr=errlog, cwd=cwd
254-
)
253+
# Don't raise, let's try to create the process without creation flags
254+
process = await anyio.open_process(
255+
[command, *args], env=env, stderr=errlog, cwd=cwd
256+
)
257+
else:
258+
process = await anyio.open_process(
259+
[command, *args], env=env, stderr=errlog, cwd=cwd
260+
)
255261

256262
return process

0 commit comments

Comments
 (0)