Skip to content

[Mailer] Throw TransportException when unable to read from socket #53157

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

Merged

Conversation

xdanik
Copy link
Contributor

@xdanik xdanik commented Dec 20, 2023

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues None
License MIT

We are seeing error fgets(): SSL: Connection reset by peer multiple times a day from connection to Office 365 SMTP server (smtp.office365.com:587).
It's certainly related to some kind of timeout as we are sending emails from long running queue dispatcher and error shows up only occasionally and never with the first message. We are not seeing this issue with any other SMTP server, but we have not tested much past smtp.mandrillapp.com and local MailHog.

We have tried adjusting the $pingThreshold and $restartThreshold options, but without much success (well $restartThreshold = 1 resolves the issue, but it also forces the transport to close connection after each message).

Stack trace:

#0 /var/www/vendor/symfony/mailer/Transport/Smtp/Stream/AbstractStream.php(77): fgets(Resource(stream))
#1 /var/www/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php(315): Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream->readLine()
#2 /var/www/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php(181): Symfony\Component\Mailer\Transport\Smtp\SmtpTransport->getFullResponse()
#3 /var/www/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php(140): Symfony\Component\Mailer\Transport\Smtp\SmtpTransport->executeCommand("RSET
", Array(1))
#4 /var/www/vendor/symfony/mailer/Mailer.php(45): Symfony\Component\Mailer\Transport\Smtp\SmtpTransport->send(Object(Symfony\Component\Mime\Email), Null)
#5 (our queue dispatcher): Symfony\Component\Mailer\Mailer->send(Object(Symfony\Component\Mime\Email))

App is running on PHP 8.0.28 on Debian Linux x64, Mailer v5.4.22.

I would gladly written some tests for this, but I don't know how to simulate calls to low-level stream functions like fgets.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.1 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "7.1" but it seems your PR description refers to branch "5.4".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@xdanik xdanik force-pushed the mailer-handle-fgets-connection-reset branch from b49f580 to 2e9669f Compare December 20, 2023 13:28
@xdanik xdanik changed the base branch from 7.1 to 5.4 December 20, 2023 13:28
@xdanik xdanik force-pushed the mailer-handle-fgets-connection-reset branch from 2e9669f to 83e4e06 Compare December 20, 2023 13:33
@xdanik xdanik changed the title [Mailer] Thrown TransportException when unable to read from socket [Mailer] Throw TransportException when unable to read from socket Dec 20, 2023
Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great PR. Thank you.

I think we should throw an exception here.
According to the manual for fgets:

If there is no more data to read in the file pointer, then false is returned. If an error occurs, false is returned.

We check for eof() earlier in this method. So if fgets returns false here it must be an error and we should convert it to an exception.

@Nyholm Nyholm modified the milestones: 7.1, 5.4 Dec 20, 2023
@OskarStark OskarStark changed the title [Mailer] Throw TransportException when unable to read from socket [Mailer] Throw TransportException when unable to read from socket Dec 28, 2023
@nicolas-grekas nicolas-grekas force-pushed the mailer-handle-fgets-connection-reset branch from 83e4e06 to 44d5b57 Compare January 29, 2024 07:33
@nicolas-grekas
Copy link
Member

Thank you @xdanik.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants