Skip to content
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

Infinite loop with mailer monolog service when running Symfony 5.4 #796

Closed
jorrit opened this issue Dec 31, 2023 · 7 comments
Closed

Infinite loop with mailer monolog service when running Symfony 5.4 #796

jorrit opened this issue Dec 31, 2023 · 7 comments

Comments

@jorrit
Copy link

jorrit commented Dec 31, 2023

How do you use Sentry?

Sentry SaaS (sentry.io)

SDK version

3.6.0

Steps to reproduce

  1. Require sentry/sentry-symfony in a Symfony 5.4 application.
  2. Setup monolog with a symfony_mailer handler.

Expected result

Working Symfony application

Actual result

Infinite loop:

This is the recurring part of the stack trace:

   19.9106   34245712 250. ContainerBlsxpCq\App_KernelProdContainer->ContainerBlsxpCq\{closure:...\var\cache\prod\ContainerBlsxpCq\App_KernelProdContainer.php:11067-11072}() ...\vendor\symfony\mailer\Transport.php:170
   19.9106   34245712 251. ContainerBlsxpCq\App_KernelProdContainer->getMailer_TransportFactory_NullService() ...\var\cache\prod\ContainerBlsxpCq\App_KernelProdContainer.php:11068
   19.9106   34245712 252. ContainerBlsxpCq\App_KernelProdContainer->getTraceableHttpClientService() ...\var\cache\prod\ContainerBlsxpCq\App_KernelProdContainer.php:11113
   19.9106   34245712 253. ContainerBlsxpCq\App_KernelProdContainer->getMonolog_Handler_MainService() ...\var\cache\prod\ContainerBlsxpCq\App_KernelProdContainer.php:7195
   19.9107   34246960 254. ContainerBlsxpCq\App_KernelProdContainer->getMonolog_Handler_DevelopersService() ...\var\cache\prod\ContainerBlsxpCq\App_KernelProdContainer.php:11273
   19.9107   34246960 255. ContainerBlsxpCq\App_KernelProdContainer->getMailer_MailerService() ...\var\cache\prod\ContainerBlsxpCq\App_KernelProdContainer.php:11230
   19.9107   34247888 256. Symfony\Component\Mailer\Transport->fromStrings($dsns = ['main' => 'smtp:https://localhost']) ...\var\cache\prod\ContainerBlsxpCq\App_KernelProdContainer.php:11072
   19.9107   34247888 257. Symfony\Component\Mailer\Transport->fromString($dsn = 'smtp:https://localhost') ...\vendor\symfony\mailer\Transport.php:105    
   19.9107   34247888 258. Symfony\Component\Mailer\Transport->parseDsn($dsn = 'smtp:https://localhost', $offset = ???) ...\vendor\symfony\mailer\Transport.php:113
   19.9107   34248304 259. Symfony\Component\Mailer\Transport->fromDsnObject($dsn = class Symfony\Component\Mailer\Transport\Dsn { private $scheme = 'smtp'; private $host = 'localhost'; private $user = NULL; private $password = NULL; private $port = NULL; private $options = [] }) ...\vendor\symfony\mailer\Transport.php:161
   19.9107   34249360 260. ContainerBlsxpCq\App_KernelProdContainer->ContainerBlsxpCq\{closure:...\var\cache\prod\ContainerBlsxpCq\App_KernelProdContainer.php:11067-11072}() ...\vendor\symfony\mailer\Transport.php:170

Basically, the Monolog initializes the Monolog handlers, which get a tracable HTTP service injected, which requires the Monolog service again.

I guess this is something specific in my installation (I recently updated from Symfony 4.4), otherwise others would have reported this already.

@jorrit
Copy link
Author

jorrit commented Dec 31, 2023

I disabled the http_client integration for now to work around this problem.

@ste93cry
Copy link
Collaborator

Could you please share the Monolog configuration you're using to reproduce the issue?

@jorrit
Copy link
Author

jorrit commented Jan 1, 2024

packages/monolog.yaml:

monolog:
    channels:
        - app
        - import
        - translation

    handlers:
        backoffice:
            type: symfony_mailer
            from_email: '[email protected]'
            to_email:
                - '[email protected]'
            subject: '[XXXXX] %%message%%'
            level: warning
            formatter: monolog.formatter.html
            content_type: text/html
            channels:
                - ratedownload

        developers:
            type: symfony_mailer
            from_email: '[email protected]'
            to_email:
                - '[email protected]'
            subject: '[XXXXXX] %%message%%'
            level: warning
            formatter: monolog.formatter.html
            content_type: text/html
            channels:
                - app

monolog/prod/monolog.yaml:

