Skip to content

fix(queue): 413 no config mutation#512

Merged
turnipdabeets merged 4 commits into
mainfrom
fix/queue-413-no-config-mutation
May 5, 2026
Merged

fix(queue): 413 no config mutation#512
turnipdabeets merged 4 commits into
mainfrom
fix/queue-413-no-config-mutation

Conversation

@turnipdabeets
Copy link
Copy Markdown
Contributor

The 413 adaptive batch sizing currently mutates the user-provided PostHogConfig, halving config.maxBatchSize and config.flushAt directly. That makes the user's configured values silently change at runtime — surprising for callers that read those fields back, and it leaks internal queue state across SDK boundaries.

It also halves from the configured cap rather than the actual batch size that triggered the 413. If a 413 fires on a batch of 10 with maxBatchSize=50, we halve from 50 → 25 → 12 → ... wasting halvings on sizes the server already proved (or is about to prove) too large.

Move the cap and flushAt into a private BatchLimits class owned by PostHogQueue, initialized from config once at construction. Halve from min(cap, actualBatchSize) so a partial batch's 413 doesn't waste cycles on larger caps the server hasn't accepted, and clamp flushAt to cap on each halve so we never buffer more events than a single batch can drain (e.g. cap=1 + flushAt=10 would otherwise pile 10 events to send 1 at a time). config.maxBatchSize and config.flushAt are now read-only as far as the queue is concerned.

Matches the equivalent iOS fix in posthog-ios PR #585

💡 Motivation and Context

💚 How did you test it?

📝 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

@turnipdabeets turnipdabeets requested a review from a team as a code owner May 5, 2026 18:11
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 5, 2026

⚠️ Changeset declares `posthog-android` but no source files in that package changed

This is informational — the PR is not blocked. Click the triangle above to collapse, or push a fix and this comment will auto-delete.

Declared in a changeset but no source files modified:

  • posthog-android

Double-check this is intentional — for example, releasing a previously-merged change.

Changesets in this PR:

  • posthog — patch
  • posthog-android — patch

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 5, 2026

posthog-android Compliance Report

Date: 2026-05-05 18:29:48 UTC
Duration: 2865ms

⚠️ Some Tests Failed

0/16 tests passed, 16 failed


Feature_Flags Tests

⚠️ 0/16 tests passed, 16 failed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 280ms
Request Payload.Flags Request Uses V2 Query Param 35ms
Request Payload.Flags Request Hits Flags Path Not Decide 43ms
Request Payload.Flags Request Omits Authorization Header 26ms
Request Payload.Token In Flags Body Matches Init 28ms
Request Payload.Groups Round Trip 24ms
Request Payload.Groups Default To Empty Object 23ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 21ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 18ms
Request Payload.Disable Geoip Omitted Defaults To False 20ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 16ms
Request Lifecycle.No Flags Request On Init Alone 13ms
Request Lifecycle.No Flags Request On Normal Capture 2051ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 15ms
Request Lifecycle.Mock Response Value Is Returned To Caller 17ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 16ms

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

Expected 0 /flags requests, got 1

request_lifecycle.no_flags_request_on_normal_capture

Expected 0 /flags requests, got 1

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'

turnipdabeets and others added 3 commits May 5, 2026 14:19
The 413 adaptive batch sizing currently mutates the user-provided
PostHogConfig, halving config.maxBatchSize and config.flushAt directly.
That makes the user's configured values silently change at runtime —
surprising for callers that read those fields back, and it leaks
internal queue state across SDK boundaries.

It also halves from the *configured* cap rather than the *actual* batch
size that triggered the 413. If a 413 fires on a batch of 10 with
maxBatchSize=50, we halve from 50 → 25 → 12 → ... wasting halvings on
sizes the server already proved (or is about to prove) too large.

Move the cap and flushAt into a private BatchLimits class owned by
PostHogQueue, initialized from config once at construction. Halve from
min(cap, actualBatchSize) so a partial batch's 413 doesn't waste cycles
on larger caps the server hasn't accepted, and clamp flushAt to cap on
each halve so we never buffer more events than a single batch can drain
(e.g. cap=1 + flushAt=10 would otherwise pile 10 events to send 1 at a
time). config.maxBatchSize and config.flushAt are now read-only as far
as the queue is concerned.

Matches the equivalent iOS fix in posthog-ios PR #585 and posthog-js-lite.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The unit tests for `deleteFilesIfAPIError` exercise the halving logic
against a `BatchLimits` instance directly. They don't catch the case
where `takeFiles()` (or any other call site) regresses to reading
`config.maxBatchSize` instead of `batchLimits.cap` — the queue would
keep sending full-size batches even after the cap had been halved.

Add an integration test that drives `flush()` against a mock returning
413 and asserts that `currentBatchCapForTesting` and
`currentFlushAtForTesting` halve across successive flushes. Adds the
two test accessors mirroring iOS's `currentBatchCapForTesting` /
`currentFlushAtForTesting`.
@turnipdabeets turnipdabeets force-pushed the fix/queue-413-no-config-mutation branch from db44f1d to ad7734d Compare May 5, 2026 18:19
Comment thread posthog/src/main/java/com/posthog/internal/PostHogQueue.kt Outdated
Comment thread posthog/src/main/java/com/posthog/internal/PostHogQueue.kt
@marandaneto marandaneto requested a review from ioannisj May 5, 2026 18:21
Replace nested max(min(...), 1) with .div(2).coerceAtLeast(1) chaining,
move BatchLimits.initial(...) to a top-level initialBatchLimits(...)
factory using .coerceAtLeast(1).
@turnipdabeets turnipdabeets changed the title Fix/queue 413 no config mutation fix(queue): 413 no config mutation May 5, 2026
@turnipdabeets turnipdabeets requested a review from marandaneto May 5, 2026 19:48
@turnipdabeets turnipdabeets merged commit 3a2a1ec into main May 5, 2026
14 checks passed
@turnipdabeets turnipdabeets deleted the fix/queue-413-no-config-mutation branch May 5, 2026 20:40
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.

3 participants