Repository navigation
chore!: mark off-main public callbacks @Sendable for v4 #914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
turnipdabeets
merged 4 commits into
chore/v4-reload-flags-result
from
chore/v4-sendable-callbacks
Oct 7, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4a42bf0
chore!: mark off-main public callbacks @Sendable for v4
turnipdabeets 7ed1e93
chore: match the public API snapshot to CI's Xcode 26.3 output
turnipdabeets 2abbfce
fix: store bridged flag values in PostHogFeatureFlagsLoaded
turnipdabeets be9478f
docs: clarify the Sendable callbacks changeset
turnipdabeets File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| 'posthog-ios': major | ||
| --- | ||
|
|
||
| **Breaking:** mark callbacks the SDK can call off the main thread `@Sendable` β `reloadFeatureFlags(_:)`, the `BeforeSendBlock` and `PostHogBeforeSendLogBlock` used by `setBeforeSend(_:)` on `PostHogConfig` and `PostHogLogsConfig`, `PostHogConfig.getAnonymousId`, `PostHogConfig.pushIdentityProvider` and its `completion`, and `sessionReplayConfig.captureLogsConfig.logSanitizer` β and make `PostHogFeatureFlagsLoaded` `Sendable`. This fixes a crash in Swift 6 apps when a `reloadFeatureFlags(_:)` callback is written in main-actor code. | ||
|
|
||
| To update main-actor state from `reloadFeatureFlags(_:)` or `pushIdentityProvider`, hop with `Task { @MainActor in ... }`. The other callbacks return a value immediately, so they must not read main-actor state. Declare closures you store in your own variables as `@Sendable` to avoid new Swift 5 warnings. Use the `BeforeSendBlock` and `PostHogBeforeSendLogBlock` typealiases for any `setBeforeSend(_:)` arrays you build or pass around, including your own helpers: in Swift 5 mode, converting an array between the old function type and these types compiles but crashes at runtime, in either direction. |
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
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
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // | ||
| // PostHogCallbackIsolationTests.swift | ||
| // PostHogSwift6Tests | ||
| // | ||
|
|
||
| import Foundation | ||
| import PostHog | ||
| import Testing | ||
|
|
||
| /// Calls the public callback APIs from main-actor code, as a Swift 6 app would. | ||
| /// | ||
| /// In Swift 6 mode, a closure written in main-actor code and passed as a non-`Sendable` | ||
| /// parameter is main-actor isolated, and the runtime traps if the SDK calls it off the main thread. | ||
| @MainActor | ||
| @Suite(.timeLimit(.minutes(1))) | ||
| struct PostHogCallbackIsolationTests { | ||
| @MainActor final class AppState { | ||
| var reloadResult: PostHogFeatureFlagsLoaded? | ||
| var loaded: PostHogFeatureFlagsLoaded? | ||
| var deliveredOnMain = false | ||
| } | ||
|
|
||
| /// Nothing listens on this port, so requests fail fast without leaving the machine. | ||
| private func makeSDK(bootstrapFlags: [String: Any]? = nil) -> PostHogSDK { | ||
| let config = PostHogConfig(projectToken: "phc_swift6_\(UUID().uuidString)", host: "http://127.0.0.1:9") | ||
| config.captureApplicationLifecycleEvents = false | ||
| config.preloadFeatureFlags = false | ||
| if let bootstrapFlags { | ||
| let bootstrap = PostHogBootstrapConfig() | ||
| bootstrap.featureFlags = bootstrapFlags | ||
| config.bootstrap = bootstrap | ||
| } | ||
| return PostHogSDK.with(config) | ||
| } | ||
|
|
||
| @Test("reloadFeatureFlags callback written in main-actor code runs without an isolation trap") | ||
| func reloadCallbackFromMainActorCode() async { | ||
| let sdk = makeSDK() | ||
| defer { sdk.close() } | ||
| let state = AppState() | ||
|
|
||
| // The SDK invokes this from its network completion, off the main thread. | ||
| await withCheckedContinuation { continuation in | ||
| sdk.reloadFeatureFlags { result in | ||
| // Sending the result into the hop requires it to be Sendable. | ||
| Task { @MainActor in | ||
| state.reloadResult = result | ||
| continuation.resume() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #expect(state.reloadResult != nil) | ||
| } | ||
|
|
||
| @Test("onFeatureFlags callback written in main-actor code runs on main with a Sendable payload") | ||
| func onFeatureFlagsDeliversOnMainActor() async { | ||
| let sdk = makeSDK(bootstrapFlags: ["swift6-flag": true]) | ||
| defer { sdk.close() } | ||
| let state = AppState() | ||
|
|
||
| var subscription: PostHogFeatureFlagsSubscription? | ||
| await withCheckedContinuation { continuation in | ||
| subscription = sdk.onFeatureFlags { loaded in | ||
| // Touches main-actor state directly, with no hop. | ||
| guard state.loaded == nil else { return } | ||
| state.loaded = loaded | ||
| state.deliveredOnMain = Thread.isMainThread | ||
| continuation.resume() | ||
| } | ||
| } | ||
| subscription?.unsubscribe() | ||
|
|
||
| #expect(state.deliveredOnMain) | ||
| // Sending the payload to another task requires it to be Sendable. | ||
| let loaded = state.loaded | ||
| let flags = await Task.detached { loaded?.flags ?? [] }.value | ||
| #expect(flags == ["swift6-flag"]) | ||
| } | ||
|
|
||
| @Test("onFeatureFlags payload keeps its variant values when a mutable bootstrap value changes") | ||
| func onFeatureFlagsPayloadIsImmutable() async { | ||
| let source = NSMutableString(string: "original") | ||
| let sdk = makeSDK(bootstrapFlags: ["swift6-variant": source]) | ||
| defer { sdk.close() } | ||
| let state = AppState() | ||
|
|
||
| var subscription: PostHogFeatureFlagsSubscription? | ||
| await withCheckedContinuation { continuation in | ||
| subscription = sdk.onFeatureFlags { loaded in | ||
| guard state.loaded == nil else { return } | ||
| state.loaded = loaded | ||
| continuation.resume() | ||
| } | ||
| } | ||
| subscription?.unsubscribe() | ||
|
|
||
| source.setString("changed") | ||
| let loaded = state.loaded | ||
| let variant = await Task.detached { loaded?.variants["swift6-variant"] as? String }.value | ||
| #expect(variant == "original") | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.