Skip to content

The agent that grows with you

fix(tools): detach stdin in the Windows Git Bash probe (#78820)

Shimakaze514869432301b03
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
index da0dd6463eb..93868e8ae27 100644
--- a/contributors/emails/[email protected]
+++ b/contributors/emails/[email protected]
@@ -1,2 +1,2 @@
Shimakaze514
-# PR #104832 salvage
+# PR: detach stdin in Git Bash probe (#78820)
diff --git a/tests/tools/test_gitbash_probe_stdin.py b/tests/tools/test_gitbash_probe_stdin.py
new file mode 100644
index 00000000000..57533467a2c
--- /dev/null
+++ b/tests/tools/test_gitbash_probe_stdin.py
@@ -0,0 +1,47 @@
+"""The Windows Git Bash / ASLR probes must not inherit the caller's stdin (#78820).
+
+``local._find_bash`` runs these probes on the first ``LocalEnvironment`` of a
+process. Inside the TUI that process is the stdio gateway, whose stdin is the
+pipe tui-parent reads JSON-RPC from. A probe started with ``capture_output``
+alone inherits that pipe; the MSYS2 runtime then switches the shared pipe to
+``PIPE_NOWAIT`` and the gateway's next ``sys.stdin.readline()`` fails with
+``OSError: [Errno 22]``. Passing ``stdin=subprocess.DEVNULL`` keeps the
+gateway's pipe out of the child entirely.
+"""
+
+import subprocess
+
+import pytest
+
+from tools.environments import local_gitbash_probe as gitbash_probe
+
+
+def run_calls(monkeypatch):
+ """Record every ``subprocess.run`` kwargs dict instead of spawning."""
+ calls = []
+
+ def fake_run(argv, **kwargs):
+ calls.append(kwargs)
+ return subprocess.CompletedProcess(argv, 0, stdout="OFF\n", stderr="")
+
+ monkeypatch.setattr(gitbash_probe.subprocess, "run", fake_run)
+ return calls
+
+
+def test_bash_starts_detaches_stdin(run_calls):
+ gitbash_probe._bash_starts_cache.clear()
+ gitbash_probe._bash_probe_details_cache.clear()
+
+ assert gitbash_probe._bash_starts(r"C:\Git\bin\bash.exe") is True
+ assert len(run_calls) == 1
+ assert run_calls[0].get("stdin") is subprocess.DEVNULL
+
+
+def test_mandatory_aslr_probe_detaches_stdin(run_calls, monkeypatch):
+ monkeypatch.setattr(gitbash_probe, "_mandatory_aslr_enabled_cache", None)
+ monkeypatch.setattr(gitbash_probe.shutil, "which", lambda _name: "powershell.exe")
+
+ assert gitbash_probe._mandatory_aslr_enabled() is False
+ assert len(run_calls) == 1
+ assert run_calls[0].get("stdin") is subprocess.DEVNULL
diff --git a/tools/environments/local_gitbash_probe.py b/tools/environments/local_gitbash_probe.py
index 25c18a953cd..108d9792fd1 100644
--- a/tools/environments/local_gitbash_probe.py
+++ b/tools/environments/local_gitbash_probe.py
@@ -90,7 +90,11 @@ def _bash_starts(bash: str) -> bool:
result = subprocess.run(
[bash, "--noprofile", "--norc", "-c", _BASH_EXTERNAL_PROGRAM_PROBE],
capture_output=True, text=True, encoding="utf-8", errors="replace",
- timeout=15, creationflags=windows_hide_flags() if _IS_WINDOWS else 0)
+ timeout=15, creationflags=windows_hide_flags() if _IS_WINDOWS else 0,
+ # Detach stdin (#78820): with capture_output alone the MSYS bash
+ # inherits the TUI gateway's stdin pipe and flips it to PIPE_NOWAIT,
+ # so the gateway's next readline() raises OSError(EINVAL).
+ stdin=subprocess.DEVNULL)
ok = result.returncode == 0
if not ok:
combined = f"{result.stdout or ''}{result.stderr or ''}".strip()