Repository navigation
fix: Start the primary environment first, then secondary environments by name - #572
Conversation
… by name Co-authored-by: Cursor <[email protected]>
There was a problem hiding this comment.
Risk: medium. Not approved — Rule 2: the PR author is not an SDK team member, so this automation cannot approve. Cursor Bugbot completed with no findings; this change is above the low-risk approval threshold, so human review is needed and no reviewers were assigned.
Sent by Cursor Approval Agent: SDK - Pull Request Router and Approver
There was a problem hiding this comment.
Risk: low.
Automated risk review (dry run): Low
This PR would be automatically approved.
Why: LDClient.start now creates the primary environment first and then secondaries by name, with testEnvironmentsAreStartedPrimaryFirstThenSecondariesByName covering that order. The change is small, tested, and Bugbot reported no issues.
If you disagree with this review, share this PR in #feedback-agent-approvals or submit a changes-requested review explaining why.
Sent by Cursor Approval Agent: Dry-Run PR Approver
…play on it only (#294) ## Summary With `LDConfig.secondaryMobileKeys` set, the iOS SDK starts one `LDClient` per environment and, for each, calls every plugin's `getHooks` and then `register`. It visits the environments by iterating a dictionary, whose order changes from launch to launch, and both plugins kept whichever client registered first: - **Wrong environment.** On some launches the observability pipeline (`highlightProjectId`, the sampling config, OTLP export) and Session Replay, which takes its key from the observability context, were bound to a **secondary** mobile key. Every other environment logged `observabilityInstanceAlreadyExist` / `sessionReplayInstanceAlreadyExist`. - **Session Replay on secondary clients.** Session Replay handed the same hook to every client, so identifies on secondary clients (including each one's initial identify) reached the replay. ## Changes - **Observability:** `register` returns early unless `LDClient.get() === client`, so the pipeline always belongs to the primary environment. The SDK publishes each client before registering plugins with it, so that holds for the primary client only, whatever the order (true back to 11.5.0, the oldest SDK this package allows). Its hooks are unchanged: every environment's client still shares the one `ObservabilityHook`, so flag evaluations, identifies and tracks from all clients are still recorded. - **Session Replay:** `register` has the same primary check. `getHooks` creates a hook per environment, kept by mobile key, and only the primary environment's hook is given the replay service as its delegate. Secondary clients' hooks stay delegate-less, and the hook already returns early without a delegate, so they record nothing, with no extra check per call. The primary's initial identify still reaches Session Replay, as before. Related SDK change making the order deterministic (primary first): launchdarkly/ios-client-sdk#572. This PR does not depend on it. ## Tests - New `MultipleEnvironmentsTests` starts a real offline client with four secondary keys and both the `Otel` and `SessionReplay` plugins. It checks that the observability context uses the primary key, that the shared Observability hook is wired, that the primary's Session Replay hook has the replay service, and that every secondary's Session Replay hook does not. - It shares `LDReplay.shared` with `LDReplayFlushTests`, so both now sit under a serialized `SharedStateTests` parent suite (the flush test file is re-indented only). - Without the `register` guards the new test fails whenever a secondary environment is visited first (4 of 5 runs locally); with them it passes. Full suite passes on the iOS simulator. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Overview** > When **`LDConfig.secondaryMobileKeys`** is used, the observability pipeline and Session Replay could attach to whichever environment registered first, and Session Replay could record events from secondary clients. > > **Observability** now initializes the shared pipeline only when **`register`** runs for the primary client (`LDClient.get() === client`), while **`getHooks`** still creates one **`ObservabilityHook`** per environment (tagged with that environment's mobile key) wired to the same exporter. **`track`** spans still emit for all environments, but the Session Replay **`track`** broadcast is limited to the primary mobile key. > > **Session Replay** uses the same primary-only **`register`** guard, keeps a hook per mobile key, and only wires the primary hook's delegate to the replay service. Secondary hooks stay inert without per-call checks. > > Adds **`MultipleEnvironmentsTests`** (plus a serialized **`SharedStateTests`** parent for shared global state) to verify primary binding and primary-only replay **`Track`** recording. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 260e6fb. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Cursor <[email protected]>
🤖 I have created a release *beep* *boop* --- ## [11.8.1](11.8.0...11.8.1) (2026-11-23) ### Bug Fixes * avoid Foundation's back-deployed Date comparison operators ([#575](#575)) ([1e18137](1e18137)) * Record no variation and a WRONG_TYPE reason in events when a typed evaluation has the wrong type ([#554](#554)) ([d76e12e](d76e12e)) * Start the primary environment first, then secondary environments by name ([#572](#572)) ([30856c4](30856c4)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Overview** > **Release 11.8.1** bumps the SDK version from `11.8.0` to `11.8.1` across packaging and metadata (Release Please manifest, `LaunchDarkly.podspec`, Xcode `MARKETING_VERSION` / dylib versions, `ReportingConsts.sdkVersion`, and README SPM pin). > > The changelog for this patch documents three **bug fixes** shipped in the release: > > **Debug events on newer Apple SDKs:** `FeatureFlag.shouldCreateDebugEvents` no longer compares `Date` values with `<=` / `>=`. It uses `timeIntervalSinceReferenceDate` instead, avoiding crashes when Foundation’s back-deployed Date operators are missing on some iOS 27.2 beta builds. > > **Typed evaluation analytics:** When a flag value cannot be decoded to the requested type, evaluations now emit **`WRONG_TYPE`** with **no variation index** in both the returned detail and recorded flag events (via `withEvaluationResult(variation: nil, …)`). > > **Multi-environment startup order:** `LDClient.start` initializes the **primary** environment first, then **secondary** environments in **stable alphabetical order by name**, so client creation and plugin registration are deterministic across launches. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 4ad6acb. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: LaunchDarklyReleaseBot <[email protected]>


Requirements
Related issues
Found while hardening the Observability and Session Replay plugins (
swift-launchdarkly-observability) for apps configured withLDConfig.secondaryMobileKeys.Describe the solution you've provided
LDClient.startcreated one client per environment, and registered every plugin with it, by iterating a[String: String]dictionary of environment names to mobile keys. Swift seeds dictionary hashing per process, so that order changed from launch to launch, and the primary environment was often not the first one visited.A plugin that keeps a single instance — such as the Observability plugin, which publishes one pipeline and rejects later registrations — therefore attached to whichever environment happened to come first, which could be a secondary one. Its telemetry then went to the wrong environment, on some launches and not others.
startnow builds an ordered list: the primary environment first, then the secondary environments sorted by name. Clients are created, and plugins registered with them, in that order on every launch. Nothing else about startup changes; the completion still fires once every client has started.This is defensive: plugins should still check which client they are registered with (the Observability plugin is getting that check separately), but a stable, primary-first order removes the launch-to-launch nondeterminism for any plugin that does not.
Describe alternatives you've considered
EnvironmentMetadata, so thatgetHookscould tell a secondary environment apart. Worth doing separately; it is an API addition, while this change is not.Additional context
testEnvironmentsAreStartedPrimaryFirstThenSecondariesByNamerecords the order ofgetHooksandregistercalls across a primary and five secondaries, and checks that the first client registered is the oneLDClient.get()returns. Against the previous code it fails (a secondary was registered first); with this change it passes, along with the rest ofLDClientPluginsSpecandLDClientSpecon the iOS simulator.Note
Overview
LDClient.startno longer walks a[String: String]dictionary of environment names to mobile keys when creating clients and registering plugins. It builds a fixedenvironmentslist—the primary key first, then secondaries sorted by name—so creation and pluginregister/getHookscalls happen in the same order on every launch.This removes launch-to-launch nondeterminism that could register single-instance plugins (e.g. Observability) with a secondary client first and send telemetry to the wrong environment.
Tests add
testEnvironmentsAreStartedPrimaryFirstThenSecondariesByNameand extendStubPluginwith anonGetHookshook to assert ordering across six environments.Reviewed by Cursor Bugbot for commit e338a16. Bugbot is set up for automated code reviews on this repo. Configure here.