Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Add the "kernel.close" tag to close services on kernel shutdown - #65989

Open
Amoifr wants to merge 1 commit into
symfony:8.2from
Amoifr:feat-65947-kernel-close
Open

[DependencyInjection] Add the "kernel.close" tag to close services on kernel shutdown#65989
Amoifr wants to merge 1 commit into
symfony:8.2from
Amoifr:feat-65947-kernel-close

Conversation

@Amoifr

@Amoifr Amoifr commented Sep 11, 2026

Copy link
Copy Markdown
Contributor
Q A
Branch? 8.2
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #65947
License MIT

As described in #65947, the kernel can reset services between requests but has no way to release the resources a service holds when it shuts down: bundles override Bundle::shutdown() and make a coordinating service public to do it.

This adds a kernel.close tag, mirroring kernel.reset:

$services->set('app.log_handler', StreamHandler::class)
    ->tag('kernel.close'); // calls close(), or pass ['method' => '...'] for another one

AbstractKernel::shutdown() closes the tagged services once the bundles are shut down, so reboot() and KernelTestCase::ensureKernelShutdown() release them too.

  • Services are referenced with IGNORE_ON_UNINITIALIZED_REFERENCE: one that was never used is not instantiated just to be closed.
  • method defaults to close, and on_invalid: ignore works as it does for kernel.reset.
  • services_closer reuses ServicesResetter, which already skips uninitialized lazy objects and keeps closing the other services when one throws. The container is cleared even then.
  • A non-shared service is rejected at compile time, since the container keeps no instance of it to close.

I went with kernel.close rather than kernel.shutdown because it names the action, as kernel.reset does. Happy to rename it, or to give the closer its own class if reusing the resetter reads oddly.

@nicolas-grekas

Copy link
Copy Markdown
Member

Rebased on 8.2, resolving conflicts in the DependencyInjection changelog and in AbstractKernelTest.

Fixed testShutdownClearsContainerWhenClosingFails: the $this->fail() call sat inside its own catch (\RuntimeException $e) block, and PHPUnit\Framework\AssertionFailedError extends \RuntimeException, so the failure was swallowed and reported as a string mismatch. It now sets a flag and asserts after the block.

Also shortened the changelog entry to one line.

@Amoifr

Amoifr commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the rebase and for fixing that test. AssertionFailedError extending \RuntimeException is a nasty one: my catch block was swallowing the very failure it existed to report.

The three reds all look unrelated to this PR. testSchemeRedirect and the two DoctrineOutboxIntegrationTest errors fail identically on #66028 and were fixed on 8.2 at 11:14 and 11:21 (6cc2cdda, b96ffaca), just after this run. FFICasterTest::testCastNonTrailingCharPointer passes on the current 8.2 too. A rebase should turn all three green.

@Amoifr
Amoifr force-pushed the feat-65947-kernel-close branch from 77e7d16 to 7057bf3 Compare September 13, 2026 17:06
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.

Add a generic way to close services on kernel shutdown (similar to services_resetter, but for shutdown)

3 participants