monolog:
    handlers:
        main:
            type: fingers_crossed
            action_level: error
            handler: main_split
            excluded_http_codes: [ 404, 405 ]
            buffer_size: 50 # How many messages should be saved? Prevent memory leaks
            channels: [ "!ratedownload" ]
        main_split:
            type: group
            members: [ main_file, developers ]
        main_file:
            type: rotating_file
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: info
            max_files: 10
        console:
            type: console
            process_psr_3_messages: false
            channels: [ "!event", "!doctrine", "!ratedownload" ]
            console_formatter_options:
                multiline: true
        #        deprecation:
        #            type: rotating_file
        #            path: "%kernel.logs_dir%/%kernel.environment%.deprecations.log"
        #            max_files: 10
        #        deprecation_filter:
        #            type: filter
        #            handler: deprecation
        #            max_level: info
        #            channels: ["php"]
        import:
            type: rotating_file
            path: "%kernel.logs_dir%/import.log"
            level: info
            channels: [ "import" ]
            max_files: 10
        translation:
            type: rotating_file
            path: "%kernel.logs_dir%/translation.log"
            level: info
            channels: [ "translation" ]
            max_files: 10
        sentry:
            type: sentry
            level: !php/const Monolog\Logger::ERROR
            hub_id: Sentry\State\HubInterface
            bubble: true
            fill_extra_context: true

@ste93cry
Copy link
Collaborator

ste93cry commented Jan 3, 2024

I tried using your configuration and I cannot reproduce the issue. I didn't set the MAILER_DSN environment variable, so it's using the NullTransport of the Mailer. Likely I'm missing something, but I don't know what. Could you prepare a little repo reproducing the problem?

@jorrit
Copy link
Author

jorrit commented Jan 5, 2024

I tried to reproduce it in an empty project. In this project, the getMailer_TransportFactory_NullService method of the container looks like this:

    protected function getMailer_TransportFactory_NullService()
    {
        $a = ($this->privates['monolog.logger.mailer'] ?? $this->getMonolog_Logger_MailerService());

        if (isset($this->privates['mailer.transport_factory.null'])) {
            return $this->privates['mailer.transport_factory.null'];
        }
        $b = ($this->services['event_dispatcher'] ?? $this->getEventDispatcherService());

        if (isset($this->privates['mailer.transport_factory.null'])) {
            return $this->privates['mailer.transport_factory.null'];
        }
        $c = ($this->privates['.debug.http_client'] ?? $this->get_Debug_HttpClientService());

        if (isset($this->privates['mailer.transport_factory.null'])) {
            return $this->privates['mailer.transport_factory.null'];
        }

        return $this->privates['mailer.transport_factory.null'] = new \Symfony\Component\Mailer\Transport\NullTransportFactory($b, $c, $a);
    }

In the 'broken' project, it looks like this:

    /**
     * Gets the private 'mailer.transport_factory.null' shared service.
     *
     * @return \Symfony\Component\Mailer\Transport\NullTransportFactory
     */
    protected function getMailer_TransportFactory_NullService()
    {
        include_once \dirname(__DIR__, 4).''.\DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR.'symfony'.\DIRECTORY_SEPARATOR.'mailer'.\DIRECTORY_SEPARATOR.'Transport'.\DIRECTORY_SEPARATOR.'TransportFactoryInterface.php';
        include_once \dirname(__DIR__, 4).''.\DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR.'symfony'.\DIRECTORY_SEPARATOR.'mailer'.\DIRECTORY_SEPARATOR.'Transport'.\DIRECTORY_SEPARATOR.'AbstractTransportFactory.php';
        include_once \dirname(__DIR__, 4).''.\DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR.'symfony'.\DIRECTORY_SEPARATOR.'mailer'.\DIRECTORY_SEPARATOR.'Transport'.\DIRECTORY_SEPARATOR.'NullTransportFactory.php';

        $a = ($this->services['event_dispatcher'] ?? $this->getEventDispatcherService());

        if (isset($this->privates['mailer.transport_factory.null'])) {
            return $this->privates['mailer.transport_factory.null'];
        }

        return $this->privates['mailer.transport_factory.null'] = new \Symfony\Component\Mailer\Transport\NullTransportFactory($a, ($this->privates['.debug.http_client'] ?? $this->get_Debug_HttpClientService()), ($this->privates['monolog.logger.mailer'] ?? $this->getMonolog_Logger_MailerService()));
    }

The isset calls break the loop. I have no idea why my project doesn't have them.

The repository with the example that works is at https://github.com/jorrit/sentry-symfony-bug-demo.

@ste93cry
Copy link
Collaborator

ste93cry commented Jan 6, 2024

I tried cloning your repository and running the bin/console script and it works fine, no issues during the compilation of the container. I'm sorry, I don't know how to help without a project that clearly shows the error.

@jorrit
Copy link
Author

jorrit commented Jan 7, 2024

I know, the repository works fine. I have yet to figure out why that repository works and my private project doesn't. It has something to do with the difference in how the DI container is generated. I'll close this issue because at this moment it doesn't seem to involve sentry directly. I will reopen when there might be something that can be improved here.

Thank you for your time.

@jorrit jorrit closed this as completed Jan 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants