Repository navigation
fix: accept plain closure arrays in setBeforeSend without a runtime trap - #974
Merged
turnipdabeets merged 1 commit intoOct 27, 2026
Merged
Conversation
Passing a [(PostHogEvent) -> PostHogEvent?] (or the logs equivalent) to setBeforeSend(_:) force-cast the array to the @sendable element type and trapped at runtime in Swift 5 mode. Add disfavored overloads for the plain function types that box each element and forward, so no cast happens. Literals, variadic calls and [BeforeSendBlock] arrays still resolve to the @sendable overloads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 of 15 tasks
Contributor
posthog-ios Compliance ReportDate: 2026-10-27T17:10:00.033497+00:00
|
| Test | Status | Duration |
|---|---|---|
| Format Validation.Event Has Required Fields | ✅ | 3106ms |
| Format Validation.Event Has Uuid | ✅ | 3001ms |
| Format Validation.Event Has Lib Properties | ✅ | 2909ms |
| Format Validation.Distinct Id Is String | ✅ | 3090ms |
| Format Validation.Token Is Present | ✅ | 3008ms |
| Format Validation.Custom Properties Preserved | ✅ | 2987ms |
| Format Validation.Event Has Timestamp | ✅ | 3002ms |
| Format Validation.Non Utc Event Timestamp Is Converted To Utc | ✅ | 2865ms |
| Retry Behavior.Retries On 503 | ✅ | 11889ms |
| Retry Behavior.Does Not Retry On 400 | ✅ | 5160ms |
| Retry Behavior.Does Not Retry On 401 | ✅ | 5022ms |
| Retry Behavior.Respects Retry After Header | ✅ | 8124ms |
| Retry Behavior.Implements Backoff | ✅ | 21886ms |
| Retry Behavior.Retries On 500 | ✅ | 9307ms |
| Retry Behavior.Retries On 502 | ✅ | 9443ms |
| Retry Behavior.Retries On 504 | ✅ | 5406ms |
| Retry Behavior.Max Retries Respected | ❌ | 21849ms |
| Deduplication.Generates Unique Uuids | ✅ | 3162ms |
| Deduplication.Preserves Uuid On Retry | ✅ | 9261ms |
| Deduplication.Preserves Uuid And Timestamp On Retry | ✅ | 16858ms |
| Deduplication.Preserves Uuid And Timestamp On Batch Retry | ✅ | 7928ms |
| Deduplication.No Duplicate Events In Batch | ✅ | 3088ms |
| Deduplication.Different Events Have Different Uuids | ✅ | 2933ms |
| Compression.Sends Gzip When Enabled | ✅ | 2979ms |
| Batch Format.Uses Proper Batch Structure | ✅ | 2878ms |
| Batch Format.Flush With No Events Sends Nothing | ✅ | 341ms |
| Batch Format.Multiple Events Batched Together | ✅ | 3061ms |
| Error Handling.Does Not Retry On 403 | ✅ | 2429ms |
| Error Handling.Does Not Retry On 413 | ✅ | 4882ms |
| Error Handling.Retries On 408 | ✅ | 5575ms |
Failures
retry_behavior.max_retries_respected
Expected 4 requests, got 6
Feature_Flags Tests
✅ 17/17 tests passed
View Details
| Test | Status | Duration |
|---|---|---|
| Request Payload.Request With Person Properties Device Id | ✅ | 2853ms |
| Request Payload.Flags Request Uses V2 Query Param | ✅ | 2967ms |
| Request Payload.Flags Request Hits Flags Path Not Decide | ✅ | 2895ms |
| Request Payload.Flags Request Omits Authorization Header | ✅ | 2961ms |
| Request Payload.Token In Flags Body Matches Init | ✅ | 3116ms |
| Request Payload.Groups Round Trip | ✅ | 3002ms |
| Request Payload.Groups Default To Empty Object | ✅ | 3003ms |
| Request Payload.Disable Geoip False Propagates As Geoip Disable False | ✅ | 3000ms |
| Request Payload.Disable Geoip Omitted Defaults To False | ✅ | 2937ms |
| Request Payload.Flag Keys To Evaluate Contains Only Requested Key | ✅ | 2951ms |
| Request Lifecycle.No Flags Request On Init Alone | ✅ | 111ms |
| Request Lifecycle.No Flags Request On Normal Capture | ✅ | 2906ms |
| Request Lifecycle.Two Flag Calls Produce Two Remote Requests | ✅ | 5816ms |
| Request Lifecycle.Mock Response Value Is Returned To Caller | ✅ | 2895ms |
| Retry Behavior.Retries Flags On 502 | ✅ | 2887ms |
| Retry Behavior.Retries Flags On 504 | ✅ | 2948ms |
| Side Effect Events.Get Feature Flag Captures Feature Flag Called Event | ✅ | 3217ms |
dustinbyrne
approved these changes
Oct 27, 2026
dustinbyrne
left a comment
Contributor
There was a problem hiding this comment.
The element-wise adaptation fixes stored plain closure arrays while preserving preferred Sendable overloads and existing hook behavior. No actionable findings.
AI-assisted review.
3 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
Addresses ioannisj's blocking review on #873 (#873).
Making
BeforeSendBlockandPostHogBeforeSendLogBlock@Sendablebroke Swift 5 apps that pass a stored array typed with the plain function type tosetBeforeSend(_:):The array is force-cast to the
@Sendableelement type at runtime and traps withCould not cast value of type '(PostHogEvent) -> Optional<PostHogEvent>' to '@Sendable …'.This adds a
@_disfavoredOverloadsetBeforeSend(_:)taking[(PostHogEvent) -> PostHogEvent?]onPostHogConfigand[(PostHogLogRecord) -> PostHogLogRecord?]onPostHogLogsConfig. Each element is wrapped in a small internal@unchecked Sendablebox and forwarded to the existing@Sendableoverload, so no array cast happens. The new overloads are Swift-only (not@objc); the Objective-C surface is unchanged.The 4.0 changeset no longer tells users to retype their arrays as
[BeforeSendBlock], since that workaround isn't needed anymore.💚 How did you test it?
make testwith signal 6 and the cast message above before the fix.PostHogConfigTest,PostHogLogsQueueTest); they pass after the fix and the data-race warning is gone.setBeforeSend([{ $0 }])@Sendableoverload@Sendableoverload[BeforeSendBlock]variable@Sendableoverload@SendableoverloadsetBeforeSend({ $0 }, { $0 })@Sendableoverload@Sendableoverload[(Event) -> Event?]variable, before fix[(Event) -> Event?]variable, after fixmake lint,make testandmake buildSdk(see Agent context for a worktree caveat).📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset fileEdited the existing
.changeset/v4-major-release.mdinstead of adding a new changeset.🤖 Agent context
Autonomy: Human-driven (agent-assisted)
@_disfavoredOverloadplus a per-element box. The box is a private generic class next toBeforeSendChain, shared by events and logs.api/posthog-ios.public-api.txt: only the two new method lines were added. Runningmake apiUpdatelocally with Xcode 26.4.1 also rewrote unrelated lines to show@Sendable, while CI'slatest-stableXcode passes on the current rendering, so those lines were left alone.make apiUpdateandmake buildSdkhad to run from a copy of the worktree in a directory namedposthog-ios, because the example projects reference../../posthog-ios.make lintandmake buildSdkpass, and the focusedbeforeSendtests pass (20 tests). Fullmake testran 1155 tests and reported 14 issues, none in code this PR touches. 13 arePostHogStorageapp-group tests that fail on this machine because macOS denies writes under~/Library/Group Containers; they fail the same way on the base branch. The other is the timing-sensitivePostHogLogsQueue"add() racing with flush()" test, which also fails on the base branch on this loaded machine.🤖 Generated with Claude Code