Skip to content

[13.x] Fix reference cycle from Mailable headers() and priority() callbacks - #61863

Merged
taylorotwell merged 2 commits into
laravel:13.xfrom
xurshudyan:mailable-callback-reference-cycle
Oct 5, 2026
Merged

taylorotwell merged 2 commits into
laravel:13.xfrom
xurshudyan:mailable-callback-reference-cycle

Conversation

@xurshudyan

Copy link
Copy Markdown
Contributor

priority() and headers() add a non-static closure to the mailable's own callbacks, so the mailable holds a reference to itself. After sending, it isn't freed until PHP's garbage collector runs, along with everything it holds, like attachments.

class InvoiceMail extends Mailable
{
    public function headers(): Headers
    {
        return new Headers(messageId: 'invoice-1@example.com');
    }
}

Sending 500 mails with a 1 MB attachment:

Mailable Before After
plain 2.0 MB 2.0 MB
with headers() 504.5 MB 2.0 MB
with priority(1) 504.4 MB 2.0 MB

Neither closure uses $this, so they can just be static. Same fix as #61802 and #61438.

@taylorotwell
taylorotwell merged commit a1c07e2 into laravel:13.x Oct 5, 2026
62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants