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(tasks): stop reporting a timed-out egress probe as a policy block (#98058)

Charles Vien3f6498259cfe
diff --git a/products/tasks/backend/logic/services/agent_server_launcher.py b/products/tasks/backend/logic/services/agent_server_launcher.py
index ac4c55309000..f2402c981ff2 100644
--- a/products/tasks/backend/logic/services/agent_server_launcher.py
+++ b/products/tasks/backend/logic/services/agent_server_launcher.py
@@ -8,6 +8,7 @@ inherit it unchanged.
from __future__ import annotations
+import re
import json
import time
import shlex
@@ -79,6 +80,11 @@ HOST_PRESSURE_PROBE_SCRIPT = (
'else echo "cold_read_ms=unavailable file=${probe_file:-none} size=$probe_size"; fi'
)
+EGRESS_PROBE_MAX_TIME_SECONDS = 3
+# curl(1): "Operation timeout. The specified time-out period was reached according to the conditions."
+CURL_EXIT_OPERATION_TIMEOUT = 28
+CURL_EXIT_PATTERN = re.compile(r"curl_exit=(\d+)$")
+
SESSION_INIT_PROBE_HOSTS = (
"gateway.us.posthog.com",
"gateway.eu.posthog.com",
@@ -103,6 +109,54 @@ def _session_init_probe_hosts() -> list[str]:
return hosts
+def _curl_exit_code(line: str) -> int | None:
+ """Read the exit code the probe appends, or None for output from an image that predates it."""
+ match = CURL_EXIT_PATTERN.search(line)
+ return int(match.group(1)) if match else None
+
+
+def _egress_failure_reason(egress: str) -> str | None:
+ """Name the failure the egress probe saw, or None when every host answered.
+
+ A refused connection (curl exit 7) or a failed lookup (exit 6) proves a network policy block.
+ A timeout (exit 28) proves nothing on its own: a slow sandbox and a policy that drops packets
+ silently both look like one. So a timeout is reported as a timeout, and the reader is sent to
+ the host-pressure probe in the same diagnostics rather than to the allowlist. A nonzero exit
+ with no HTTP code means curl never ran or was killed, so that host was never probed at all.
+ """
+ failed: list[str] = []
+ blocked: list[str] = []
+ unprobed: list[str] = []
+ for line in egress.splitlines():
+ exit_code = _curl_exit_code(line)
+ if exit_code == 0:
+ continue
+ curl_reported_no_response = "http_code=000" in line or line.endswith("FAILED")
+ if not curl_reported_no_response and (exit_code is None or "http_code=" in line):
+ continue
+ failed.append(line)
+ if exit_code == CURL_EXIT_OPERATION_TIMEOUT:
+ continue
+ if curl_reported_no_response:
+ blocked.append(line)
+ else:
+ unprobed.append(line)
+ if not failed:
+ return None
+ if blocked:
+ return "egress blocked to required session-init host(s): " + "; ".join(failed)
+ if unprobed:
+ return (
+ "egress probe did not run for required session-init host(s); curl exited without an HTTP code, "
+ "so nothing here rules an allowlist block in or out: " + "; ".join(failed)
+ )
+ return (
+ f"egress probe timed out after {EGRESS_PROBE_MAX_TIME_SECONDS}s to every session-init host it could "
+ "not reach, and none refused the connection; read the host-pressure probe before the allowlist, "
+ "because a starved sandbox and a policy that drops packets silently both time out: " + "; ".join(failed)
+ )
+
+
def _start_and_wait_command(command: str, max_attempts: int = AGENT_SERVER_HEALTH_MAX_ATTEMPTS) -> str:
health_command = build_health_check_command(AGENT_SERVER_PORT, max_attempts, pid_file="/tmp/agent-server.pid")
return (
@@ -282,9 +336,9 @@ class AgentServerLaunchMixin(SandboxBase):
egress = self._probe_session_init_egress()
diagnostics["egress_probe"] = egress
- blocked = [line for line in egress.splitlines() if "http_code=000" in line or line.endswith("FAILED")]
- if blocked:
- diagnostics["failure_reason"] = "egress blocked to required session-init host(s): " + "; ".join(blocked)
+ egress_reason = _egress_failure_reason(egress)
+ if egress_reason:
+ diagnostics["failure_reason"] = egress_reason
else:
diagnostics["failure_reason"] = (
"agent server alive but never reported hasSession=true; no egress block detected, "
@@ -313,7 +367,9 @@ class AgentServerLaunchMixin(SandboxBase):
hosts = _session_init_probe_hosts()
checks = "; ".join(
f"printf '%s ' {shlex.quote(host)}; "
- f"curl -sS --max-time 3 -o /dev/null -w 'http_code=%{{http_code}}\\n' https://{host}/ 2>/dev/null || echo FAILED"
+ f"curl -sS --max-time {EGRESS_PROBE_MAX_TIME_SECONDS} -o /dev/null -w 'http_code=%{{http_code}}' "
+ f"https://{host}/ 2>/dev/null; "
+ 'echo " curl_exit=$?"'
for host in hosts
)
return self.execute(checks, timeout_seconds=30).stdout.strip()
diff --git a/products/tasks/backend/logic/services/tests/test_modal_sandbox.py b/products/tasks/backend/logic/services/tests/test_modal_sandbox.py
index 2f07be3f61d3..320be9a282b5 100644
--- a/products/tasks/backend/logic/services/tests/test_modal_sandbox.py
+++ b/products/tasks/backend/logic/services/tests/test_modal_sandbox.py
@@ -35,6 +35,7 @@ from products.tasks.backend.logic.services.agent_server_launcher import (
AGENT_SERVER_HEALTH_MAX_ATTEMPTS,
HOST_PRESSURE_PROBE_SCRIPT,
STARTUP_LOG_MAX_BYTES,
+ _egress_failure_reason,
)
from products.tasks.backend.logic.services.local_packages import LocalPackage
from products.tasks.backend.logic.services.local_skills import ENV_DISABLE_BUNDLED_SKILLS
@@ -1390,18 +1391,50 @@ class TestStartupFailureDiagnostics:
assert "poll=137" in diagnostics["failure_reason"]
sandbox._sandbox.exec.assert_not_called()
+ @pytest.mark.parametrize(
+ "probe_stdout, expected, unexpected",
+ [
+ (
+ "api.anthropic.com http_code=200 curl_exit=0\nmcp-eu.posthog.com http_code=000 curl_exit=7",
+ "egress blocked",
+ "timed out",
+ ),
+ (
+ "api.anthropic.com http_code=200\nmcp-eu.posthog.com http_code=000\nFAILED",
+ "egress blocked",
+ "timed out",
+ ),
+ (
+ "api.anthropic.com http_code=000 curl_exit=28\nmcp-eu.posthog.com http_code=000 curl_exit=28",
+ "timed out",
+ "egress blocked",
+ ),
+ (
+ "api.anthropic.com http_code=000 curl_exit=28\nmcp-eu.posthog.com http_code=000 curl_exit=7",
+ "egress blocked",
+ "timed out",
+ ),
+ (
+ "api.anthropic.com http_code=200 curl_exit=0\nmcp-eu.posthog.com curl_exit=127",
+ "did not run",
+ "no egress block detected",
+ ),
+ ],
+ ids=[
+ "refused",
+ "legacy_image_without_curl_exit",
+ "every_host_timed_out",
+ "one_refused_one_timed_out",
+ "curl_never_ran",
+ ],
+ )
@override_settings(SITE_URL="https://eu.posthog.com", SANDBOX_MCP_URL=None)
- def test_reports_blocked_egress_host(self):
+ def test_reports_blocked_egress_host(self, probe_stdout: str, expected: str, unexpected: str):
sandbox = self._sandbox()
def _exec(command: str, timeout_seconds: Any = None) -> ExecutionResult:
if "http_code=" in command:
- return ExecutionResult(
- stdout="api.anthropic.com http_code=200\nmcp-eu.posthog.com http_code=000",
- stderr="",
- exit_code=0,
- error=None,
- )
+ return ExecutionResult(stdout=probe_stdout, stderr="", exit_code=0, error=None)
if "agent-server.log" in command:
return ExecutionResult(stdout="agent log tail", stderr="", exit_code=0, error=None)
return ExecutionResult(stdout='{"status":"ok","hasSession":false}', stderr="", exit_code=0, error=None)
@@ -1413,9 +1446,13 @@ class TestStartupFailureDiagnostics:
diagnostics = sandbox._diagnose_startup_failure(allowed_domains=["github.com"])
assert diagnostics["sandbox_terminated"] == "false"
- assert "egress blocked" in diagnostics["failure_reason"]
+ assert expected in diagnostics["failure_reason"]
+ assert unexpected not in diagnostics["failure_reason"]
assert "mcp-eu.posthog.com" in diagnostics["failure_reason"]
+ def test_transfer_error_after_a_response_is_not_an_egress_failure(self):
+ assert _egress_failure_reason("api.anthropic.com http_code=200 curl_exit=56") is None
+
def test_reports_alive_without_session_when_no_block(self):
sandbox = self._sandbox()