-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Implement resettable containers #15185
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
Conversation
public function clear() | ||
{ | ||
if (!empty($this->scopedServices)) { | ||
throw new RuntimeException('Clearing the container is not allowed when a scope is active.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that I added this check because if you are calling clear()
while a scope is active, we can be totally sure that it is a misuse of the method.
I renamed it to |
And others are not aware of it, sadly. Maybe a little section on http://symfony.com/doc/current/components/dependency_injection/introduction.html is enough, what do you think ? |
@mickaelandrieu it does not belong to the introduction IMO, given it is an edge case feature |
Maybe this behavior can be used for Testing purpose? When you need to run multiple kernels in a test and check some datas in the container, you may have a reference of it, so clearing the container when shutting the kernel down might be good anyway. |
@Pierstoval I don't understand what you mean |
For example, when running a phpunit test, one might test compiler passes, so in a single test, he instantiates a first kernel, gets the container and make assertions to check consistency, and then instantiates another kernel, also make assertions on the new container. I don't know if it's clear enough, but if it's not, then don't mind :p |
@Pierstoval unit testing a compiler pass should not involve booting a kernel (this would be functional testing of the whole app, which is totally inefficient for that). |
I don't think we need to doc anything here :) |
yeah, for the few cases where it could be useful to know about it, we can probably tell developers about this feature. I'm thinking it might maybe benefit Drupal in their testing environment if they recreate a container each time. As they don't use the kernel, they won't benefit form the automatic usage of the feature. but this would be only once they move to 2.8+ though. /cc @Crell |
@@ -13,6 +13,7 @@ | |||
|
|||
use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator; | |||
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; | |||
use Symfony\Component\DependencyInjection\ResettableContainerInterface; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be added at the alphabetical order
👍 |
public function reset() | ||
{ | ||
if (!empty($this->scopedServices)) { | ||
throw new RuntimeException('Resetting the container is not allowed when a scope is active.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LogicException?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
I truly believe it should be documented. Every project using the single components instead of the full stack framework could make use of it and there is some case here it could be very useful. Maybe an entry in the cookbook? Actually I don't see where it could be in the component documentation... Or just leave it undocumented and rely to the docblock? As you said this feature will not be used by a lot of people. For the record, phpbb/phpbb:master will make use of it as soon as it will be merged (especially because there is one edge case where we have to build a first container to build the real one (dynamic extension/bundle definition of loading...)) |
@@ -376,6 +376,18 @@ public function initialized($id) | |||
} | |||
|
|||
/** | |||
* Clears shared services from the container. | |||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely document the side effects of calling this method, not only the intended effects (memory) but also the side effects (a shared service will be re-created if get() is called).
@stof Can you address the comments before merging? |
@Nicofuma Yea, I was thinking that we rely on the PHP documentation for this. And also, since the |
02ab521
to
1888eff
Compare
updated. Status: needs review |
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be LogicException
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
This allows to remove references to all services during shutdown, giving much more chances to destruct services and the container through refcounting rather than waiting GC, as it will break cycles between the container and container-aware services.
1888eff
to
4457745
Compare
👍 |
2 similar comments
👍 |
👍 |
👍 |
FYI, I tried this patch on my work project (I backported the patch on Symfony 2.6 and then ran my Behat testsuite). |
Thank you @stof. |
This PR was merged into the 2.8 branch. Discussion ---------- Implement resettable containers | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | small one | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13448 | License | MIT | Doc PR | n/a This allows to release remove references to all services during shutdown, giving much more chances to destruct services and the container through refcounting rather than waiting GC, as it will break cycles between the container and container-aware services. There is a small BC break for a very edge case: if someone keeps a reference to the container and then shutdowns the kernel, the container would now be cleared and so would not work as intended anymore. But I don't think it is a supported use case. If you shutdown the kernel, the container of this kernel is released by the kernel and should not be used anymore IMO. Thus, shutting down the kernel generally does not happen except during tests on teardown. I'm not sure a doc PR is needed here: users of the fullstack framework should never use this feature (the kernel is using it for them). What do you think @weaverryan ? Commits ------- 4457745 Implement resettable containers
…e/Test/KernelTestCase (nicolas-grekas) This PR was merged into the 2.8 branch. Discussion ---------- [HttpKernel] Don't reset on shutdown but in FrameworkBundle/Test/KernelTestCase | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15185 | License | MIT | Doc PR | - While trying to migrate Blackfire to 2.8-beta, I found this BC-break: by resetting the container on kernel shut-down, functional tests are broken when they need to use the container after a call to `$this->client->request()`. Broken because e.g . the session or the profiler state is lost between consecutive requests in the same test, and because a call to $container->get('kernel') throws a synthetic-related exception. This PR fixes the BC-break by reverting to the <=2.7 behavior (not resetting the container on kernel shut-down), and moving resetting to the KernelTestCase. Commits ------- baad4da [HttpKernel] Don't reset on shutdown but in FrameworkBundle/Test/KernelTestCase
This allows to release remove references to all services during shutdown, giving much more chances to destruct services and the container through refcounting rather than waiting GC, as it will break cycles between the container and container-aware services.
There is a small BC break for a very edge case: if someone keeps a reference to the container and then shutdowns the kernel, the container would now be cleared and so would not work as intended anymore. But I don't think it is a supported use case. If you shutdown the kernel, the container of this kernel is released by the kernel and should not be used anymore IMO.
Thus, shutting down the kernel generally does not happen except during tests on teardown.
I'm not sure a doc PR is needed here: users of the fullstack framework should never use this feature (the kernel is using it for them). What do you think @weaverryan ?