Skip to content

[FrameworkBundle] Fix service reset between tests #58240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2024

Conversation

HypeMC
Copy link
Contributor

@HypeMC HypeMC commented Sep 12, 2024

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues -
License MIT

Currently, not all services are reset between tests. If a service uses the kernel.reset tag but does not implement the ResetInterface, it never gets reset.

if ($container instanceof ResetInterface) {
if ($container->has('services_resetter')) {
$container->get('services_resetter')->reset();
} elseif ($container instanceof ResetInterface) {
$container->reset();
Copy link
Member

Choose a reason for hiding this comment

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

$container->reset() must still be called, to reset the instances hold by the container itself (see the code of line 302).

Btw, if you ensure that the services_resetter service is instantiated, the container reset call will then call it (as ServiceResetter implements the ResetInterface).

The reason it was not called is because Container::reset only resets instantiated resettable services

Copy link
Contributor Author

@HypeMC HypeMC Sep 12, 2024

Choose a reason for hiding this comment

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

@stof Fixed, see https://github.com/symfony/symfony/compare/549ada5257c771da92d51aff76e224e1ac5d31f4..5491aa54a3205acfaf5ba62c134bddc26d794a36

Initially, I wanted to just instantiate the services_resetter and let Container::reset() call reset on it, but that didn’t work because the services_resetter only resets instantiated services. Because of line 302, by the time it’s called, there aren’t any instantiated services left.

The downside of the current approach is that some services will get reset twice, but I don’t see a better solution at the moment.

Copy link
Member

Choose a reason for hiding this comment

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

ah indeed. The container resets itself before resetting the services.

I think this might actually be an issue if the reset method of a service triggers the instantiation of some other service. I think the resetting of the container state should probably be moved after the loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@HypeMC HypeMC force-pushed the fix-service-reset-between-tests branch 2 times, most recently from 5491aa5 to 53d0f56 Compare September 12, 2024 18:18
@HypeMC HypeMC force-pushed the fix-service-reset-between-tests branch from 53d0f56 to fb1ae1a Compare September 12, 2024 20:01
@xabbuh
Copy link
Member

xabbuh commented Sep 13, 2024

Isn’t the test failure on PHP 8.2 related?

@HypeMC
Copy link
Contributor Author

HypeMC commented Sep 13, 2024

Isn’t the test failure on PHP 8.2 related?

@xabbuh Yes, but that's high-deps and if I'm not mistaking, that'll get resolved when merged upstream.

@nicolas-grekas
Copy link
Member

Thank you @HypeMC.

@nicolas-grekas nicolas-grekas merged commit b2201c3 into symfony:5.4 Sep 16, 2024
10 of 12 checks passed
@HypeMC HypeMC deleted the fix-service-reset-between-tests branch September 16, 2024 13:50
xabbuh added a commit that referenced this pull request Sep 20, 2024
…nel is shut down (jderusse)

This PR was merged into the 5.4 branch.

Discussion
----------

[FrameworkBundle] Do not access the container when the kernel is shut down

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

#58240 play with the container after the kernel was shut down.
This is an issue when the kernel performs a cleanup operation. For instance, the `@Nyholm` TestBundle [deletes cache files](https://github.com/SymfonyTest/symfony-bundle-test/blob/241bf0e2f00f28f7327113570ab20e24a5828829/src/TestKernel.php#L184-L199) as a result, the service is not accessible anymore and triggers errors.

This PR move the call to `container->get` before `kernel::shutdown`

Commits
-------

e68f88c [FrameworkBundle] Do not access the container when the kernel is shut down
This was referenced Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants