Skip to content

fix: fall back when payload compression fails#700

Draft
marandaneto wants to merge 1 commit into
mainfrom
fix/compression-failure-fallback
Draft

fix: fall back when payload compression fails#700
marandaneto wants to merge 1 commit into
mainfrom
fix/compression-failure-fallback

Conversation

@marandaneto

Copy link
Copy Markdown
Member

💡 Motivation and Context

When gzip=True, local gzip failures should not prevent the SDK from sending the batch. The SDK should fall back to the original uncompressed JSON payload without a Content-Encoding header.

This catches local gzip errors during request construction and keeps the upload path uncompressed. A unit test covers the fallback.

💚 How did you test it?

  • uv run --extra test pytest posthog/test/test_request.py -q

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Implemented as part of a cross-SDK consistency pass for client-side compression failure fallback. Kept the change scoped to request construction and added a focused regression test.

@marandaneto marandaneto self-assigned this Jun 27, 2026
@greptile-apps

greptile-apps Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "fix: fall back when payload compression ..." | Re-trigger Greptile

Comment thread posthog/request.py
Comment on lines +251 to +252
except OSError as exc:
log.warning("failed to gzip request body, sending uncompressed: %s", exc)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 The except OSError is narrower than needed. GzipFile.write() delegates to zlib.Compress.compress(), which raises zlib.error — a plain Exception subclass, not an OSError — if the zlib library itself fails. That exception would bypass this handler, propagate uncaught, and drop the batch instead of falling back to uncompressed. Broadening the catch to Exception covers all realistic in-process compression failures without swallowing truly unexpected errors like MemoryError.

Suggested change
except OSError as exc:
log.warning("failed to gzip request body, sending uncompressed: %s", exc)
except Exception as exc:
log.warning("failed to gzip request body, sending uncompressed: %s", exc)

Comment on lines +182 to +201
def test_post_falls_back_to_uncompressed_payload_when_gzip_fails(self):
mock_response = requests.Response()
mock_response.status_code = 200
mock_session = mock.MagicMock()
mock_session.post.return_value = mock_response

with mock.patch.object(request_module, "GzipFile", side_effect=OSError("boom")):
request_module.post(
TEST_API_KEY,
host="https://test.posthog.com",
path="/batch/",
gzip=True,
session=mock_session,
batch=[],
)

data = mock_session.post.call_args.kwargs["data"]
headers = mock_session.post.call_args.kwargs["headers"]
self.assertIsInstance(data, str)
self.assertNotIn("Content-Encoding", headers)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Prefer parameterised test

The team's style guide says "We always prefer parameterised tests." This test exercises a single hard-coded OSError scenario, but if the catch is widened (e.g. to Exception) there are now multiple distinct exception types worth covering (e.g. OSError, zlib.error). Converting to @pytest.mark.parametrize (or subTest) with multiple exception types would both express the intent more clearly and guard against future regressions on each path.

Context Used: Do not attempt to comment on incorrect alphabetica... (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@github-actions

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-27 13:32:59 UTC
Duration: 530108ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 517ms
Format Validation.Event Has Uuid 10007ms
Format Validation.Event Has Lib Properties 10007ms
Format Validation.Distinct Id Is String 10007ms
Format Validation.Token Is Present 10007ms
Format Validation.Custom Properties Preserved 10007ms
Format Validation.Event Has Timestamp 10007ms
Retry Behavior.Retries On 503 18016ms
Retry Behavior.Does Not Retry On 400 12008ms
Retry Behavior.Does Not Retry On 401 10006ms
Retry Behavior.Respects Retry After Header 16015ms
Retry Behavior.Implements Backoff 30017ms
Retry Behavior.Retries On 500 13012ms
Retry Behavior.Retries On 502 16006ms
Retry Behavior.Retries On 504 16015ms
Retry Behavior.Max Retries Respected 30017ms
Deduplication.Generates Unique Uuids 7003ms
Deduplication.Preserves Uuid On Retry 16016ms
Deduplication.Preserves Uuid And Timestamp On Retry 23019ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 16006ms
Deduplication.No Duplicate Events In Batch 10002ms
Deduplication.Different Events Have Different Uuids 10007ms
Compression.Sends Gzip When Enabled 10008ms
Batch Format.Uses Proper Batch Structure 10007ms
Batch Format.Flush With No Events Sends Nothing 5005ms
Batch Format.Multiple Events Batched Together 10005ms
Error Handling.Does Not Retry On 403 12008ms
Error Handling.Does Not Retry On 413 10008ms
Error Handling.Retries On 408 14015ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 9501ms
Request Payload.Flags Request Uses V2 Query Param 10007ms
Request Payload.Flags Request Hits Flags Path Not Decide 10007ms
Request Payload.Flags Request Omits Authorization Header 10007ms
Request Payload.Token In Flags Body Matches Init 10007ms
Request Payload.Groups Round Trip 10007ms
Request Payload.Groups Default To Empty Object 10007ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 10007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 10007ms
Request Payload.Disable Geoip Omitted Defaults To False 10007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 10007ms
Request Lifecycle.No Flags Request On Init Alone 5003ms
Request Lifecycle.No Flags Request On Normal Capture 10508ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 9511ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10002ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 10510ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant