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(observability): log streaming responses that hold DB connections open (#66243)

Sam Penningtonba4cf9b20b5e
diff --git a/posthog/middleware.py b/posthog/middleware.py
index 95f013c9ed23..829d8d672752 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -15,7 +15,10 @@ from django.conf import settings
from django.contrib.auth import BACKEND_SESSION_KEY, logout
from django.core.cache import cache
from django.core.exceptions import MiddlewareNotUsed
-from django.db import connection
+from django.db import (
+ connection,
+ connections as db_connections,
+)
from django.db.models import QuerySet
from django.http import HttpRequest, HttpResponse, JsonResponse
from django.http.response import HttpResponseRedirectBase
@@ -750,6 +753,7 @@ def per_request_logging_context_middleware(
if mcp_conversation_id:
span.set_attribute("mcp.conversation_id", mcp_conversation_id)
+ response: HttpResponse | None = None
try:
response = get_response(request)
finally:
@@ -775,6 +779,30 @@ def per_request_logging_context_middleware(
method=request.method,
worker_pid=os.getpid(),
)
+ # Detect streaming responses that are holding DB connections open.
+ # Under ASGI, a StreamingHttpResponse keeps the request alive until
+ # the stream ends — any connection still open at this point stays
+ # pinned to a pgbouncer slot for the entire stream duration, which
+ # can be minutes for SSE endpoints (AI chat, dashboard tiles, etc.).
+ # Endpoints that use sse_streaming_response() release connections
+ # before returning, so this only fires for ones that don't.
+ if response is not None and getattr(response, "streaming", False):
+ held = [
+ conn.alias
+ for conn in db_connections.all(initialized_only=True)
+ if conn.connection is not None and not conn.in_atomic_block
+ ]
+ if held:
+ _user = getattr(request, "user", None)
+ logger.warning(
+ "stream_with_held_connections",
+ held_connections=len(held),
+ aliases=held,
+ request_path=request.path,
+ method=request.method,
+ worker_pid=os.getpid(),
+ team_id=_user.current_team_id if _user is not None and _user.is_authenticated else None,
+ )
return response