: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-warehouse): treat SeaweedFS's bucket-exists race as benign (#113566)
Tom Owersbb7a7f05b339
diff --git a/products/data_warehouse/backend/s3.py b/products/data_warehouse/backend/s3.pyindex 8a1e5ee8ba91..b1de72fbf04e 100644--- a/products/data_warehouse/backend/s3.py+++ b/products/data_warehouse/backend/s3.py@@ -159,6 +159,14 @@ def get_size_of_folder(path: str) -> float:# to let that race self-heal; a persistent misconfiguration still raises once it's exhausted._HEAD_BUCKET_MAX_ATTEMPTS = 4+# SeaweedFS (the S3-compatible backend behind local/self-hosted setups) has no per-account ownership+# check like AWS's, so it reports the loser of a concurrent create_bucket race as a bare+# BucketAlreadyExists instead of AWS's BucketAlreadyOwnedByYou. Its message otherwise reuses AWS's own+# "bucket namespace is shared by all users of the system" wording verbatim for a genuine collision too,+# so matching on that alone can't tell the two apart; "existing collection" is SeaweedFS's own+# bucket-equivalent term inserted into that message and never appears in AWS's.+_SEAWEEDFS_BUCKET_RACE_NEEDLE = "existing collection"+def ensure_bucket_exists(s3_url: str, s3_key: str, s3_secret: str, s3_endpoint: Optional[str] = None) -> None:try:@@ -204,7 +212,11 @@ def ensure_bucket_exists(s3_url: str, s3_key: str, s3_secret: str, s3_endpoint:# the loser's create_bucket then reports it already owns the bucket the winner just# made. That's the intended end state, not a failure.create_error_code = create_error.response.get("Error", {}).get("Code")- if create_error_code != "BucketAlreadyOwnedByYou":+ is_benign_race = create_error_code == "BucketAlreadyOwnedByYou" or (+ create_error_code == "BucketAlreadyExists"+ and _SEAWEEDFS_BUCKET_RACE_NEEDLE in str(create_error)+ )+ if not is_benign_race:raisereturndiff --git a/products/data_warehouse/backend/tests/test_s3.py b/products/data_warehouse/backend/tests/test_s3.pyindex fe314d369087..23483cae891d 100644--- a/products/data_warehouse/backend/tests/test_s3.py+++ b/products/data_warehouse/backend/tests/test_s3.py@@ -16,8 +16,8 @@ from products.data_warehouse.backend.s3 import ()-def _client_error(code: str) -> ClientError:- return ClientError({"Error": {"Code": code}}, "operation")+def _client_error(code: str, message: str = "") -> ClientError:+ return ClientError({"Error": {"Code": code, "Message": message}}, "operation")class TestAgetS3Client(SimpleTestCase):@@ -133,19 +133,40 @@ class TestEnsureBucketExists(SimpleTestCase):@parameterized.expand([- ("owned_by_us", "BucketAlreadyOwnedByYou", False),- ("owned_by_someone_else", "BucketAlreadyExists", True),- ("access_denied", "AccessDenied", True),+ ("owned_by_us", "BucketAlreadyOwnedByYou", "", False),+ ("owned_by_someone_else", "BucketAlreadyExists", "", True),+ (+ "seaweedfs_shared_namespace_race",+ "BucketAlreadyExists",+ "The bucket name can not be an existing collection, and the bucket namespace is "+ "shared by all users of the system. Please select a different name and try again.",+ False,+ ),+ (+ "genuine_aws_collision_with_shared_namespace_wording",+ "BucketAlreadyExists",+ "The requested bucket name is not available. The bucket namespace is shared by all "+ "users of the system. Please select a different name and try again.",+ True,+ ),+ ("access_denied", "AccessDenied", "", True),])@patch("products.data_warehouse.backend.s3.boto3.client")- def test_create_bucket_race_after_a_404(self, _name, create_error_code, should_raise, mock_boto3_client) -> None:+ def test_create_bucket_race_after_a_404(+ self, _name, create_error_code, create_error_message, should_raise, mock_boto3_client+ ) -> None:# A concurrent caller can create the bucket between our head_bucket 404 and our own# create_bucket call. BucketAlreadyOwnedByYou means we lost that race but still own the- # bucket, so it must not surface as a failure; any other create_bucket error is real.+ # bucket, so it must not surface as a failure. SeaweedFS (the backend behind local/self-hosted+ # setups) has no per-account ownership check, so it reports the identical race as a bare+ # BucketAlreadyExists carrying its own "existing collection" wording instead — that must not+ # raise either. SeaweedFS's message otherwise reuses AWS's own wording for a genuine collision+ # verbatim, so a BucketAlreadyExists with AWS's wording but no "existing collection" is a real,+ # unrelated name collision and must still raise.s3_client = MagicMock()s3_client.head_bucket.side_effect = _client_error("404")- s3_client.create_bucket.side_effect = _client_error(create_error_code)+ s3_client.create_bucket.side_effect = _client_error(create_error_code, create_error_message)mock_boto3_client.return_value = s3_clientif should_raise: