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-imports): classify table-size fd-exhaustion as transient (#102813)

Tom Owersdf7d81768850
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/calculate_table_size.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/calculate_table_size.py
index ef423ae6ce45..6b90e686d52b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/calculate_table_size.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/calculate_table_size.py
@@ -1,3 +1,4 @@
+import errno
import dataclasses
from django.conf import settings
@@ -6,6 +7,7 @@ from django.db import DatabaseError, close_old_connections
from structlog.contextvars import bind_contextvars
from temporalio import activity
+from posthog.temporal.common.errors import NonReportableError
from posthog.temporal.common.logger import get_logger
from products.data_warehouse.backend.facade.api import get_size_of_folder
@@ -63,7 +65,17 @@ def calculate_table_size_activity(inputs: CalculateTableSizeActivityInputs) -> N
else:
s3_folder = f"{settings.BUCKET_URL}/{folder_name}/{schema.normalized_name}"
- total_mib = get_size_of_folder(s3_folder)
+ try:
+ total_mib = get_size_of_folder(s3_folder)
+ except OSError as e:
+ if e.errno not in (errno.EMFILE, errno.ENFILE):
+ raise
+ # Fd pressure on this worker (e.g. botocore loading a data file while building the S3
+ # client) — the same transient-capacity class postgres.py's _is_too_many_open_files_error
+ # already recognizes on the connect path. A descriptor frees the moment another
+ # connection/client in this worker closes, so it's never a defect in this activity.
+ logger.warning("Too many open files calculating table size in S3", exc_info=e)
+ raise NonReportableError("Too many open files calculating table size in S3") from e
logger.debug(f"Total size in MiB = {total_mib:.2f}")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_calculate_table_size.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_calculate_table_size.py
index e2f33f81e3a3..7d785d8e3d23 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_calculate_table_size.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_calculate_table_size.py
@@ -1,7 +1,10 @@
+import errno
+
import pytest
from unittest.mock import patch
from posthog.models import Organization, Team
+from posthog.temporal.common.errors import NonReportableError
from products.warehouse_sources.backend.models.external_data_job import ExternalDataJob
from products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchema
@@ -85,3 +88,28 @@ class TestCalculateTableSizeActivity:
assert not ExternalDataJob.objects.filter(id=job.id).exists()
assert not DataWarehouseTable.objects.filter(id=table.id).exists()
+
+ def test_reraises_fd_exhaustion_as_non_reportable(self) -> None:
+ # get_size_of_folder() builds a fresh S3 client per call, which can hit a bare OSError
+ # (EMFILE/ENFILE) when this worker is briefly out of file descriptors. That's our own
+ # transient capacity, not a bug in this activity, so it must not reach error tracking -
+ # it used to, because nothing here classified it before re-raising.
+ team = _team()
+ schema, _table, job = _schema_table_job(team)
+
+ with patch.object(calc, "get_size_of_folder", side_effect=OSError(errno.EMFILE, "Too many open files")):
+ with pytest.raises(NonReportableError):
+ calculate_table_size_activity(
+ CalculateTableSizeActivityInputs(team_id=team.id, schema_id=str(schema.id), job_id=str(job.id))
+ )
+
+ def test_reraises_unrelated_os_error(self) -> None:
+ team = _team()
+ schema, _table, job = _schema_table_job(team)
+
+ with patch.object(calc, "get_size_of_folder", side_effect=OSError(errno.EACCES, "Permission denied")):
+ with pytest.raises(OSError) as exc_info:
+ calculate_table_size_activity(
+ CalculateTableSizeActivityInputs(team_id=team.id, schema_id=str(schema.id), job_id=str(job.id))
+ )
+ assert not isinstance(exc_info.value, NonReportableError)