From ce51a91adffb36e5d269e002878adc160dea76aa Mon Sep 17 00:00:00 2001 From: "zz@hexems.com" Date: Thu, 13 Nov 2025 13:58:22 +0800 Subject: [PATCH] fix: defer CLI path lookup to async context to prevent blocking calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Move shutil.which() call from __init__ to connect() method - Use asyncio.to_thread() to run _find_cli() in background thread - Prevents blockbuster library from detecting os.access as blocking call - Maintains backward compatibility and minimal code changes Resolves issue where SDK would fail in async environments with blocking call detection libraries like blockbuster. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../_internal/transport/subprocess_cli.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/claude_agent_sdk/_internal/transport/subprocess_cli.py b/src/claude_agent_sdk/_internal/transport/subprocess_cli.py index d669a412..a0428b40 100644 --- a/src/claude_agent_sdk/_internal/transport/subprocess_cli.py +++ b/src/claude_agent_sdk/_internal/transport/subprocess_cli.py @@ -1,5 +1,6 @@ """Subprocess transport implementation using Claude Code CLI.""" +import asyncio import json import logging import os @@ -48,9 +49,7 @@ def __init__( self._prompt = prompt self._is_streaming = not isinstance(prompt, str) self._options = options - self._cli_path = ( - str(options.cli_path) if options.cli_path is not None else self._find_cli() - ) + self._cli_path = str(options.cli_path) if options.cli_path is not None else None self._cwd = str(options.cwd) if options.cwd else None self._process: Process | None = None self._stdout_stream: TextReceiveStream | None = None @@ -266,6 +265,10 @@ async def connect(self) -> None: if self._process: return + # Defer CLI path lookup to async context to avoid blocking calls + if self._cli_path is None: + self._cli_path = await asyncio.to_thread(self._find_cli) + if not os.environ.get("CLAUDE_AGENT_SDK_SKIP_VERSION_CHECK"): await self._check_claude_version()