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(data-warehouse): keep the database URL out of the partition job's frame locals (#94342)

Jovan Sakovicc9ba26d3c0a2
diff --git a/posthog/temporal/tests/warehouse_sources_queue_partition_management/test_activities.py b/posthog/temporal/tests/warehouse_sources_queue_partition_management/test_activities.py
index 6cca6bc78869..f45d5dbb46ed 100644
--- a/posthog/temporal/tests/warehouse_sources_queue_partition_management/test_activities.py
+++ b/posthog/temporal/tests/warehouse_sources_queue_partition_management/test_activities.py
@@ -4,11 +4,14 @@ from __future__ import annotations
from contextlib import contextmanager
from datetime import date
+from types import TracebackType
from typing import Any, Literal
import pytest
from unittest.mock import MagicMock, call, patch
+import psycopg
+
from posthog.temporal.warehouse_sources_queue_partition_management import activities as activities_module
from posthog.temporal.warehouse_sources_queue_partition_management.activities import (
RETENTION_STRANDED_ERROR,
@@ -519,3 +522,37 @@ async def test_activity_logs_s3_deleted_count(activity_environment) -> None:
]
assert len(completion_calls) == 1
assert completion_calls[0].kwargs["s3_deleted_count"] == 2
+
+
+def _connect_refused(*args: Any, **kwargs: Any) -> None:
+ del args, kwargs
+ raise psycopg.OperationalError("connection refused")
+
+
+def _frames_holding(tb: TracebackType | None, needle: str) -> list[str]:
+ hits: list[str] = []
+ while tb is not None:
+ if needle in repr(tb.tb_frame.f_locals):
+ hits.append(tb.tb_frame.f_code.co_qualname)
+ tb = tb.tb_next
+ return hits
+
+
+_SECRET = "invented-secret"
+
+
+async def test_connect_failure_keeps_database_url_out_of_traceback_locals() -> None:
+ with (
+ patch.object(
+ activities_module.settings,
+ "WAREHOUSE_SOURCES_DATABASE_URL",
+ f"postgres://alice:{_SECRET}@db.example.com/app",
+ ),
+ patch.object(activities_module.psycopg.Connection, "connect", _connect_refused),
+ pytest.raises(psycopg.OperationalError) as exc_info,
+ ):
+ await manage_warehouse_sources_queue_partitions()
+
+ hits = _frames_holding(exc_info.value.__traceback__, _SECRET)
+ assert hits == []
diff --git a/posthog/temporal/warehouse_sources_queue_partition_management/activities.py b/posthog/temporal/warehouse_sources_queue_partition_management/activities.py
index e26319c9fb8c..10b63a9abe2a 100644
--- a/posthog/temporal/warehouse_sources_queue_partition_management/activities.py
+++ b/posthog/temporal/warehouse_sources_queue_partition_management/activities.py
@@ -41,12 +41,11 @@ class PartitionResult:
@temporalio.activity.defn
async def manage_warehouse_sources_queue_partitions() -> dict:
- database_url: str = settings.WAREHOUSE_SOURCES_DATABASE_URL
ensured: list[str] = []
dropped: list[str] = []
errors: list[str] = []
- with psycopg.Connection.connect(database_url, autocommit=True) as conn:
+ with psycopg.Connection.connect(settings.WAREHOUSE_SOURCES_DATABASE_URL, autocommit=True) as conn:
today = datetime.now(UTC).date()
for table in PARTITIONED_TABLES: