Skip to content

Database queries in kernel.terminate are not logged to profiler (only to file) #35382

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

Closed
mhujer opened this issue Jan 18, 2020 · 19 comments
Closed

Comments

@mhujer
Copy link
Contributor

mhujer commented Jan 18, 2020

Symfony version(s) affected: 5.0.2

Description
Database queries in listeners to kernel.terminate event are not logged to profiler, but they are still logged to var/log/dev.log. I would expect them to be either in both places or none.

I discovered this after I added a listener that saves timestamp of last user activity to database (and I thought it may be a good idea to do it in kernel.terminate not to block the response).

How to reproduce

  1. create a Symfony demo application
  2. create a listener in src/DemoListener.php:
<?php declare(strict_types = 1);

namespace App;

use App\Entity\Post;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;

final class DemoListener
{
    /** @var \Doctrine\ORM\EntityManagerInterface */
    private $entityManager;

    public function __construct(
        EntityManagerInterface $entityManager
    )
    {
        $this->entityManager = $entityManager;
    }

    public function __invoke(TerminateEvent $event): void
    {
        $posts = $this->entityManager->createQueryBuilder()
            ->select('post')
            ->from(Post::class, 'post')
            ->getQuery()->getResult();
    }
}
  1. and register in in services.yaml:
    App\DemoListener:
        tags:
            - { name: 'kernel.event_listener' }

(You can use the patch listener.patch.txt )

  1. open any page in the demo application
  2. the SELECT from the listener is not visible in profiler
  3. the SELECT from the listener is visible in var/log/dev.log

Possible Solution
Maybe collect the profiler data in kernel.terminate? But I guess that would have other consequences.

Additional context
I found an opinion that use of kernel.terminate is dangerous and should be avoided #27544 (comment) but it is not mentioned in docs.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@mhujer
Copy link
Contributor Author

mhujer commented Jan 22, 2021

@carsonbot it is still relevant.

@carsonbot carsonbot removed the Stalled label Jan 22, 2021
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@mhujer
Copy link
Contributor Author

mhujer commented Jul 23, 2021

@carsonbot it is still not resolved

@carsonbot carsonbot removed the Stalled label Jul 23, 2021
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@mhujer
Copy link
Contributor Author

mhujer commented Jan 24, 2022

@carsonbot It probably wasn't fixed, but is not affecting my application anymore.

@carsonbot carsonbot removed the Stalled label Jan 24, 2022
@yellow1912
Copy link

I run into the same issue. My theory is that the entity manager is closed before this terminate event? I get this error alot when running command and listen to the console.terminate event.

@PabloKowalczyk
Copy link
Contributor

PabloKowalczyk commented Mar 24, 2022

On Symfony 6.0 issue still exists, IMO profiler should show queries from terminate, like it show e-mails.

My theory is that the entity manager is closed before this terminate event?

Doctrine will throw exception in that case.

Edit:
"Performance" panel in profiler also shows queries done on kernel.terminate.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@PabloKowalczyk
Copy link
Contributor

Is this bug still relevant?

Yep, nothing changed.

@carsonbot carsonbot removed the Stalled label Oct 29, 2022
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@PabloKowalczyk
Copy link
Contributor

Still not fixed.

@carsonbot carsonbot removed the Stalled label May 4, 2023
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@PabloKowalczyk
Copy link
Contributor

Still not fixed.

@carsonbot carsonbot removed the Stalled label Nov 5, 2023
@nicolas-grekas
Copy link
Member

@PabloKowalczyk would you like to give this a try? Keeping the issue open won't make it solve by itself, someone needs to commit to figuring out a solution.

@PabloKowalczyk
Copy link
Contributor

I have done some investigation and turns out that profiles are collected on kernel.response event (\Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse), while queries on kernel.terminate are correctly added to \Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder, they can't be collected (by Profiler) because it is too late.

@nicolas-grekas Any ideas how it can be fixed?

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Friendly ping? Should this still be open? I will close if I don't hear anything.

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants