Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

Fix #2222: [Bug] Telemetry flusher causes console window to flash on Windows during VS Code index updates (#2380)

P A M O Def0b8dfc81cd
diff --git a/packages/cli/src/repowise/cli/platform/telemetry/emitter.py b/packages/cli/src/repowise/cli/platform/telemetry/emitter.py
index c89d584a9..d17c04183 100644
--- a/packages/cli/src/repowise/cli/platform/telemetry/emitter.py
+++ b/packages/cli/src/repowise/cli/platform/telemetry/emitter.py
@@ -97,6 +97,22 @@ def record(event: TelemetryEvent) -> None:
return
+def _flusher_executable() -> str:
+ """Return the executable for the detached flusher.
+
+ On Windows (``os.name == "nt"``) prefer a ``pythonw.exe`` sibling next
+ to ``sys.executable`` (windowless GUI subsystem, never allocates a
+ console) when it exists as a file; otherwise fall back to
+ ``sys.executable``.
+ """
+ if os.name == "nt" and sys.executable:
+ candidate = os.path.join(os.path.dirname(sys.executable), "pythonw.exe")
+ with contextlib.suppress(Exception):
+ if os.path.isfile(candidate):
+ return candidate
+ return sys.executable or ""
+
+
def _spawn_flusher() -> bool:
"""Start the detached delivery process. Returns whether it started.
@@ -106,7 +122,10 @@ def _spawn_flusher() -> bool:
"""
if not sys.executable:
return False
- spawn_detached([sys.executable, "-m", _FLUSHER_MODULE], os.getcwd())
+ try:
+ spawn_detached([_flusher_executable(), "-m", _FLUSHER_MODULE], os.getcwd())
+ except Exception:
+ return False
return True
diff --git a/packages/cli/src/repowise/cli/spawn.py b/packages/cli/src/repowise/cli/spawn.py
index 6f11fde6a..ed25b7d84 100644
--- a/packages/cli/src/repowise/cli/spawn.py
+++ b/packages/cli/src/repowise/cli/spawn.py
@@ -6,9 +6,11 @@ import os
import subprocess
from typing import IO
-#: DETACHED_PROCESS | CREATE_NO_WINDOW: no console window flashes up in front
-#: of the user.
-_WINDOWS_FLAGS = 0x00000008 | 0x08000000
+#: CREATE_NO_WINDOW alone: a hidden console that the child and its own console
+#: children (a venv python.exe trampoline's real interpreter, git) inherit.
+#: Never add DETACHED_PROCESS: Windows then ignores CREATE_NO_WINDOW and gives
+#: each console grandchild a new visible window (the flash in #2222).
+_WINDOWS_FLAGS = 0x08000000
#: Leave the parent's job, so a child outlives a host that closes its job on
#: exit. A job that forbids breakaway fails CreateProcess with access denied,
#: and the spawn is retried inside the job.
diff --git a/tests/unit/cli/test_telemetry.py b/tests/unit/cli/test_telemetry.py
index 07f6cd68f..36aa9ba56 100644
--- a/tests/unit/cli/test_telemetry.py
+++ b/tests/unit/cli/test_telemetry.py
@@ -8,6 +8,8 @@ central command wrapper recording exactly one event without breaking commands.
from __future__ import annotations
+import subprocess
+import sys
from pathlib import Path
import pytest
@@ -483,3 +485,117 @@ class TestBucketCount:
assert telemetry.drain_command_outcome() == {"a": 1}
# A second drain is empty — the field belongs to one invocation only.
assert telemetry.drain_command_outcome() == {}
+
+
+class TestFlusherExecutable:
+ """Tests for _flusher_executable() executable selection logic."""
+
+ def test_windows_prefers_pythonw_when_sibling_exists(
+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+ ):
+ """On Windows, prefer pythonw.exe next to sys.executable when it exists."""
+ monkeypatch.setattr("os.name", "nt")
+ fake_python = tmp_path / "python.exe"
+ fake_pythonw = tmp_path / "pythonw.exe"
+ fake_python.write_text("")
+ fake_pythonw.write_text("")
+ monkeypatch.setattr(sys, "executable", str(fake_python))
+
+ assert emitter._flusher_executable() == str(fake_pythonw)
+
+ def test_windows_falls_back_without_pythonw_sibling(
+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+ ):
+ """On Windows, fall back to sys.executable when no pythonw.exe sibling exists."""
+ monkeypatch.setattr("os.name", "nt")
+ fake_python = tmp_path / "python.exe"
+ fake_python.write_text("")
+ monkeypatch.setattr(sys, "executable", str(fake_python))
+
+ assert emitter._flusher_executable() == str(fake_python)
+
+ def test_posix_returns_sys_executable(self, monkeypatch: pytest.MonkeyPatch):
+ """On POSIX, always return sys.executable."""
+ monkeypatch.setattr("os.name", "posix")
+ monkeypatch.setattr(sys, "executable", "/usr/bin/python3")
+
+ assert emitter._flusher_executable() == "/usr/bin/python3"
+
+ def test_posix_ignores_pythonw_sibling(
+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+ ):
+ """On POSIX, even if pythonw.exe exists, return sys.executable."""
+ monkeypatch.setattr("os.name", "posix")
+ fake_python = tmp_path / "python3"
+ fake_python.write_text("")
+ fake_pythonw = tmp_path / "pythonw.exe"
+ fake_pythonw.write_text("")
+ monkeypatch.setattr(sys, "executable", str(fake_python))
+
+ assert emitter._flusher_executable() == str(fake_python)
+
+
+class TestSpawnFlusherFlags:
+ """Tests for _spawn_flusher() subprocess creation flags."""
+
+ def test_windows_spawns_pythonw_with_create_no_window_alone(
+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+ ):
+ """On Windows, the flusher gets CREATE_NO_WINDOW without DETACHED_PROCESS."""
+ monkeypatch.setattr("os.name", "nt")
+ fake_python = tmp_path / "python.exe"
+ fake_pythonw = tmp_path / "pythonw.exe"
+ fake_python.write_text("")
+ fake_pythonw.write_text("")
+ monkeypatch.setattr(sys, "executable", str(fake_python))
+
+ calls: list[tuple[list[str], dict]] = []
+ monkeypatch.setattr(subprocess, "Popen", lambda args, **kw: calls.append((args, kw)))
+
+ assert emitter._spawn_flusher() is True
+ (args, kwargs), = calls
+ assert args[0] == str(fake_pythonw)
+ # CREATE_NO_WINDOW | CREATE_BREAKAWAY_FROM_JOB; DETACHED_PROCESS (0x8) would disable it.
+ assert kwargs["creationflags"] == 0x08000000 | 0x01000000
+ assert not kwargs["creationflags"] & 0x00000008
+
+ def test_posix_uses_start_new_session(
+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+ ):
+ """On POSIX, subprocess.Popen receives start_new_session=True."""
+ monkeypatch.setattr("os.name", "posix")
+ monkeypatch.setattr(sys, "executable", "/usr/bin/python3")
+
+ captured_kwargs: dict = {}
+
+ class FakePopen:
+ def __init__(self, args, **kwargs):
+ captured_kwargs.update(kwargs)
+
+ monkeypatch.setattr(subprocess, "Popen", FakePopen)
+
+ result = emitter._spawn_flusher()
+
+ assert result is True
+ assert captured_kwargs.get("start_new_session") is True
+ assert "creationflags" not in captured_kwargs
+ assert "startupinfo" not in captured_kwargs
+
+ def test_popen_failure_returns_false(self, monkeypatch: pytest.MonkeyPatch):
+ """If subprocess.Popen raises, _spawn_flusher returns False silently."""
+ monkeypatch.setattr("os.name", "posix")
+ monkeypatch.setattr(sys, "executable", "/usr/bin/python3")
+
+ def raise_popen(*args, **kwargs):
+ raise OSError("spawn failed")
+
+ monkeypatch.setattr(subprocess, "Popen", raise_popen)
+
+ result = emitter._spawn_flusher()
+ assert result is False
+
+ def test_empty_executable_returns_false(self, monkeypatch: pytest.MonkeyPatch):
+ """If sys.executable is empty, _spawn_flusher returns False."""
+ monkeypatch.setattr(sys, "executable", "")
+ result = emitter._spawn_flusher()
+ assert result is False