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(temporal): fail fast when test worker hangs on startup (#61875)

Raúl Negrón-Otero895f08aa00eb
diff --git a/posthog/temporal/common/test_threaded_worker.py b/posthog/temporal/common/test_threaded_worker.py
new file mode 100644
index 000000000000..94a4cda0c866
--- /dev/null
+++ b/posthog/temporal/common/test_threaded_worker.py
@@ -0,0 +1,46 @@
+import time
+import asyncio
+
+import pytest
+
+from posthog.temporal.common.test_utils import ThreadedWorker
+
+
+class _FakeWorker(ThreadedWorker):
+ def __init__(self) -> None:
+ # Match the base Worker's asyncio.Event type; only .set()/.is_set() are used (both sync).
+ self._shutdown_event = asyncio.Event()
+
+ @property
+ def is_running(self) -> bool:
+ return False
+
+
+class _WorkerDiesOnStartup(_FakeWorker):
+ def run_using_loop(self, loop) -> None:
+ raise RuntimeError("simulated worker boot failure")
+
+
+class _WorkerNeverReady(_FakeWorker):
+ def run_using_loop(self, loop) -> None:
+ while not self._shutdown_event.is_set():
+ time.sleep(0.05)
+
+
+def test_run_in_thread_surfaces_worker_startup_failure():
+ with pytest.raises(RuntimeError, match="failed to start") as exc_info:
+ with _WorkerDiesOnStartup().run_in_thread():
+ pass
+
+ assert isinstance(exc_info.value.__cause__, RuntimeError)
+ assert "simulated worker boot failure" in str(exc_info.value.__cause__)
+
+
+def test_run_in_thread_times_out_when_worker_never_starts():
+ started = time.monotonic()
+
+ with pytest.raises(TimeoutError, match="did not start within"):
+ with _WorkerNeverReady().run_in_thread(startup_timeout=0.5):
+ pass
+
+ assert time.monotonic() - started < 5
diff --git a/posthog/temporal/common/test_utils.py b/posthog/temporal/common/test_utils.py
index 339f7d359de2..917ae6984f36 100644
--- a/posthog/temporal/common/test_utils.py
+++ b/posthog/temporal/common/test_utils.py
@@ -17,17 +17,40 @@ class ThreadedWorker(Worker):
"""
@contextmanager
- def run_in_thread(self):
+ def run_in_thread(self, *, startup_timeout: float = 30.0):
"""Run a Temporal Worker in a thread.
Don't use this outside of tests. Once PostHog is fully async we can get rid of this.
"""
loop = asyncio.new_event_loop()
- t = threading.Thread(target=self.run_using_loop, daemon=True, args=(loop,))
+ startup_error: list[Exception] = []
+
+ def run() -> None:
+ try:
+ self.run_using_loop(loop)
+ except Exception as e:
+ # Capture so the main thread can fail fast on a startup crash, and re-raise so a
+ # worker that dies *after* startup still surfaces its traceback (threading.excepthook).
+ startup_error.append(e)
+ raise
+
+ t = threading.Thread(target=run, daemon=True)
t.start()
try:
+ # Bound the wait for the worker to come up. If the worker thread dies or
+ # never reports running, fail fast with a clear error — a silent spin here
+ # would otherwise consume the entire CI job timeout.
+ deadline = time.monotonic() + startup_timeout
while not self.is_running:
+ if not t.is_alive() or startup_error:
+ # A crashing worker appends to startup_error before its thread exits, so
+ # surface that cause rather than a bare "exited before it started" message.
+ if startup_error:
+ raise RuntimeError("Temporal test worker failed to start") from startup_error[0]
+ raise RuntimeError("Temporal test worker thread exited before it started running")
+ if time.monotonic() >= deadline:
+ raise TimeoutError(f"Temporal test worker did not start within {startup_timeout:g}s")
time.sleep(0.1)
yield
finally: