fix: fall back when payload compression fails#249
Conversation
posthog-dotnet Compliance ReportDate: 2026-06-27 13:24:09 UTC
|
| Test | Status | Duration |
|---|---|---|
| Request Payload.Request With Person Properties Device Id | ❌ | 49ms |
| Request Payload.Flags Request Uses V2 Query Param | ❌ | 23ms |
| Request Payload.Flags Request Hits Flags Path Not Decide | ❌ | 7ms |
| Request Payload.Flags Request Omits Authorization Header | ❌ | 5ms |
| Request Payload.Token In Flags Body Matches Init | ❌ | 6ms |
| Request Payload.Groups Round Trip | ❌ | 5ms |
| Request Payload.Groups Default To Empty Object | ❌ | 6ms |
| Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It | ❌ | 5ms |
| Request Payload.Disable Geoip False Propagates As Geoip Disable False | ❌ | 5ms |
| Request Payload.Disable Geoip Omitted Defaults To False | ❌ | 5ms |
| Request Payload.Flag Keys To Evaluate Contains Only Requested Key | ❌ | 5ms |
| Request Lifecycle.No Flags Request On Init Alone | ✅ | 4ms |
| Request Lifecycle.No Flags Request On Normal Capture | ✅ | 190ms |
| Request Lifecycle.Two Flag Calls Produce Two Remote Requests | ❌ | 7ms |
| Request Lifecycle.Mock Response Value Is Returned To Caller | ❌ | 6ms |
| Side Effect Events.Get Feature Flag Captures Feature Flag Called Event | ❌ | 5ms |
Failures
request_payload.request_with_person_properties_device_id
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_payload.flags_request_uses_v2_query_param
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_payload.flags_request_hits_flags_path_not_decide
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_payload.flags_request_omits_authorization_header
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_payload.token_in_flags_body_matches_init
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_payload.groups_round_trip
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_payload.groups_default_to_empty_object
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_payload.person_properties_distinct_id_auto_populated_when_caller_omits_it
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_payload.disable_geoip_false_propagates_as_geoip_disable_false
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_payload.disable_geoip_omitted_defaults_to_false
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_payload.flag_keys_to_evaluate_contains_only_requested_key
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_lifecycle.two_flag_calls_produce_two_remote_requests
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
request_lifecycle.mock_response_value_is_returned_to_caller
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
side_effect_events.get_feature_flag_captures_feature_flag_called_event
404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'
|
Reviews (1): Last reviewed commit: "fix: fall back when payload compression ..." | Re-trigger Greptile |
| catch (IOException) | ||
| { | ||
| return null; | ||
| } | ||
| catch (InvalidDataException) | ||
| { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
Missing test for the fallback path
The entire purpose of this PR — silently falling back to uncompressed when gzip fails — is not exercised by any test. The existing CompressesRequestBodyWithGzip test only verifies the happy path. A test that injects a triggerable failure (e.g., a custom MemoryStream subclass that throws IOException on Write) and then asserts the request still reaches the server uncompressed would directly cover this new branch. Without it, a future regression (e.g., accidentally dropping the catch or the null-check) would go undetected.
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!
| static async Task<ByteArrayContent?> TryCreateCompressedJsonContentAsync( | ||
| object content, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| // Stream JSON directly into gzip to avoid intermediate allocation | ||
| using var memoryStream = new MemoryStream(4096); | ||
| using (var gzipStream = new GZipStream(memoryStream, CompressionLevel.Fastest, leaveOpen: true)) | ||
| { | ||
| await JsonSerializer.SerializeAsync(gzipStream, content, JsonSerializerHelper.Options, cancellationToken); | ||
| } | ||
|
|
||
| // Use TryGetBuffer to avoid ToArray() copy when possible | ||
| var compressedContent = memoryStream.TryGetBuffer(out var buffer) | ||
| ? new ByteArrayContent(buffer.Array!, buffer.Offset, buffer.Count) | ||
| : new ByteArrayContent(memoryStream.ToArray()); | ||
|
|
||
| compressedContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); | ||
| compressedContent.Headers.ContentEncoding.Add("gzip"); | ||
|
|
||
| return await httpClient.PostAsync(requestUri, compressedContent, cancellationToken); | ||
| return compressedContent; | ||
| } | ||
| catch (IOException) | ||
| { | ||
| return null; | ||
| } | ||
| catch (InvalidDataException) | ||
| { | ||
| return null; | ||
| } | ||
| } |
There was a problem hiding this comment.
Silent fallback has no observability
When TryCreateCompressedJsonContentAsync catches an exception and returns null, the caller silently retries without compression and there is no log, metric, or any other signal that compression was skipped. In production, an operator enabling EnableCompression would see requests going through but have no way to know they are not actually compressed. Adding even a debug-level log statement here (once a logger is threaded through, or via a passed-in ILogger) would make this fallback diagnosable.
| catch (IOException) | ||
| { | ||
| return null; | ||
| } | ||
| catch (InvalidDataException) | ||
| { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
Caught exception types may not match realistic
MemoryStream/GZipStream write failures
IOException and InvalidDataException are the two caught exceptions, but MemoryStream (an in-memory buffer) does not throw IOException during normal writes — it can only throw NotSupportedException (read-only stream) or ObjectDisposedException. InvalidDataException is typically thrown when reading malformed gzip data, not when writing. Could you add a brief comment (or a test) that documents the concrete scenario — OS-level or platform-specific — that triggers one of these exceptions, to clarify that the catch scope is intentionally narrow?
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!
💡 Motivation and Context
If local gzip compression fails while preparing a batch upload, the SDK should still send the original payload uncompressed instead of failing before the request is sent.
This updates the compressed batch path to build gzip content first and fall back to the normal JSON request path when gzip content creation fails.
💚 How did you test it?
dotnet test tests/UnitTests/UnitTests.csproj --filter HttpClientExtensionsTests📝 Checklist
If releasing new changes
pnpm changesetto 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 surgical and limited to the gzip request-construction path.