You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(api): block multivariate option changes during live experiments - #8917
The API now rejects multivariate option changes on a feature that is in a running experiment:
MultivariateFeatureOptionViewSet create, update and destroy return 400 while the feature has a running experiment in any environment. Multivariate options belong to the feature, so one experiment locks them everywhere.
Feature create/update: the nested multivariate_options are rejected only when the submitted options differ from the stored ones. The frontend resubmits the whole feature on save, so other fields (description, tags, etc.) still save on a frozen feature.
New helper has_live_experiment in experimentation/feature_state_metadata.py.
Frontend: the create-feature modal already disables the add, edit and remove variant controls when the feature is frozen (useFeatureExperimentFreeze). That check only looks at experiments running in the current environment. Blocking variant edits from the UI for a feature running in another environment needs a project-level experiments query, which is not part of this PR. The API rejects those edits regardless.
How did you test this code?
Backend unit tests:
cd api
pytest tests/unit/features/multivariate/test_unit_multivariate_live_experiment.py
pytest tests/unit/features/ -k "multivariate or mv or feature_serializer or create_feature or update_feature"
The new test module covers create, update and delete of MV options during a live experiment (400), the same create still working without one, and feature PATCH with changed options (400) versus unchanged options plus a metadata change (200). The existing multivariate and feature suites pass.
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.
No actionable comments were generated in the recent review.
Walkthrough
The change adds checks that prevent multivariate variation changes while an experiment is running or paused. The checks cover direct option operations, feature updates, and environment imports. Transactions and feature-row locks are added to relevant write paths, and starting an experiment locks its feature row. Tests cover rejected and permitted operations, imports, and cross-environment cases. Documentation updates the experiment prerequisite and six event source line references.
The change blocks multivariate option edits during running or paused experiments, and its main paths are covered by tests. Some narrow race windows and a pre-existing cross-project authorization gap remain. These are bounded concerns and are worth owner awareness rather than blocking the merge.
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
First of all, thanks for picking this one, that is a very complex one given the model of the MV options and their weight allocation (mix between project and environment level). It also implies some design decisions that are still under discussions
There are some blockers right now, the main changes we need to move it forward for now would be:
Unchanged updates to a variant must not be rejected, otherwise saving the flag in another environment breaks
The lock should cover paused experiments as well as running ones
The import guard is too wide, and the new transaction changes the behaviour of every import
The error should come back in one shape and name the environment running the experiment
I described some tests that should help implementing the first point.
This behavior could also be rewarded with a one-line mention/warning in the doc 👍
@Zaimwa9 thanks for the detailed review, the test table made the first point easy to pin down. Pushed a91b390:
perform_update only checks for an experiment when the type, value, key or feature of the variation actually changes. default_percentage_allocation is left out, and create and delete stay blocked. The tests from your table are in, including the replay of the dashboard save in environment B while A runs the experiment.
Paused experiments lock the variations too. The live_experiment fixture runs every test for both running and paused.
_import_features_for_environment is back to how it was. A first pass over the import rejects it before anything is written, and only when it would add a variation or when the target is the experiment's environment. Importing existing variations into another environment goes through.
The error is {"detail": "..."} everywhere and names the environment.
The check in validate_multivariate_options is gone, only update checks now.
Added a sentence about the lock to the Create an Experiment page.
Could you take another look when you get a chance?
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fast changes. However we had to re-think the behavior around destructive imports in an environment with features running an experiment, which would be great to adapt.
@Zaimwa9 updated in 8682e20: get_live_experiment now takes an optional environment_id, and destructive imports skip features that have a running or paused experiment in the target environment (including when another environment also has an experiment) or would add a variation while an experiment is active anywhere, letting the rest of the batch import.
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
So basically 8682e20 is exactly what we wanted and 7aa28e9 is fine too.
It would be preferrable to revert the two following commits (e81f2fd and 81166e8). They start creeping out of scope and it modifies the path every flag save goes through. Let's leave them out of this work.
Those comments don't need to be addressed, i'll answer to the cubic threads, the concurrency risk is too rare to justify extra locking on these paths.
So just need:
reverting the 2 commits
resolving conflicts
maybe add one doc mention that an overwrite import skips flags under an experiment
Multivariate options were only restricted in the frontend. The API
accepted create, update and delete on the MV options endpoint, and
changed nested multivariate_options on feature update, even when the
feature was in a running experiment.
Reject these writes with a 400 while any environment has a running
experiment for the feature. On feature update, only reject when the
submitted options differ from the stored ones, so other feature fields
can still be saved.
ClosesFlagsmith#8766
- mv-options PUT only checks for an experiment when the variation itself
changes (type, value, key or feature). default_percentage_allocation is
ignored, so the dashboard can still save other environments.
- Paused experiments lock the variations as well as running ones.
- Imports keep their original per-feature behaviour. A first pass rejects
the import before anything is written, and only when it would add a
variation or overwrite the environment running the experiment.
- One error shape, {"detail": ...}, naming the experiment environment.
- Drop the duplicate check in validate_multivariate_options.
- Mention the lock in the experiment docs.
…e import
Skip individual features in _import_features_for_environment instead of
failing the whole import when a feature has a running or paused experiment
in the target environment or when the import would add a variation while an
experiment is running or paused in any environment.
@Zaimwa9 updated and rebased onto main (head ff02709):
Dropped e81f2fd and 81166e8 so the branch sits back on the 8682e20 + 7aa28e9 scope.
Resolved the conflicts in api/experimentation/services.py and docs/docs/deployment-self-hosting/observability/_events-catalogue.md (verified against flagsmith docgen events).
Added a note to docs/docs/administration-and-security/data-management/bulk-import-and-export.md explaining that Overwrite Destructive skips features with a running or paused experiment in the target environment, or when the import would add a variation while an experiment is active in any environment.
❌ Patch coverage is 99.61832% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 98.07%. Comparing base (02dd781) to head (ff02709). ⚠️ Report is 22 commits behind head on main.
The reason will be displayed to describe this comment to others. Learn more.
I think that's what codecov is flagging, sorry I missed it but I believe this path is unreachable because a request without a feature would fail the serializer validation.
It should be safe to remove and please codecov
Thanks @Zaimwa9. The one uncovered line was a feature mismatch check in perform_create that can't be reached, since create() already rejects a mismatched feature in the body with a 400 (covered by test_create_mv_option__body_feature_mismatches_url__returns_400). I removed it in 7c2d410, so codecov/patch should pass now.
The Fix Documentation Artefacts failure looks unrelated to the code. The job dies at "Generate GitHub App token with push access" because the app client-id secret isn't available to workflows from forks. The docs changes in this PR were already checked against flagsmith docgen events, so I think a maintainer needs to re-run that job or push the artefacts.
The reason will be displayed to describe this comment to others. Learn more.
0 issues found across 1 file (changes from recent commits).
Confidence score: 5/5
Automated review surfaced no issues in the provided summaries.
No files require special attention.
Requires human review: Auto-approval skipped because cubic reviewed only this push, not the earlier force-push. Comment @cubic review to review the whole pull request.
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
apiIssue related to the REST APIdocsDocumentation updates
2 participants
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.
Thanks for submitting a PR! Please check the boxes below:
docs/if required so people know about the feature.Changes
Contributes to #8766
The API now rejects multivariate option changes on a feature that is in a running experiment:
MultivariateFeatureOptionViewSetcreate, update and destroy return 400 while the feature has a running experiment in any environment. Multivariate options belong to the feature, so one experiment locks them everywhere.multivariate_optionsare rejected only when the submitted options differ from the stored ones. The frontend resubmits the whole feature on save, so other fields (description, tags, etc.) still save on a frozen feature.has_live_experimentinexperimentation/feature_state_metadata.py.Frontend: the create-feature modal already disables the add, edit and remove variant controls when the feature is frozen (
useFeatureExperimentFreeze). That check only looks at experiments running in the current environment. Blocking variant edits from the UI for a feature running in another environment needs a project-level experiments query, which is not part of this PR. The API rejects those edits regardless.How did you test this code?
Backend unit tests:
The new test module covers create, update and delete of MV options during a live experiment (400), the same create still working without one, and feature PATCH with changed options (400) versus unchanged options plus a metadata change (200). The existing multivariate and feature suites pass.