Skip to content

fix: make TCP drop recovery test deterministic#248

Merged
marandaneto merged 3 commits into
mainfrom
fix-tcp-drop-flake
Jul 1, 2026
Merged

fix: make TCP drop recovery test deterministic#248
marandaneto merged 3 commits into
mainfrom
fix-tcp-drop-flake

Conversation

@marandaneto

@marandaneto marandaneto commented Jul 1, 2026

Copy link
Copy Markdown
Member

💡 Motivation and Context

TestTCPDropRecovery/DropAfterConnect was flaky because the test asserted server.SuccessCount() immediately after the PostHog callback reported success. The TCP test server increments its success counter after writing the HTTP response, so the client callback can win that race and observe 0 successes even though delivery succeeded.

This change makes the test wait for the TCP server's connection handler to finish before asserting server-side counters. Review feedback also pointed out the same server-counter race in TestTCPDropFailure, so that test now closes the TCP server before reading connection counts as well.

Semgrep also flagged the feature-flags project-board caller for using secrets: inherit. This mirrors PostHog/posthog-python#717: the central reusable workflow requires inherited secrets because it does not declare workflow_call.secrets, so the workflow keeps secrets: inherit with a scoped # nosemgrep suppression and explanatory comments. The workflow now also declares permissions: {} to satisfy CodeQL's minimal-token-permissions recommendation.

💚 How did you test it?

  • docker run --rm -v "$PWD":/src -w /src golang:1.24 go test ./extras -run 'TestTCPDropRecovery/DropAfterConnect' -count=100
  • docker run --rm -v "$PWD":/src -w /src golang:1.24 go test ./extras -count=20
  • docker run --rm -v "$PWD":/src -w /src golang:1.24 go test -race ./extras -run 'TestTCPDropRecovery/DropAfterConnect' -count=20
  • docker run --rm -v "$PWD":/src -w /src golang:1.24 go test ./extras -run 'TestTCPDrop' -count=50
  • docker run --rm -v "$PWD":/src -w /src golang:1.24 go test ./...
  • Parsed .github/workflows/call-flags-project-board.yml with PyYAML and verified permissions resolves to {} and secrets resolves to inherit.
  • docker run --rm -v "$PWD":/src -v /tmp/posthog-dotgithub-semgrep:/dotgithub -w /src semgrep/semgrep:1.163.0 semgrep ... .github/

📝 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)

Pi investigated the flaky TCP drop recovery test in a dedicated git worktree. The chosen test fix is intentionally small: close the flaky TCP server before reading its counters, because Close waits for handler goroutines and removes the race between the client callback and server-side bookkeeping. Review feedback was addressed by removing duplicate deferred closes and applying the same deterministic close-before-counter-read pattern to TestTCPDropFailure.

After CI reported a Semgrep failure, Pi inspected the job logs and aligned this repo's feature-flags project-board caller with PostHog/posthog-python#717: keep the required inherited secrets for the central reusable workflow and suppress only that intentional Semgrep finding inline with reviewer-facing context. CodeQL feedback was addressed by declaring minimal workflow permissions.

@marandaneto marandaneto self-assigned this Jul 1, 2026
@marandaneto marandaneto marked this pull request as ready for review July 1, 2026 09:12
@marandaneto marandaneto requested a review from a team as a code owner July 1, 2026 09:12
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

posthog-go Compliance Report

Date: 2026-07-01 09:40:48 UTC
Duration: 169146ms

✅ All Tests Passed!

