[6.x] Scope mutable services#19242
Merged
Merged
Conversation
📚 Storybook previews@craftcms/cp — open Storybook No changed components detected in this Storybook. resources/js — open Storybook No changed components detected in this Storybook. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses lifecycle/state leakage in long-running Craft CMS processes by scoping several mutable services to the current operation/request and avoiding persisting scoped service instances inside singletons (including legacy Yii service wrappers).
Changes:
- Marked mutable core services (
Assets,Gql,RouteTokens,Users,UserPermissions) as#[Scoped]rather than#[Singleton]. - Updated singleton/legacy call sites to resolve scoped services at call time (rather than storing them from the constructor).
- Added tests to validate scoped bindings and legacy wrapper behavior, and documented the fix in the changelog.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| yii2-adapter/tests-laravel/Legacy/UserPermissionsLifecycleTest.php | Adds a regression test ensuring the legacy Yii service resolves the current scoped core UserPermissions instance. |
| yii2-adapter/legacy/services/UserPermissions.php | Stops caching the core service in the legacy wrapper; resolves it lazily per call to respect scoped lifecycles. |
| tests/Unit/Support/LifecycleBindingsTest.php | Adds a unit test asserting selected services are annotated as #[Scoped]. |
| src/User/Users.php | Switches Users service from #[Singleton] to #[Scoped]. |
| src/User/UserPermissions.php | Switches UserPermissions service from #[Singleton] to #[Scoped]. |
| src/RouteToken/RouteTokens.php | Switches RouteTokens service from #[Singleton] to #[Scoped]. |
| src/Gql/Gql.php | Switches Gql service from #[Singleton] to #[Scoped]. |
| src/Asset/Volumes.php | Removes persisted Assets dependency from this singleton; resolves Assets via the container when needed. |
| src/Asset/Assets.php | Switches Assets service to #[Scoped] and tightens the temp-folder cache docblock. |
| CHANGELOG.md | Notes the lifecycle leak fix in the Unreleased section. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
14cd5ec to
29bbe52
Compare
# Conflicts: # CHANGELOG.md
[ci skip]
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description