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

[Mailer] Memory leaking when sending many emails #50580

Open
cmodijk opened this issue Jun 6, 2023 · 4 comments
Open

[Mailer] Memory leaking when sending many emails #50580

cmodijk opened this issue Jun 6, 2023 · 4 comments

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.

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

4 participants