: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-warehouse): dump thread stacks when the loader watchdog trips (#124021)
Tom Owersb1719666b110
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.pyindex 6e6b26e0db39..c1697a7dddd9 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/postgres_queue/test_consumer.py@@ -1,3 +1,4 @@+import sysimport mathimport timeimport asyncio@@ -5315,6 +5316,29 @@ class TestBatchPhaseTracking:consumer._report_health()assert BATCHES_IN_PHASE.labels(phase="write")._value.get() == 0+ @pytest.mark.parametrize(+ "ticks, expected_dumps",+ [+ pytest.param(["a"], 1, id="first_trip_dumps"),+ pytest.param(["a", "a", "a"], 1, id="same_batch_dumps_once"),+ pytest.param(["a", "b"], 2, id="different_batch_dumps_again"),+ ],+ )+ def test_watchdog_trip_dumps_thread_stacks_once_per_stuck_batch(+ self, ticks: list[str], expected_dumps: int+ ) -> None:+ consumer = _make_consumer(stuck_batch_timeout_seconds=0.0)+ consumer._health_reporter = lambda: None++ with patch.object(batch_consumer_module.faulthandler, "dump_traceback") as dump:+ for batch_id in ticks:+ consumer._inflight_started = {batch_id: time.monotonic() - 10}+ consumer._report_health()++ assert dump.call_count == expected_dumps+ for call in dump.call_args_list:+ assert call.kwargs == {"file": sys.stderr, "all_threads": True}+def test_watchdog_trip_names_the_phase_the_batch_stopped_in(self) -> None:from products.warehouse_sources.backend.temporal.data_imports.workload_report import report_phase@@ -5328,8 +5352,9 @@ class TestBatchPhaseTracking:consumer._report_health()assert calls == [] # tripped: liveness withheld- logger.error.assert_called_once()- kwargs = logger.error.call_args.kwargs+ tripped = [c for c in logger.error.call_args_list if "stuck_batch_watchdog_tripped" in c.args[0]]+ assert len(tripped) == 1+ kwargs = tripped[0].kwargsassert kwargs.get("batch_id") == batch.idassert kwargs.get("phase") == "write"assert kwargs.get("phase_seconds") is not Nonediff --git a/products/warehouse_sources_queue/backend/core/batch_consumer.py b/products/warehouse_sources_queue/backend/core/batch_consumer.pyindex 329554b197f7..3d918b43fec8 100644--- a/products/warehouse_sources_queue/backend/core/batch_consumer.py+++ b/products/warehouse_sources_queue/backend/core/batch_consumer.py@@ -1,5 +1,6 @@from __future__ import annotations+import sysimport timeimport randomimport signal@@ -580,6 +581,7 @@ class BatchConsumer:# batch_id -> the attempt this consumer wrote as 'executing', for the shutdown marker.self._executing: dict[str, ExecutingAttempt] = {}self._last_stuck_log_monotonic = 0.0+ self._stack_dumped_batch_id: str | None = None# Consecutive failed polls, for the poll-failure liveness trip.self._consecutive_poll_failures = 0self._last_poll_failure_log_monotonic = 0.0@@ -2011,10 +2013,16 @@ class BatchConsumer:now = time.monotonic()oldest_id, oldest_started = min(self._inflight_started.items(), key=lambda kv: kv[1])if now - oldest_started > timeout:+ progress = self._inflight_progress.get(oldest_id)+ phase, phase_seconds = progress.snapshot() if progress is not None else ("unknown", 0.0)+ if self._stack_dumped_batch_id != oldest_id:+ # Worker threads can hang in native code while the event loop stays alive, so the+ # timer-based stall dump never fires. Dump once per stuck batch before the pod restarts.+ self._stack_dumped_batch_id = oldest_id+ logger.error(self._event("stuck_batch_stack_dump"), batch_id=oldest_id, phase=phase)+ faulthandler.dump_traceback(file=sys.stderr, all_threads=True)if now - self._last_stuck_log_monotonic > 60:self._last_stuck_log_monotonic = now- progress = self._inflight_progress.get(oldest_id)- phase, phase_seconds = progress.snapshot() if progress is not None else ("unknown", 0.0)logger.error(self._event("stuck_batch_watchdog_tripped"),batch_id=oldest_id,