Fix GH #153: set_context hangs with stdio transport

Move blocking subprocess.run() call off event loop using asyncio.to_thread()
with timeout to prevent deadlock when using stdio transport.

- Wrap _resolve_workspace_root() in asyncio.to_thread() with 2s timeout
- Add fallback to os.path.abspath() on timeout
- Ensure logging uses stderr to avoid stdio protocol pollution

Amp-Thread-ID: https://ampcode.com/threads/T-fc335bda-5fca-4daa-bdc1-5d53d0eb818f
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-11-02 11:02:42 -08:00
parent 01160082fa
commit 8304ca7379

View File

@@ -34,6 +34,7 @@ logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
stream=sys.stderr, # Ensure logs don't pollute stdio protocol
)
T = TypeVar("T")
@@ -228,8 +229,15 @@ async def set_context(workspace_root: str) -> str:
Returns:
Confirmation message with resolved paths
"""
# Resolve to git repo root if possible
resolved_root = _resolve_workspace_root(workspace_root)
# Resolve to git repo root if possible (run in thread to avoid blocking event loop)
try:
resolved_root = await asyncio.wait_for(
asyncio.to_thread(_resolve_workspace_root, workspace_root),
timeout=2.0,
)
except asyncio.TimeoutError:
logger.warning(f"Git detection timed out, using provided path: {workspace_root}")
resolved_root = os.path.abspath(workspace_root)
# Always set working directory and context flag
os.environ["BEADS_WORKING_DIR"] = resolved_root