Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(devbox): probe coder before touching tailscale (#111907)

Raúl Negrón-Oterof3e6123baea6
diff --git a/tools/hogli-commands/hogli_commands/devbox/cli.py b/tools/hogli-commands/hogli_commands/devbox/cli.py
index 74af008aba91..73463038ada2 100644
--- a/tools/hogli-commands/hogli_commands/devbox/cli.py
+++ b/tools/hogli-commands/hogli_commands/devbox/cli.py
@@ -52,8 +52,6 @@ from .coder import (
ensure_coder_installed,
ensure_coder_reachable,
ensure_runtime_ready,
- ensure_tailscale_connected,
- ensure_tailscale_routes_accepted,
exec_replace,
extract_workspace_label,
get_coder_url,
@@ -1237,9 +1235,7 @@ def devbox_setup(
)
click.echo(click.style("Configuring devbox CLI access...", bold=True))
- ensure_tailscale_connected("rerun `hogli devbox:setup`.")
- ensure_tailscale_routes_accepted()
- ensure_coder_reachable()
+ ensure_coder_reachable("rerun `hogli devbox:setup`.")
ensure_coder_installed(verbose=verbose)
ensure_coder_authenticated()
diff --git a/tools/hogli-commands/hogli_commands/devbox/coder.py b/tools/hogli-commands/hogli_commands/devbox/coder.py
index 1be416af5fe3..70372e54325d 100644
--- a/tools/hogli-commands/hogli_commands/devbox/coder.py
+++ b/tools/hogli-commands/hogli_commands/devbox/coder.py
@@ -497,10 +497,13 @@ def _tailscale_routes_accepted() -> bool:
return not any(_ACCEPT_ROUTES_HEALTH_FRAGMENT in (msg or "") for msg in health)
-def ensure_tailscale_routes_accepted() -> None:
- """Enable Tailscale subnet route acceptance when peers advertise routes."""
+def ensure_tailscale_routes_accepted() -> bool:
+ """Enable Tailscale subnet route acceptance when peers advertise routes.
+
+ Returns whether this call enabled it.
+ """
if _tailscale_routes_accepted():
- return
+ return False
# The fix below is silent, so without this the number of hosts that land
# here stays unknowable.
@@ -508,7 +511,7 @@ def ensure_tailscale_routes_accepted() -> None:
tailscale_path = _resolve_tailscale()
if not tailscale_path:
- return
+ return False
click.echo("Enabling Tailscale subnet routes (required for devbox access)...")
cmd = [tailscale_path, "set", "--accept-routes"]
@@ -522,16 +525,22 @@ def ensure_tailscale_routes_accepted() -> None:
f"Failed to enable Tailscale subnet routes. Run manually: {manual}",
cause="accept_routes_failed",
)
+ return True
-def coder_reachable(timeout: float = 5.0) -> bool:
- """Return whether the Coder deployment responds on /api/v2/buildinfo."""
+def _probe_coder(timeout: float = 5.0) -> requests.Response | None:
+ """Return the response from /api/v2/buildinfo, or None when no response arrives."""
coder_url = get_coder_url()
try:
- resp = requests.get(f"{coder_url}/api/v2/buildinfo", timeout=timeout)
+ return requests.get(f"{coder_url}/api/v2/buildinfo", timeout=timeout)
except requests.RequestException:
- return False
- return resp.ok
+ return None
+
+
+def coder_reachable(timeout: float = 5.0) -> bool:
+ """Return whether the Coder deployment responds on /api/v2/buildinfo."""
+ resp = _probe_coder(timeout)
+ return resp is not None and resp.ok
@dataclass(frozen=True)
@@ -705,7 +714,7 @@ def _diagnose_blocked_route(
)
-def ensure_coder_reachable() -> None:
+def ensure_coder_reachable(setup_hint: str = RUNTIME_SETUP_HINT) -> None:
"""Fail fast with a structured diagnosis when the Coder ALB is unreachable.
Tailscale reporting ``BackendState=Running`` with ``--accept-routes`` does
@@ -713,8 +722,19 @@ def ensure_coder_reachable() -> None:
and packets still blackhole. Probe the API directly, and on failure pick
the single most-likely cause + next step instead of dumping a list of
commands the engineer has to interpret themselves.
+
+ The Tailscale checks run only when the probe gets no response. A host
+ inside the Coder network, such as a devbox, reaches the API without
+ Tailscale, and accepting a subnet route that covers its own network sends
+ local traffic through the tailnet and disconnects its workspace agent. An
+ error response proves the network path works, so Tailscale is not the cause.
"""
- if coder_reachable():
+ resp = _probe_coder()
+ if resp is None:
+ ensure_tailscale_connected(setup_hint)
+ if ensure_tailscale_routes_accepted():
+ resp = _probe_coder()
+ if resp is not None and resp.ok:
return
diagnosis = _diagnose_unreachable_coder()
@@ -922,9 +942,7 @@ def ensure_coder_authenticated() -> None:
def ensure_runtime_ready() -> None:
- """Verify runtime prerequisites without mutating host setup."""
- ensure_tailscale_connected()
- ensure_tailscale_routes_accepted()
+ """Verify runtime prerequisites, failing with the setup hint instead of installing or logging in."""
ensure_coder_reachable()
if not coder_installed():
diff --git a/tools/hogli-commands/hogli_commands/tests/test_devbox.py b/tools/hogli-commands/hogli_commands/tests/test_devbox.py
index 877848c57cf0..e87b05e1c5e7 100644
--- a/tools/hogli-commands/hogli_commands/tests/test_devbox.py
+++ b/tools/hogli-commands/hogli_commands/tests/test_devbox.py
@@ -436,8 +436,6 @@ class TestCoderConfig:
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
- monkeypatch.setattr(coder, "ensure_tailscale_connected", lambda setup_hint=coder.RUNTIME_SETUP_HINT: None)
- monkeypatch.setattr(coder, "ensure_tailscale_routes_accepted", lambda: None)
monkeypatch.setattr(coder, "ensure_coder_reachable", lambda: None)
monkeypatch.setattr(coder, "coder_installed", lambda: False)
@@ -574,7 +572,9 @@ class TestCoderReachable:
recorded_properties: dict[str, object],
) -> None:
monkeypatch.setattr(coder, "get_coder_url", lambda: "https://coder.example.com")
- monkeypatch.setattr(coder, "coder_reachable", lambda: False)
+ monkeypatch.setattr(coder, "_probe_coder", lambda timeout=5.0: None)
+ monkeypatch.setattr(coder, "tailscale_connected", lambda: True)
+ monkeypatch.setattr(coder, "_tailscale_routes_accepted", lambda: True)
monkeypatch.setattr(
coder,
"_diagnose_unreachable_coder",
@@ -596,6 +596,56 @@ class TestCoderReachable:
assert "fact: one" in out
assert recorded_properties == {"devbox_failure_cause": "stubbed_code"}
+ @pytest.mark.parametrize(
+ "probe_statuses, expected_tailscale_commands, exits",
+ [
+ ([200], [], False),
+ ([None, 200], [["/usr/bin/tailscale", "set", "--accept-routes"]], False),
+ ([None, None], [["/usr/bin/tailscale", "set", "--accept-routes"]], True),
+ ([503], [], True),
+ ],
+ )
+ def test_ensure_coder_reachable_changes_tailscale_only_when_coder_does_not_respond(
+ self,
+ monkeypatch: pytest.MonkeyPatch,
+ probe_statuses: list[int | None],
+ expected_tailscale_commands: list[list[str]],
+ exits: bool,
+ ) -> None:
+ def response(status: int | None) -> coder.requests.Response | None:
+ if status is None:
+ return None
+ resp = coder.requests.Response()
+ resp.status_code = status
+ return resp
+
+ probes = iter(probe_statuses)
+ monkeypatch.setattr(coder, "_probe_coder", lambda timeout=5.0: response(next(probes)))
+ monkeypatch.setattr(
+ coder,
+ "_diagnose_unreachable_coder",
+ lambda: coder.CoderReachabilityDiagnosis(code="stubbed_code", cause="", next_step="", facts=[]),
+ )
+ monkeypatch.setattr(coder, "tailscale_connected", lambda: True)
+ monkeypatch.setattr(coder, "_tailscale_routes_accepted", lambda: False)
+ monkeypatch.setattr(coder, "_resolve_tailscale", lambda: "/usr/bin/tailscale")
+ monkeypatch.setattr(coder.os, "geteuid", lambda: 0)
+ commands: list[list[str]] = []
+
+ def fake_run(args: list[str], **kwargs: object) -> subprocess.CompletedProcess[str]:
+ commands.append(args)
+ return subprocess.CompletedProcess(args, 0)
+
+ monkeypatch.setattr(coder.subprocess, "run", fake_run)
+
+ if exits:
+ with pytest.raises(SystemExit):
+ coder.ensure_coder_reachable()
+ else:
+ coder.ensure_coder_reachable()
+
+ assert commands == expected_tailscale_commands
+
class TestDiagnoseUnreachableCoder:
"""Test the structured cause-and-next-step diagnosis for unreachable Coder."""
@@ -1413,9 +1463,7 @@ class TestDevboxCommands:
def test_devbox_setup_runs_explicit_setup_steps(self, monkeypatch: pytest.MonkeyPatch) -> None:
calls: list[str] = []
- monkeypatch.setattr(devbox_cli, "ensure_tailscale_connected", lambda setup_hint="": calls.append("tailscale"))
- monkeypatch.setattr(devbox_cli, "ensure_tailscale_routes_accepted", lambda: calls.append("routes"))
- monkeypatch.setattr(devbox_cli, "ensure_coder_reachable", lambda: calls.append("reachable"))
+ monkeypatch.setattr(devbox_cli, "ensure_coder_reachable", lambda setup_hint="": calls.append("reachable"))
monkeypatch.setattr(devbox_cli, "ensure_coder_installed", lambda **kw: calls.append("install"))
monkeypatch.setattr(devbox_cli, "ensure_coder_authenticated", lambda: calls.append("login"))
monkeypatch.setattr(devbox_cli, "list_user_secrets", lambda: [])
@@ -1466,8 +1514,6 @@ class TestDevboxCommands:
assert result.exit_code == 0
assert calls == [
- "tailscale",
- "routes",
"reachable",
"install",
"login",
@@ -1485,9 +1531,7 @@ class TestDevboxCommands:
) -> None:
captured: dict[str, object] = {}
- monkeypatch.setattr(devbox_cli, "ensure_tailscale_connected", lambda setup_hint="": None)
- monkeypatch.setattr(devbox_cli, "ensure_tailscale_routes_accepted", lambda: None)
- monkeypatch.setattr(devbox_cli, "ensure_coder_reachable", lambda: None)
+ monkeypatch.setattr(devbox_cli, "ensure_coder_reachable", lambda setup_hint="": None)
monkeypatch.setattr(devbox_cli, "ensure_coder_installed", lambda **kw: None)
monkeypatch.setattr(devbox_cli, "ensure_coder_authenticated", lambda: None)
monkeypatch.setattr(devbox_cli, "_resolve_local_identity_agent_for_coder", lambda: "/tmp/resolved.sock")
@@ -1516,9 +1560,7 @@ class TestDevboxCommands:
monkeypatch: pytest.MonkeyPatch,
devbox_config_path: Path,
) -> None:
- monkeypatch.setattr(devbox_cli, "ensure_tailscale_connected", lambda setup_hint="": None)
- monkeypatch.setattr(devbox_cli, "ensure_tailscale_routes_accepted", lambda: None)
- monkeypatch.setattr(devbox_cli, "ensure_coder_reachable", lambda: None)
+ monkeypatch.setattr(devbox_cli, "ensure_coder_reachable", lambda setup_hint="": None)
monkeypatch.setattr(devbox_cli, "ensure_coder_installed", lambda **kw: None)
monkeypatch.setattr(devbox_cli, "ensure_coder_authenticated", lambda: None)
monkeypatch.setattr(devbox_cli, "_resolve_local_identity_agent_for_coder", lambda: None)
@@ -1552,9 +1594,7 @@ class TestDevboxCommands:
) -> None:
devbox_config_path.write_text(json.dumps({"git_name": "Existing User", "git_email": "[email protected]"}))
- monkeypatch.setattr(devbox_cli, "ensure_tailscale_connected", lambda setup_hint="": None)
- monkeypatch.setattr(devbox_cli, "ensure_tailscale_routes_accepted", lambda: None)
- monkeypatch.setattr(devbox_cli, "ensure_coder_reachable", lambda: None)
+ monkeypatch.setattr(devbox_cli, "ensure_coder_reachable", lambda setup_hint="": None)
monkeypatch.setattr(devbox_cli, "ensure_coder_installed", lambda **kw: None)
monkeypatch.setattr(devbox_cli, "ensure_coder_authenticated", lambda: None)
monkeypatch.setattr(devbox_cli, "_resolve_local_identity_agent_for_coder", lambda: None)
@@ -2054,12 +2094,7 @@ class TestDevboxCommands:
@pytest.fixture
def stub_setup_environment(monkeypatch: pytest.MonkeyPatch) -> None:
"""No-op every external dependency in ``devbox_setup`` so reset/gate tests can run hermetically."""
- for name in (
- "ensure_tailscale_connected",
- "ensure_tailscale_routes_accepted",
- "ensure_coder_reachable",
- "ensure_coder_authenticated",
- ):
+ for name in ("ensure_coder_reachable", "ensure_coder_authenticated"):
monkeypatch.setattr(devbox_cli, name, lambda *a, **kw: None)
monkeypatch.setattr(devbox_cli, "ensure_coder_installed", lambda **kw: None)
monkeypatch.setattr(devbox_cli, "_resolve_local_identity_agent_for_coder", lambda: None)