Problem/Motivation

If a middleware is registered with the needs_destruction tag, on a page cache hit, even if it runs before the page cache, it will never be destructed.

This was intentionally added in #2348679: Move the remaining procedural page cache code to the page cache stack middleware but I don't entirely understand why, so going to see what happens if we remove that logic.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3412556

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

catch created an issue. See original summary.

catch’s picture

Status: Active » Needs review

This is not explicit test coverage, but it's the test coverage I was a trying to add when I discovered this bug. The green test run shows there's no explicit or implicit test coverage for the current behaviour at all.

andypost’s picture

catch’s picture

OpenTelemetryFrontPagePerformanceTest::testNodePageHotCache() has some kind of problem with the page cache not being set - this is what led me to here in the first place. However I can't reproduce that problem locally, only on gitlab CI, which makes it hard to track down what's going on. I've added some extra assertions to the Umami front page test instead, which should fail on a test only run.

@znerol pointed out in slack the original reason this was done: https://www.drupal.org/project/drupal/issues/2348679#comment-9517741

All event subscribers to the onKernelTerminate event will be run if we do my original approach here - and many of those aren't and shouldn't be run on page cache hits (like path alias subscriber).

However, we we take service destruction out of the onKernelTerminate event subscriber and run it in its own right, that might work. Services only get destructed if they're already initialized, so that's minimal overhead to add.

I've pushed a commit that does that.

This seems to work - but we either need to rename that class or just move the logic into DrupalKernel since it leaves it wrongly named.

catch’s picture

This requires adjusting some test assertions - possibly because the collector is running after other services in kernel destruct and hence picking up more database queries.

In general I think while the performance test use case is very specific, this change in behaviour is an improvement:

1. Destructable services should destruct as late as possible - i.e. after other things that might happen in kernel destruct like UserRequestSubscriber or automated cron.

2. If you tag a service with needs_destruction it seems reasonable to assume it'll be destructed on any request where it's iniatialized.

catch’s picture

Inlining was causing very strange errors on gitlab which I couldn't reproduce with local test runs, so backed that out of the MR. We could just rename the class/service, if this approach is otherwise OK.

smustgrave’s picture

There any concern about the increase in calls?

catch’s picture

It will only run for services that were already instantiated and are also destructable, which will usually be nothing.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

In that case let’s give it a shot

catch’s picture

Status: Reviewed & tested by the community » Needs work

We need to rename the class to something other than KernelDestructionSubscriber to avoid confusion before commit (and put it in a different namespace). Maybe something like ServiceDestructor?

catch’s picture

Status: Needs work » Needs review

I had tried that before, but completely failed - however @alexpott pointed out that this was because I hadn't removed the call to the event subscriber (now a non-existing class), because it's so low level the test failures weren't obvious and I was looking for bugs with what I'd changed, not what I hadn't changed. With that cruft removed, it's fine. This avoids the renaming issue altogether.

smustgrave’s picture

+1 for me. kernel.destructable_services seems like a good name and feedback appears to be addressed.

Going to leave in review for additional eyes.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Been a few days, going to go ahead and mark.

longwave’s picture

Status: Reviewed & tested by the community » Needs work

Added feedback to the MR, also we need to remove the event subscriber from core.services.yml:

  kernel_destruct_subscriber:
    class: Drupal\Core\EventSubscriber\KernelDestructionSubscriber
    calls:
      - [setContainer, ['@service_container']]
catch’s picture

Status: Needs work » Needs review

Should address #16, also added a CR for the deprecation.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Appears deprecation feedback has been addressed.

longwave’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs rebasing following changes to StandardPerformanceTest.

catch’s picture

Status: Needs work » Needs review

Rebased. A few assertions have to be increased now that we have exact assertions, which I'm pretty sure is because it's recording more things that were already happening - i.e. destruct is now guaranteed to run after any terminate subscribers.

catch’s picture

Issue tags: -Needs reroll
andypost’s picture

@catch maybe instead service should be deprecated? https://www.drupal.org/about/core/policies/core-change-policies/drupal-d...

catch’s picture

@andypost all event subscribers are implicitly @internal per https://www.drupal.org/about/core/policies/core-change-policies/bc-polic... The service is only to register the event subscriber so similarly we should remove rather than deprecate here - we'd have to remove the event subscriber tag anyway.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

@catch Clear, thank you!

back to RTBC

  • longwave committed e5fc18c5 on 11.x
    Issue #3412556 by catch, smustgrave, andypost, longwave: Allow...
longwave’s picture

Status: Reviewed & tested by the community » Fixed

Committed e5fc18c and pushed to 11.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

dpi’s picture