8000 [Mailer] Memory leaking when sending many emails · Issue #50580 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mailer] Memory leaking when sending many emails #50580

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

Open
cmodijk opened this issue Jun 6, 2023 · 12 comments · May be fixed by #60515
Open

[Mailer] Memory leaking when sending many emails #50580

cmodijk opened this issue Jun 6, 2023 · 12 comments · May be fixed by #60515

Comments

@cmodijk
Copy link
cmodijk commented Jun 6, 2023

Symfony version(s) affected

5.4.24

Description

In #37712 the the MessageLoggerListener was moved to only be in the mailer_debug.php file which fixes memory leaking issues. But in 6214f3a this change sems to be reverted and i'm not sure why.

Other reference #45211

How to reproduce

symfony new memory-leak
cd memory-leak
composer require mailer messenger doctrine symfony/doctrine-messenger

config/packages/messenger.yaml

framework:
    messenger:
        reset_on_message: true
        transports:
            async: '%env(MESSENGER_TRANSPORT_DSN)%'
        routing:
            Symfony\Component\Mailer\Messenger\SendEmailMessage: 'async'

.env

APP_ENV=prod
APP_DEBUG=0
MESSENGER_TRANSPORT_DSN=doctrine://default
MAILER_DSN=null://null
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"

src/Command/SendBatchEmailCommand.php

<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;

final class SendBatchEmailCommand extends Command
{
    private MailerInterface $mailer;

    public function __construct(MailerInterface $mailer)
    {
        parent::__construct();

        $this->mailer = $mailer;
    }

    protected function configure(): void
    {
        $this->setName('send:batch:email');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        for ($x = 0; $x < 100; $x++) {
            $email = (new Email())
                ->to('my-test@test.com');

            $this->mailer->send($email);
        }

        return self::SUCCESS;
    }
}

If you run these steps and then debug MessageLoggerListener you can see thet it stores all the e-mail items in a array leaking them in memory. If you have attachment items then it leaks super fast.

I have pushed my example to a repo here https://github.com/cmodijk/MemoryLeak

Possible Solution

I think the linked PR already fixed the issue before and it should be moved again.

Additional Context

No response

@stof
Copy link
Member
stof commented Jun 6, 2023

It has been moved back in #42771, saying it was done to match the 5.1 and 5.3 configuration. This ignored the fact that the change was done intentionally in 5.2 and not by mistake.
Apparently, it was done to solve an issue. But this might indicate that it was not the right way to fix the issue.

@cmodijk
Copy link
Author
cmodijk commented Jun 6, 2023

@stof Tnx for referencing the PR that moved it back. I'm wondering what the correct solution for it should be in the end.

@cmodijk cmodijk closed this as completed Jun 6, 2023
@cmodijk cmodijk reopened this Jun 6, 2023
@cmodijk
Copy link
Author
cmodijk commented Dec 4, 2023

Does anybody know what the correct sollution would be here. I can supply a PR if needed but i'm not sure if moving it back to mailer_debug.php is what the correct solution is here.

@mxr576
Copy link
mxr576 commented Feb 28, 2024

This ignored the fact that the change was done intentionally in 5.2 and not by mistake.

So what should be the next step? I am also curious.

@cafferata
Copy link
Contributor

Friendly ping to @nicolas-grekas, @derrabus, @lyrixx (because you all approved GitHub pull request #42771). How can we solve this issue? We would like to receive input so that we can work on this. 👍

@xabbuh
Copy link
Member
xabbuh commented Jul 19, 2024

It was probably not a good idea to to not disable the MessageLoggerListener when the profiler is not enabled. Looking at the source code the Notifier integration in NotificationAssertionsTrait probably suffers the same issue.

For the HttpClientAssertionsTrait one actually has to enable the profiler to be able to use it.

@cmodijk
Copy link
Author
cmodijk commented Jul 23, 2024

@xabbuh So you would propose a solution where we would only enable the MessageLoggerListener when the profiler is enabled? Or should it it just be moved to the mailer_debug.php?

@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?

@cafferata
Copy link
Contributor

@carsonbot this GitHub issue is still relevant and has not yet been resolved.

@peterpeppered
Copy link
peterpeppered commented May 22, 2025

FYI : I just got hit by this bug. in my case it was leaking ~100KB per mail which was... not good. I worked around it by instantiating the ESMTPTransport myself, feeding it the needed connection details, an using its send menthod instead of the mailer's (do a drop-in replacement). zero leakage. using symfony 7.3 btw

@stof
Copy link
Member
stof commented May 22, 2025

The idea in #50580 (comment) (registering the listener only when the collector services are registered) looks like a good idea. Anyone willing to implement it ?

@stof
Copy link
Member
stof commented May 22, 2025

Note that for the dev environment, something like #60243 should be implemented as well.

andrew-demb added a commit to andrew-demb/symfony that referenced this issue May 22, 2025
andrew-demb added a commit to andrew-demb/symfony that referenced this issue May 22, 2025
andrew-demb added a commit to andrew-demb/symfony that referenced this issue May 22, 2025
…hen profiler enabled to prevent memory leaks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants
0