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.

feat(data-warehouse): dump thread stacks when the load consumer stalls (#117306)

Daniel RC8d494698fe43
diff --git a/products/warehouse_sources/backend/management/commands/run_warehouse_sources_load.py b/products/warehouse_sources/backend/management/commands/run_warehouse_sources_load.py
index 5b10e90cdb15..bc44bc87cf55 100644
--- a/products/warehouse_sources/backend/management/commands/run_warehouse_sources_load.py
+++ b/products/warehouse_sources/backend/management/commands/run_warehouse_sources_load.py
@@ -58,6 +58,7 @@ def build_consumer_config(options: dict) -> ConsumerConfig:
"max_attempts": options["max_attempts"],
"health_port": options["health_port"],
"health_timeout_seconds": options["health_timeout"],
+ "stall_dump_after_seconds": options["health_timeout"],
"stuck_batch_timeout_seconds": options["stuck_batch_timeout"] or None,
}
if options.get("poll_timeout") is not None:
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.py
index 4d9b522e61cc..4d208067ab8d 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
@@ -3825,6 +3825,22 @@ class TestHeartbeatLoop:
await asyncio.sleep(0.2)
assert health.is_healthy() is False
+ @pytest.mark.asyncio
+ async def test_stalled_heartbeat_dumps_stacks_but_shutdown_does_not(self, capfd):
+ consumer = _make_consumer(heartbeat_interval_seconds=0.01, stall_dump_after_seconds=0.1)
+ consumer._health_reporter = lambda: None
+
+ task = asyncio.create_task(consumer._heartbeat_loop())
+ await asyncio.sleep(0.03)
+ time.sleep(1.0) # block the event loop so the heartbeat cannot run
+ assert "most recent call first" in capfd.readouterr().err
+
+ await asyncio.sleep(0.03) # the heartbeat resumes and arms the timer again
+ consumer._shutdown.set()
+ await asyncio.wait_for(task, timeout=1.0)
+ await asyncio.sleep(0.3)
+ assert "most recent call first" not in capfd.readouterr().err
+
class TestOwnershipVerification:
@pytest.mark.asyncio
diff --git a/products/warehouse_sources_queue/backend/core/batch_consumer.py b/products/warehouse_sources_queue/backend/core/batch_consumer.py
index 90d123a779ec..7a693bb14a07 100644
--- a/products/warehouse_sources_queue/backend/core/batch_consumer.py
+++ b/products/warehouse_sources_queue/backend/core/batch_consumer.py
@@ -5,6 +5,7 @@ import random
import signal
import asyncio
import contextlib
+import faulthandler
from collections import defaultdict
from collections.abc import Callable, Coroutine, Iterator
from dataclasses import dataclass, replace
@@ -307,6 +308,10 @@ class BatchConsumerConfig:
# Withhold liveness after this many consecutive failed polls: a pod that
# cannot poll does no work but would otherwise pass liveness forever.
poll_failure_liveness_threshold: int | None = 10
+ # When set, all thread stacks are written to stderr once the heartbeat loop has not run for
+ # this many seconds. faulthandler writes them from a C thread, so the dump also works when a
+ # native call holds the GIL and every Python thread, logging included, is frozen.
+ stall_dump_after_seconds: float | None = None
def __post_init__(self) -> None:
if self.recovery_grace_seconds is None:
@@ -1827,12 +1832,20 @@ class BatchConsumer:
A poll cycle can run far longer than the health timeout (e.g. large final-batch
compaction), so the main loop's per-poll health report is not enough on its own.
"""
- while not self._shutdown.is_set():
- self._report_health()
- try:
- await asyncio.wait_for(self._shutdown.wait(), timeout=self._config.heartbeat_interval_seconds)
- except TimeoutError:
- pass
+ stall_dump_after = self._config.stall_dump_after_seconds
+ try:
+ while not self._shutdown.is_set():
+ if stall_dump_after is not None:
+ faulthandler.dump_traceback_later(stall_dump_after)
+ self._report_health()
+ try:
+ await asyncio.wait_for(self._shutdown.wait(), timeout=self._config.heartbeat_interval_seconds)
+ except TimeoutError:
+ pass
+ finally:
+ # The shutdown drain sends no heartbeats, so a timer left armed would dump stacks on every long drain.
+ if stall_dump_after is not None:
+ faulthandler.cancel_dump_traceback_later()
async def _recovery_sweep(self, *, should_abort: Callable[[], bool] | None = None) -> None:
grace_seconds = self._config.recovery_grace_seconds