Skip to content

Commit

Permalink
bug #36417 Force ping after transport exception (oesteve)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

Force ping after transport exception

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36301
| License       | MIT
| Doc PR        |

SMTP transport fails for long running processes after tranport exception, if stream is closed all messages will throw a transport exception until $lastMessageTime exceds $pingThreshold.

With this PR, after transport expception the transport will ping the server to check if the connection is still alive.

Commits
-------

7ccbef6 Force ping after transport Exception
  • Loading branch information
fabpot committed Apr 12, 2020
2 parents 0745ea4 + 7ccbef6 commit 280674f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
Expand Down Expand Up @@ -43,6 +44,29 @@ public function testSendDoesNotPingBelowThreshold(): void
$this->assertNotContains("NOOP\r\n", $stream->getCommands());
}

public function testSendPingAfterTransportException(): void
{
$stream = new DummyStream();
$envelope = new Envelope(new Address('sender@example.org'), [new Address('recipient@example.org')]);

$transport = new SmtpTransport($stream);
$transport->send(new RawMessage('Message 1'), $envelope);
$stream->close();
$catch = false;

try {
$transport->send(new RawMessage('Message 2'), $envelope);
} catch (TransportException $exception) {
$catch = true;
}
$this->assertTrue($catch);
$this->assertTrue($stream->isClosed());

$transport->send(new RawMessage('Message 3'), $envelope);

$this->assertFalse($stream->isClosed());
}

public function testSendDoesPingAboveThreshold(): void
{
$stream = new DummyStream();
Expand Down Expand Up @@ -76,13 +100,23 @@ class DummyStream extends AbstractStream
*/
private $commands;

/**
* @var bool
*/
private $closed = true;

public function initialize(): void
{
$this->closed = false;
$this->nextResponse = '220 localhost';
}

public function write(string $bytes, $debug = true): void
{
if ($this->closed) {
throw new TransportException('Unable to write bytes on the wire.');
}

$this->commands[] = $bytes;

if (0 === strpos($bytes, 'DATA')) {
Expand Down Expand Up @@ -120,4 +154,14 @@ protected function getReadConnectionDescription(): string
{
return 'null';
}

public function close(): void
{
$this->closed = true;
}

public function isClosed(): bool
{
return $this->closed;
}
}
5 changes: 2 additions & 3 deletions src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php
Expand Up @@ -206,12 +206,11 @@ protected function doSend(SentMessage $message): void
$this->stream->flush();
$this->executeCommand("\r\n.\r\n", [250]);
$message->appendDebug($this->stream->getDebug());
$this->lastMessageTime = microtime(true);
} catch (TransportExceptionInterface $e) {
$e->appendDebug($this->stream->getDebug());

$this->lastMessageTime = 0;
throw $e;
} finally {
$this->lastMessageTime = microtime(true);
}
}

Expand Down

0 comments on commit 280674f

Please sign in to comment.