8000 bug #36417 Force ping after transport exception (oesteve) · symfony/symfony@280674f · GitHub
[go: up one dir, main page]

Skip to content

Commit 280674f

Browse files
committed
bug #36417 Force ping after transport exception (oesteve)
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
2 parents 0745ea4 + 7ccbef6 commit 280674f

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Mailer\Envelope;
16+
use Symfony\Component\Mailer\Exception\TransportException;
1617
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
1718
use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
1819
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
@@ -43,6 +44,29 @@ public function testSendDoesNotPingBelowThreshold(): void
4344
$this->assertNotContains("NOOP\r\n", $stream->getCommands());
4445
}
4546

47+
public function testSendPingAfterTransportException(): void
48+
{
49+
$stream = new DummyStream();
50+
$envelope = new Envelope(new Address('sender@example.org'), [new Address('recipient@example.org')]);
51+
52+
$transport = new SmtpTransport($stream);
53+
$transport->send(new RawMessage('Message 1'), $envelope);
54+
$stream->close();
55+
$catch = false;
56+
57+
try {
58+
$transport->send(new RawMessage('Message 2'), $envelope);
59+
} catch (TransportException $exception) {
60+
$catch = true;
61+
}
62+
$this->assertTrue($catch);
63+
$this->assertTrue($stream->isClosed());
64+
65+
$transport->send(new RawMessage('Message 3'), $envelope);
66+
67+
$this->assertFalse($stream->isClosed());
68+
}
69+
4670
public function testSendDoesPingAboveThreshold(): void
4771
{
4872
$stream = new DummyStream();
@@ -76,13 +100,23 @@ class DummyStream extends AbstractStream
76100
*/
77101
private $commands;
78102

103+
/**
104+
* @var bool
105+
*/
106+
private $closed = true;
107+
79108
public function initialize(): void
80109
{
110+
$this->closed = false;
81111
$this->nextResponse = '220 localhost';
82112
}
83113

84114
public function write(string $bytes, $debug = true): void
85115
{
116+
if ($this->closed) {
117+
throw new TransportException('Unable to write bytes on the wire.');
118+
}
119+
86120
$this->commands[] = $bytes;
87121

88122
if (0 === strpos($bytes, 'DATA')) {
@@ -120,4 +154,14 @@ protected function getReadConnectionDescription(): string
120154
{
121155
return 'null';
122156
}
157+
158+
public function close(): void
159+
{
160+
$this->closed = true;
161+
}
162+
163+
public function isClosed(): bool
164+
{
165+
return $this->closed;
166+
}
123167
}

src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,11 @@ protected function doSend(SentMessage $message): void
206206
$this->stream->flush();
207207
$this->executeCommand("\r\n.\r\n", [250]);
208208
$message->appendDebug($this->stream->getDebug());
209+
$this->lastMessageTime = microtime(true);
209210
} catch (TransportExceptionInterface $e) {
210211
$e->appendDebug($this->stream->getDebug());
211-
212< 6244 span class="diff-text-marker">+
$this->lastMessageTime = 0;
212213
throw $e;
213-
} finally {
214-
$this->lastMessageTime = microtime(true);
215214
}
216215
}
217216

0 commit comments

Comments
 (0)
0