110/110 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint 17ms
Endpoint And Method.Does Not Use Legacy Endpoints 6ms
Required Headers.Has Authorization Bearer Header 6ms
Required Headers.Has Content Type Json 5ms
Required Headers.Has Posthog Sdk Info Format 6ms
Required Headers.Has Posthog Attempt Header 6ms
Required Headers.Has Posthog Request Id 6ms
Required Headers.Has Posthog Request Timestamp 6ms
Required Headers.Has User Agent 6ms
Body Format.Body Has Created At And Batch 5ms
Body Format.No Api Key In Body 6ms
Body Format.No Sent At In Body 6ms
Event Format.Event Has Required Root Fields 6ms
Event Format.Event Uuid Is Valid 6ms
Event Format.Event Timestamp Is Rfc3339 16ms
Event Format.Distinct Id Is String 6ms
Event Format.Distinct Id At Root Not Properties 6ms
Event Format.Custom Properties Preserved 6ms
Event Format.Set Properties Preserved 5ms
Event Format.Set Once Properties Preserved 6ms
Event Format.Groups Properties Preserved 6ms
Event Format.Sdk Generates Uuid If Not Provided 6ms
Event Format.Event Has Required Root Fields Batch 8ms
Event Format.Event Uuid Is Valid Batch 8ms
Event Format.Event Timestamp Is Rfc3339 Batch 9ms
Event Format.Distinct Id Is String Batch 8ms
Event Format.Distinct Id At Root Not Properties Batch 9ms
Event Format.Custom Properties Preserved Batch 8ms
Event Format.Set Properties Preserved Batch 8ms
Event Format.Set Once Properties Preserved Batch 9ms
Event Format.Groups Properties Preserved Batch 8ms
Event Format.Sdk Generates Uuid If Not Provided Batch 9ms
Batch Behavior.Multiple Events In Single Batch 30ms
Batch Behavior.Batch Envelope Smoke 9ms
Batch Behavior.Flush With No Events Sends Nothing 3ms
Batch Behavior.Flush At Triggers Batch 1008ms
Batch Behavior.Created At Reflects Batch Creation Time 6ms
Deduplication.Generates Unique Uuids 15ms
Deduplication.Different Events Same Content Different Uuids 7ms
Deduplication.Preserves Uuid On Retry 5134ms
Deduplication.Preserves Timestamp On Retry 5136ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 5136ms
Deduplication.No Duplicate Events In Batch 31ms
Header Behavior On Retry.Attempt Header Starts At One 7ms
Header Behavior On Retry.Attempt Header Increments On Retry 10139ms
Header Behavior On Retry.Request Id Preserved On Retry 5136ms
Header Behavior On Retry.Different Requests Have Different Request Ids 2013ms
Header Behavior On Retry.Request Timestamp Changes On Retry 5134ms
Response Format Validation.Success Response Has Uuid Keyed Results 7ms
Response Format Validation.Success Response Has Ok For Each Event 28ms
Response Format Validation.Success No Retry After When All Ok 27ms
Response Format Validation.Success Retry After Present When Retry Events 130ms
Response Format Validation.Success No Retry After When Drop Only 27ms
Response Format Validation.Response Echoes Request Id 6ms
Retry Behavior.Retries On 408 5132ms
Retry Behavior.Retries On 500 5135ms
Retry Behavior.Retries On 503 5136ms
Retry Behavior.Retries On 504 5133ms
Retry Behavior.Retryable Errors Have Retry After 2132ms
Retry Behavior.Respects Retry After On Retryable Error 8139ms
Retry Behavior.Does Not Retry On 400 2132ms
Retry Behavior.Does Not Retry On 401 2132ms
Retry Behavior.Does Not Retry On 402 2131ms
Retry Behavior.Does Not Retry On 413 2132ms
Retry Behavior.Does Not Retry On 415 2132ms
Retry Behavior.Non Retryable Errors Have No Retry After 2133ms
Retry Behavior.Implements Backoff 15145ms
Retry Behavior.Max Retries Respected 15142ms
Partial Batch Handling.Handles 200 Full Success 2009ms
Partial Batch Handling.Handles 200 With All Ok 3032ms
Partial Batch Handling.Does Not Retry Dropped Events 3031ms
Partial Batch Handling.Does Not Retry Limited Events 3031ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 5136ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 5135ms
Partial Batch Handling.Retries Only Retry Events From Partial 5133ms
Partial Batch Handling.Partial Retry Preserves Uuids 5136ms
Partial Batch Handling.Partial Retry Attempt Header Increments 5136ms
Partial Batch Handling.Partial Retry Request Id Preserved 5135ms
Partial Batch Handling.Respects Retry After On Partial 5132ms
Partial Batch Handling.Unknown Result Treated As Terminal 3031ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 3033ms
Compression.Sends Gzip Content Encoding 16ms
Compression.No Content Encoding When Disabled 6ms
Compression.Compressed Body Is Decompressible 6ms
Error Handling.Does Not Retry On Unknown 4Xx 2131ms
Event Options.Cookieless Mode Override 7ms
Event Options.Disable Skew Correction Override 5ms
Event Options.Process Person Profile Override 6ms
Event Options.Product Tour Id Override 5ms
Event Options.Unset Options Omitted 6ms
Event Options.Options Override In Batch 9ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 6ms
Geoip And Historical Migration.Historical Migration Set In Body 6ms
Geoip And Historical Migration.Historical Migration Absent By Default 6ms

Feature_Flags Tests

16/16 tests passed

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

@greptile-apps

greptile-apps Bot commented Jul 1, 2026

Copy link
Copy Markdown

Comments Outside Diff (2)

  1. extras/tcp_drop_test.go, line 55 (link)

    P2 Double server.Close() call

    defer server.Close() on line 55 is still present, so after the explicit require.NoError(t, server.Close(), …) on line 94 the deferred call will run a second time when the test function exits. If flakyhttp.TCPServer.Close() is not idempotent the second call will return an error that is silently swallowed by the bare defer. Even if it is idempotent today, the pattern is fragile. The cleanest fix is to remove the defer and rely solely on the explicit close with error checking; early-failure paths (e.g. if server.Start() fails) already require.NoError and return before line 55 matters.

  2. extras/tcp_drop_test.go, line 190-193 (link)

    P2 TestTCPDropFailure has the same server-counter race

    TestTCPDropFailure asserts server.ConnCount() (line 193) after client.Close() but without first closing the server. The same race between client-side callback and server-side bookkeeping that prompted this PR can apply here: the last connection's server goroutine may not have finished incrementing the connection counter before the assertion runs. Closing the server (or waiting for its handlers) before reading counters would make this test equally deterministic.

Reviews (1): Last reviewed commit: "fix: make TCP drop recovery test determi..." | Re-trigger Greptile

Comment thread .github/workflows/call-flags-project-board.yml Fixed
@marandaneto marandaneto force-pushed the fix-tcp-drop-flake branch from 705b6bc to 90a77e9 Compare July 1, 2026 09:26
Comment thread .github/workflows/call-flags-project-board.yml Fixed
@marandaneto

Copy link
Copy Markdown
Member Author

Addressed the review feedback in e8d0970: removed the duplicate deferred TCP server closes, explicitly close the server before reading counters, and applied the same deterministic close-before-counter-read pattern to TestTCPDropFailure. Also added permissions: {} to the workflow for the CodeQL permissions feedback.

@marandaneto marandaneto merged commit ed70fae into main Jul 1, 2026
25 checks passed
@marandaneto marandaneto deleted the fix-tcp-drop-flake branch July 1, 2026 10:05
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