Flagsmith / Flagsmith/flagsmith
Defer change request publishing side effects until transaction commit
- Dominant language
- Python
- Stars
- 6.6k
- Forks
- 567
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 121
Description
## Summary
Defer external side effects from change request publishing until the enclosing database transaction commits successfully.
## Rationale
`ChangeRequestCommitService.commit()` now runs in `transaction.atomic`. `_publish_environment_feature_versions()` dispatches Celery tasks and sends `environment_feature_version_published` before this transaction commits. If a later operation fails, for example when `_publish_segments()` rejects a system segment, the database transaction rolls back but queued tasks and signal-receiver side effects can still run against data that was not committed.
## Affected area
- `api/core/workflows_services.py`
- `ChangeRequestCommitService._publish_environment_feature_versions`
## Required changes
Register `trigger_update_version_webhooks.delay(...)`, `rebuild_environment_document.delay(...)`, and `environment_feature_version_published.send(...)` through `transaction.on_commit()`. Bind each loop value in its callback so callbacks use the correct environment feature version and scheduled timestamp.
## Acceptance criteria
- No Celery task is dispatched when `ChangeRequestCommitService.commit()` rolls back.
- No `environment_feature_version_published` receiver runs when `ChangeRequestCommitService.commit()` rolls back.
- The existing side effects run after a successful transaction commit.
- Tests cover both rollback and successful commit behaviour.
## Backlinks
- Pull request: https://github.com/Flagsmith/flagsmith/pull/8298
- Review comment: https://github.com/Flagsmith/flagsmith/pull/8298#discussion_r3786731701
- Requested by: @srijantrpth
Contributor guide
Research direction
Start in api/core/workflows_services.py at ChangeRequestCommitService._publish_environment_feature_versions and inspect the transaction.atomic flow in commit(). Add tests for rollback and successful commit behaviour, verifying that the listed Celery tasks and environment_feature_version_published signal run only after a successful commit. The issue links pull request #8298 for existing work and review context.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100