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.

chore(observability): run web memory sampler under ASGI (was wsgi-only) (#66249)

Julian Bez3a99d80cfa29
diff --git a/posthog/asgi.py b/posthog/asgi.py
index d3a2f5fcda7f..62a966f009ab 100644
--- a/posthog/asgi.py
+++ b/posthog/asgi.py
@@ -17,9 +17,9 @@ os.environ["SERVER_GATEWAY_INTERFACE"] = "ASGI" # Set definitively
# Get a structlog logger for asgi.py's own messages
logger = structlog.get_logger(__name__)
-# NOTE: OTel and continuous profiling init is deferred to first request via
-# _ensure_post_fork_init() below. Both start background threads (OTel's
-# BatchSpanProcessor, Pyroscope's native profiler) that cannot survive
+# NOTE: OTel, continuous profiling, and the web-memory sampler init are deferred to first
+# request via _ensure_post_fork_init() below. They start background threads (OTel's
+# BatchSpanProcessor, Pyroscope's native profiler, the sampler's loop) that cannot survive
# fork(). Nginx Unit loads this module in a "prototype" process and then
# forks workers from it — the forked children inherit dead thread state and
# corrupted mutexes, causing SIGSEGV / SIGABRT on the worker. Deferring to
@@ -37,10 +37,14 @@ def _ensure_post_fork_init():
from posthog.caching.redis_cluster_connection_factory import prewarm_query_cache_cluster_in_background
from posthog.continuous_profiling import start_continuous_profiling
from posthog.otel_instrumentation import initialize_otel
+ from posthog.web_memory_sampler import start_web_memory_sampler
start_continuous_profiling()
initialize_otel()
prewarm_query_cache_cluster_in_background()
+ # Production runs ASGI, so the sampler — previously only started from wsgi.py — never
+ # ran in prod, leaving posthog_web_worker_rss_mb empty. Start it here, post-fork.
+ start_web_memory_sampler()
_post_fork_initialized = True