Skip to content

[Mailer] Memory leaking when sending many emails #50580

Open
@cmodijk

Description

@cmodijk

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions