Skip to content

fix: fall back when payload compression fails#249

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

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

Conversation

@marandaneto

Copy link
Copy Markdown
Member

💡 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

  • 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 pnpm changeset 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 surgical and limited to the gzip request-construction path.

@marandaneto marandaneto self-assigned this Jun 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

posthog-dotnet Compliance Report

Date: 2026-06-27 13:24:09 UTC
Duration: 675ms

⚠️ Some Tests Failed

2/16 tests passed, 14 failed


Feature_Flags Tests

⚠️ 2/16 tests passed, 14 failed

View Details
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'

@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 on lines +315 to 322
catch (IOException)
{
return null;
}
catch (InvalidDataException)
{
return null;
}

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 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!

Comment on lines +293 to 323
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;
}
}

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 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.

Comment on lines +315 to 322
catch (IOException)
{
return null;
}
catch (InvalidDataException)
{
return null;
}

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 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!

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