: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-imports): stop leaking a second s3 client on fresh instances (#79824)
Tom Owers0bc2df0a43e2
diff --git a/products/data_warehouse/backend/s3.py b/products/data_warehouse/backend/s3.pyindex db1d3be936da..74cebe389f42 100644--- a/products/data_warehouse/backend/s3.py+++ b/products/data_warehouse/backend/s3.py@@ -74,13 +74,16 @@ async def aget_s3_client(*, fresh_instance: bool = False, endpoint_url: Optionaltry:yield s3finally:- # Uncached instances aren't finalized by the fsspec registry, so close the aiobotocore client- # explicitly (s3fs's set_session docs: "to be closed later with await .close()") to avoid- # leaking HTTP connections in long-lived workers. Never close the shared cached instance —- # other callers hold references to it.+ # Uncached instances aren't finalized by the fsspec registry, so close the aiobotocore+ # client(s) explicitly (s3fs's set_session docs: "to be closed later with await .close()")+ # to avoid leaking HTTP connections in long-lived workers. Close via _s3creator rather than+ # just _s3: with region caching on (the default), s3fs lazily opens a second, region-specific+ # client the first time a bucket resolves to a different region (S3BucketRegionCache.get_bucket_client),+ # and only _s3creator tracks that second client — closing _s3 alone leaks it. _s3creator's+ # __aexit__ closes everything it opened either way (see s3fs's own close_session, which does+ # the same). Never close the shared cached instance — other callers hold references to it.with contextlib.suppress(Exception):- if s3._s3 is not None:- await s3._s3.close()+ await s3._s3creator.__aexit__(None, None, None)def get_size_of_folder(path: str) -> float:diff --git a/products/data_warehouse/backend/tests/test_s3.py b/products/data_warehouse/backend/tests/test_s3.pynew file mode 100644index 000000000000..6d2dedda6494--- /dev/null+++ b/products/data_warehouse/backend/tests/test_s3.py@@ -0,0 +1,31 @@+import asyncio++from unittest.mock import AsyncMock, MagicMock, patch++from django.test import SimpleTestCase, override_settings++from products.data_warehouse.backend.s3 import aget_s3_client+++class TestAgetS3Client(SimpleTestCase):+ @override_settings(USE_LOCAL_SETUP=False)+ def test_fresh_instance_closes_the_full_s3creator_not_just_the_general_client(self) -> None:+ # With region caching on (the s3fs default), a fresh_instance client that touches a bucket+ # outside its default region opens a second, region-specific aiobotocore client that only+ # _s3creator tracks (S3BucketRegionCache.get_bucket_client). Closing _s3 alone would leak it.+ fake_s3 = MagicMock()+ fake_s3.set_session = AsyncMock()+ fake_s3._s3 = MagicMock()+ fake_s3._s3.close = AsyncMock()+ fake_s3._s3creator = MagicMock()+ fake_s3._s3creator.__aexit__ = AsyncMock()++ async def run() -> None:+ with patch("products.data_warehouse.backend.s3.s3fs.S3FileSystem", return_value=fake_s3):+ async with aget_s3_client(fresh_instance=True) as s3:+ assert s3 is fake_s3++ asyncio.run(run())++ fake_s3._s3creator.__aexit__.assert_awaited_once_with(None, None, None)+ fake_s3._s3.close.assert_not_awaited()