Skip to content

How the Kill Switch Works

Lionel Serrao edited this page Nov 29, 2026 · 1 revision

How the Kill Switch Works

The deep dive. Code: extension/src/incidentOps.ts, which drives the steps, and api.ts, which makes the requests. The same code runs in the extension, the unit tests and the live end-to-end tests.

The rule: users first, keep going, then verify

flowchart TD
    A["Read every state of the flag in the environment:<br/>default + segment overrides + identity overrides"] --> B["Turn off everything that is on<br/>(one request per state, or one new version)"]
    B -->|"a write is refused"| B2["Note the problem,<br/>carry on with the rest"]
    B2 --> C
    B --> C["Find A/B tests serving the flag:<br/>hidden test segments that were on<br/>+ running tests on this flag"]
    C --> D["For each test: take its traffic away<br/>(experiment rollout endpoint), then pause it"]
    D --> E["Save a restore record:<br/>exactly what went off"]
    E --> F["Read the flag back from Flagsmith"]
    F -->|"nobody gets it"| G["'Flagsmith confirms nobody gets it now'"]
    F -->|"someone still does"| H["Error: who, and what Flagsmith refused<br/>with Try again / Open in Flagsmith"]
Loading

What exactly gets written

State Normal environment Versioned environment (v2)
Environment default PATCH /environments/{key}/featurestates/{id}/ {enabled:false} One new published version: POST /environments/{id}/features/{fid}/versions/ with the default and segment states
Segment override PUT /features/featurestates/{id}/ (value kept) Same new version, matched by segment
Identity override PATCH /environments/{key}/identities/{iid}/featurestates/{id}/ Same as normal
A/B test PATCH /environments/{key}/experiments/{id}/rollout/ {enabled:false, …} then POST …/pause/ Same

Values are never changed, only enabled, so the restore brings back the same values and splits.

Why A/B tests need their own path

An experiment serves users through a hidden system segment named experiment-<id>-rollout. Flagsmith refuses direct writes to it with 409 system_segment_modification. So Flag Responder leaves that segment alone and turns the test's traffic off through the experiment's own rollout endpoint. It also pauses the test, so exposures during the incident don't count towards the results.

The kill switch finds tests from the flag's own overrides, at the moment you press it. A test started a few seconds ago is caught even if the editor hasn't refreshed yet.

What happens when something fails

Situation What Flag Responder does
Flagsmith refuses one write (or the network drops one) Carries on with the rest, saves a restore record for what did go off, and lists what failed
Someone still gets the flag afterwards Says so plainly, with who, and offers Try again and Open in Flagsmith
Experimentation is switched off on the instance Turns off everything else; there is no test to pause
Pausing the test is refused Still takes the test's traffic away; reports the pause failure
Flagsmith doesn't answer Every request gives up after 15 seconds with a readable error
The flag is already off everywhere Writes nothing, offers nothing to restore

The restore

The restore record holds segment ids and identity ids, not feature-state ids. Versioned environments give states new ids with every version, so ids would go stale.

stateDiagram-v2
    [*] --> Live: flag on for someone
    Live --> Unhealthy: alert
    Unhealthy --> Off: kill switch (record saved)
    Off --> Recovered: alert clears
    Recovered --> Live: Turn back on (record used, then dropped)
    Recovered --> KeptOff: Keep it off (record dropped)
    KeptOff --> [*]
Loading

On restore it:

  • turns back on only the states in the record that are still off; anything someone already turned back on is left alone;
  • skips overrides that were deleted since;
  • gives the test its traffic back only if it is still off, and resumes it only if it is still paused;
  • leaves a test that was completed or deleted since, and says so;
  • keeps the record only if something failed that a retry could fix.

The record lives in the VS Code workspace where the kill switch ran.

Proven by

Unit tests in extension/src/test/incidentOps.test.ts and api.test.ts. Live cases C1–C8 and V1–V8 (normal and versioned environments) and X1–X4 (A/B tests) in demo-app/e2e.mjs. See Testing.

Clone this wiki locally