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(streaming): release DB connections before SSE streams in summarize, mcp proxy, and observation progress (#66800)

Sam Pennington7433b1f7a715
diff --git a/posthog/session_recordings/session_recording_api.py b/posthog/session_recordings/session_recording_api.py
index daf17ce22526..f3733e3e7fb7 100644
--- a/posthog/session_recordings/session_recording_api.py
+++ b/posthog/session_recordings/session_recording_api.py
@@ -58,6 +58,7 @@ from posthog.schema import (
from posthog.api.person import MinimalPersonSerializer
from posthog.api.routing import TeamAndOrgViewSetMixin
+from posthog.api.streaming import _release_request_connections
from posthog.api.utils import ServerTimingsGathered, action, safe_clickhouse_string
from posthog.auth import (
ExportRendererAuthentication,
@@ -1672,6 +1673,7 @@ class SessionRecordingViewSet(
session_ids=[session_id],
video_based=True,
)
+ _release_request_connections()
response = StreamingHttpResponse(
self._generate_video_based_summary(
session_id, user, tracking_id, product_context, custom_tags, force_restart=force_restart
diff --git a/products/mcp_store/backend/proxy.py b/products/mcp_store/backend/proxy.py
index 73dd40ef65cc..0eeee0cfbd62 100644
--- a/products/mcp_store/backend/proxy.py
+++ b/products/mcp_store/backend/proxy.py
@@ -8,6 +8,7 @@ from django.http import HttpResponse, StreamingHttpResponse
import httpx
import structlog
+from posthog.api.streaming import _release_request_connections
from posthog.security.url_validation import is_url_allowed
from posthog.settings import SERVER_GATEWAY_INTERFACE
@@ -403,6 +404,7 @@ def proxy_mcp_request(request: Any, installation: MCPServerInstallation) -> Http
content_type = upstream_response.headers.get("content-type", "")
if "text/event-stream" in content_type:
+ _release_request_connections()
return _build_sse_response(upstream_response, client)
# Read body then close to avoid memory leaks from buffered responses
diff --git a/products/replay_vision/backend/api/observations.py b/products/replay_vision/backend/api/observations.py
index 9848a9313fbd..bcc045c35a88 100644
--- a/products/replay_vision/backend/api/observations.py
+++ b/products/replay_vision/backend/api/observations.py
@@ -20,6 +20,7 @@ from rest_framework.response import Response
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.shared import UserBasicSerializer
+from posthog.api.streaming import _release_request_connections
from posthog.renderers import ServerSentEventRenderer
from products.replay_vision.backend.api.filters import MultiChoiceFilter, OrderByFilter, ordering_enum
@@ -599,6 +600,7 @@ class SessionReplayObservationViewSet(ReplayObservationViewSet):
if getattr(settings, "SERVER_GATEWAY_INTERFACE", "ASGI") != "ASGI":
raise RuntimeError("observation progress stream requires ASGI.")
observation = self.get_object()
+ _release_request_connections()
response = StreamingHttpResponse(
stream_observation_progress(observation),
content_type=ServerSentEventRenderer.media_type,