Skip to content

fix: Start the primary environment first, then secondary environments by name - #572

Merged
abelonogov-ld merged 1 commit into
v11from
andrey/ordered-environment-start
Nov 23, 2026
Merged

abelonogov-ld merged 1 commit into
v11from
andrey/ordered-environment-start

Conversation

@abelonogov-ld

@abelonogov-ld abelonogov-ld commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Related issues

Found while hardening the Observability and Session Replay plugins (swift-launchdarkly-observability) for apps configured with LDConfig.secondaryMobileKeys.

Describe the solution you've provided

LDClient.start created 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.

start now 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

  • Leaving the order unspecified and relying on plugins alone. Each plugin author would have to know that the order is random, and any that does not would misbehave intermittently.
  • Exposing the environment name in EnvironmentMetadata, so that getHooks could tell a secondary environment apart. Worth doing separately; it is an API addition, while this change is not.

Additional context

testEnvironmentsAreStartedPrimaryFirstThenSecondariesByName records the order of getHooks and register calls across a primary and five secondaries, and checks that the first client registered is the one LDClient.get() returns. Against the previous code it fails (a secondary was registered first); with this change it passes, along with the rest of LDClientPluginsSpec and LDClientSpec on the iOS simulator.


Note

Overview
LDClient.start no longer walks a [String: String] dictionary of environment names to mobile keys when creating clients and registering plugins. It builds a fixed environments list—the primary key first, then secondaries sorted by name—so creation and plugin register / getHooks calls 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 testEnvironmentsAreStartedPrimaryFirstThenSecondariesByName and extend StubPlugin with an onGetHooks hook 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.

@abelonogov-ld
abelonogov-ld requested a review from a team as a code owner November 21, 2026 01:40

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Open in Web View Automation 

Sent by Cursor Approval Agent: SDK - Pull Request Router and Approver

@cursor cursor Bot added the risk:low label Nov 21, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Open in Web View Automation 

Sent by Cursor Approval Agent: Dry-Run PR Approver

abelonogov-ld added a commit to launchdarkly/swift-launchdarkly-observability that referenced this pull request Nov 21, 2026
…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]>
@abelonogov-ld
abelonogov-ld merged commit 30856c4 into v11 Nov 23, 2026
22 checks passed
@abelonogov-ld
abelonogov-ld deleted the andrey/ordered-environment-start branch November 23, 2026 17:17
tanderson-ld pushed a commit that referenced this pull request Nov 23, 2026
🤖 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants