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(managed-warehouse): stop reporting DuckLake cleanup on a dead connection (#87450)

posthog[bot]e71717213631
diff --git a/products/managed_warehouse/backend/temporal/ducklake_register_data_imports_workflow.py b/products/managed_warehouse/backend/temporal/ducklake_register_data_imports_workflow.py
index ad72340733ec..b9b212f2e05a 100644
--- a/products/managed_warehouse/backend/temporal/ducklake_register_data_imports_workflow.py
+++ b/products/managed_warehouse/backend/temporal/ducklake_register_data_imports_workflow.py
@@ -810,11 +810,27 @@ def _cleanup_registration_tables(conn: psycopg.Connection, schema_name: str, tab
# Best-effort: this runs in the register `finally`, so raising here would mask
# the in-flight exception. A table left behind is picked up by the workflow's
# cleanup finalizer activity, which retries with a fresh connection.
+ #
+ # A dead connection cannot recover, so retrying the drop or reporting the
+ # failure adds no value: the register activity already failed on the transport,
+ # and the finalizer retries the drop with a fresh connection.
+ if conn.closed:
+ LOGGER.warning(
+ "Skipped DuckLake registration cleanup on a closed connection; the workflow cleanup finalizer will retry",
+ schema_name=schema_name,
+ )
+ return
for table_name in table_names:
for attempt in range(1, _CLEANUP_DROP_ATTEMPTS + 1):
try:
_drop_registration_table(conn, schema_name, table_name)
break
+ except (psycopg.OperationalError, psycopg.InterfaceError):
+ LOGGER.warning(
+ "Aborted DuckLake registration cleanup on a broken connection; the workflow cleanup finalizer will retry",
+ table_name=f"{schema_name}.{table_name}",
+ )
+ return
except Exception as error:
if attempt == _CLEANUP_DROP_ATTEMPTS:
LOGGER.error(
diff --git a/products/managed_warehouse/backend/tests/temporal/test_ducklake_register_data_imports_workflow.py b/products/managed_warehouse/backend/tests/temporal/test_ducklake_register_data_imports_workflow.py
index 00983fdf4122..81334d4415c3 100644
--- a/products/managed_warehouse/backend/tests/temporal/test_ducklake_register_data_imports_workflow.py
+++ b/products/managed_warehouse/backend/tests/temporal/test_ducklake_register_data_imports_workflow.py
@@ -186,6 +186,7 @@ def test_copy_activity_uses_s3_copy_and_local_duckgres_postgres_connection(monke
)
conn = MagicMock()
+ conn.closed = False
conn.__enter__ = MagicMock(return_value=conn)
conn.__exit__ = MagicMock(return_value=False)
active_transaction: object | None = None
@@ -452,6 +453,7 @@ def test_duckgres_cancel_watchdog_rejects_an_exhausted_activity_budget(monkeypat
def test_registration_stops_after_glob_query_cancellation(monkeypatch):
monkeypatch.setattr(registration_module, "setup_duckgres_session", MagicMock())
conn = MagicMock()
+ conn.closed = False
cancel_requested = registration_module.threading.Event()
def execute(query: object) -> MagicMock:
@@ -595,6 +597,7 @@ def test_copy_activity_does_not_publish_a_row_count_mismatch_when_cleanup_fails(
)
monkeypatch.setattr(registration_module, "_prepared_generation_is_current", lambda inputs: True)
conn = MagicMock()
+ conn.closed = False
counts = iter([(10,), (9,)])
def execute(query: object) -> MagicMock:
@@ -638,6 +641,7 @@ def test_copy_activity_skips_publish_when_newer_generation_already_landed(monkey
)
monkeypatch.setattr(registration_module, "_should_publish_prepared_generation", lambda inputs: False)
conn = MagicMock()
+ conn.closed = False
def execute(query: object) -> MagicMock:
if "SELECT count(*) FROM" in str(query):
@@ -690,6 +694,7 @@ def test_unknown_publish_commit_error_survives_temporary_table_cleanup(monkeypat
monkeypatch.setattr(registration_module, "setup_duckgres_session", MagicMock())
monkeypatch.setattr(registration_module.time, "sleep", lambda _seconds: None)
conn = MagicMock()
+ conn.closed = False
commit_error = RuntimeError("commit acknowledgement lost")
@contextlib.contextmanager
@@ -729,6 +734,39 @@ def test_unknown_publish_commit_error_survives_temporary_table_cleanup(monkeypat
)
+def test_cleanup_skips_a_closed_connection_without_reporting(monkeypatch):
+ sleep = MagicMock()
+ capture = MagicMock()
+ monkeypatch.setattr(registration_module.time, "sleep", sleep)
+ monkeypatch.setattr(registration_module, "capture_exception", capture)
+ conn = MagicMock()
+ conn.closed = True
+
+ registration_module._cleanup_registration_tables(conn, "schema", ["__ph_register_x"])
+
+ conn.execute.assert_not_called()
+ sleep.assert_not_called()
+ capture.assert_not_called()
+
+
+def test_cleanup_aborts_a_broken_connection_without_reporting(monkeypatch):
+ sleep = MagicMock()
+ capture = MagicMock()
+ monkeypatch.setattr(registration_module.time, "sleep", sleep)
+ monkeypatch.setattr(registration_module, "capture_exception", capture)
+ conn = MagicMock()
+ conn.closed = False
+ conn.execute.side_effect = registration_module.psycopg.OperationalError(
+ "SSL connection has been closed unexpectedly"
+ )
+
+ registration_module._cleanup_registration_tables(conn, "schema", ["__ph_register_x", "__ph_previous_x"])
+
+ assert conn.execute.call_count == 1
+ sleep.assert_not_called()
+ capture.assert_not_called()
+
+
@pytest.mark.asyncio
async def test_workflow_does_not_record_duration_when_disabled(monkeypatch):
execute_activity = AsyncMock(return_value=False)