Skip to content

Commit 6e610d5

Browse files
committed
Consistently use ECONNABORTED for cancelled connection attempts
1 parent 4aa5267 commit 6e610d5

9 files changed

+54
-10
lines changed

src/DnsConnector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ function ($_, $reject) use (&$promise, &$resolved, $uri) {
9191
// cancellation should reject connection attempt
9292
// reject DNS resolution with custom reason, otherwise rely on connection cancellation below
9393
if ($resolved === null) {
94-
$reject(new \RuntimeException('Connection to ' . $uri . ' cancelled during DNS lookup'));
94+
$reject(new \RuntimeException(
95+
'Connection to ' . $uri . ' cancelled during DNS lookup',
96+
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
97+
));
9598
}
9699

97100
// (try to) cancel pending DNS lookup / connection attempt

src/HappyEyeBallsConnectionBuilder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ public function connect()
103103
return $deferred->promise();
104104
})->then($lookupResolve(Message::TYPE_A));
105105
}, function ($_, $reject) use ($that, &$timer) {
106-
$reject(new \RuntimeException('Connection to ' . $that->uri . ' cancelled' . (!$that->connectionPromises ? ' during DNS lookup' : '')));
106+
$reject(new \RuntimeException(
107+
'Connection to ' . $that->uri . ' cancelled' . (!$that->connectionPromises ? ' during DNS lookup' : ''),
108+
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
109+
));
107110
$_ = $reject = null;
108111

109112
$that->cleanUp();

src/SecureConnector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ function ($resolve, $reject) use ($promise) {
105105
},
106106
function ($_, $reject) use (&$promise, $uri, &$connected) {
107107
if ($connected) {
108-
$reject(new \RuntimeException('Connection to ' . $uri . ' cancelled during TLS handshake'));
108+
$reject(new \RuntimeException(
109+
'Connection to ' . $uri . ' cancelled during TLS handshake',
110+
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
111+
));
109112
}
110113

111114
$promise->cancel();

src/TcpConnector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ public function connect($uri)
141141
}
142142
// @codeCoverageIgnoreEnd
143143

144-
throw new \RuntimeException('Connection to ' . $uri . ' cancelled during TCP/IP handshake');
144+
throw new \RuntimeException(
145+
'Connection to ' . $uri . ' cancelled during TCP/IP handshake',
146+
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
147+
);
145148
});
146149
}
147150
}

tests/DnsConnectorTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
167167
$promise = $this->connector->connect('example.com:80');
168168
$promise->cancel();
169169

170-
$this->setExpectedException('RuntimeException', 'Connection to tcp://example.com:80 cancelled during DNS lookup');
170+
$this->setExpectedException(
171+
'RuntimeException',
172+
'Connection to tcp://example.com:80 cancelled during DNS lookup',
173+
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
174+
);
171175
$this->throwRejection($promise);
172176
}
173177

@@ -196,7 +200,10 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionWithTcpRejectio
196200
$first = new Deferred();
197201
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn($first->promise());
198202
$pending = new Promise\Promise(function () { }, function () {
199-
throw new \RuntimeException('Connection cancelled');
203+
throw new \RuntimeException(
204+
'Connection cancelled',
205+
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
206+
);
200207
});
201208
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($pending);
202209

@@ -205,7 +212,11 @@ public function testCancelDuringTcpConnectionCancelsTcpConnectionWithTcpRejectio
205212

206213
$promise->cancel();
207214

208-
$this->setExpectedException('RuntimeException', 'Connection cancelled');
215+
$this->setExpectedException(
216+
'RuntimeException',
217+
'Connection cancelled',
218+
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
219+
);
209220
$this->throwRejection($promise);
210221
}
211222

tests/HappyEyeBallsConnectionBuilderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,10 @@ public function testCancelConnectWillRejectPromiseAndCancelBothDnsLookups()
664664
});
665665

666666
$this->assertInstanceOf('RuntimeException', $exception);
667+
assert($exception instanceof \RuntimeException);
668+
667669
$this->assertEquals('Connection to tcp://reactphp.org:80 cancelled during DNS lookup', $exception->getMessage());
670+
$this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode());
668671
}
669672

670673
public function testCancelConnectWillRejectPromiseAndCancelPendingIpv6LookupAndCancelDelayTimer()
@@ -701,7 +704,10 @@ public function testCancelConnectWillRejectPromiseAndCancelPendingIpv6LookupAndC
701704
});
702705

703706
$this->assertInstanceOf('RuntimeException', $exception);
707+
assert($exception instanceof \RuntimeException);
708+
704709
$this->assertEquals('Connection to tcp://reactphp.org:80 cancelled during DNS lookup', $exception->getMessage());
710+
$this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode());
705711
}
706712

707713
public function testCancelConnectWillRejectPromiseAndCancelPendingIpv6ConnectionAttemptAndPendingIpv4LookupAndCancelAttemptTimer()
@@ -744,7 +750,10 @@ public function testCancelConnectWillRejectPromiseAndCancelPendingIpv6Connection
744750
});
745751

746752
$this->assertInstanceOf('RuntimeException', $exception);
753+
assert($exception instanceof \RuntimeException);
754+
747755
$this->assertEquals('Connection to tcp://reactphp.org:80 cancelled', $exception->getMessage());
756+
$this->assertEquals(defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103, $exception->getCode());
748757
}
749758

750759
public function testResolveWillReturnResolvedPromiseWithEmptyListWhenDnsResolverFails()

tests/HappyEyeBallsConnectorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,11 @@ public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
275275
$that->throwRejection($promise);
276276
});
277277

278-
$this->setExpectedException('RuntimeException', 'Connection to tcp://example.com:80 cancelled during DNS lookup');
278+
$this->setExpectedException(
279+
'RuntimeException',
280+
'Connection to tcp://example.com:80 cancelled during DNS lookup',
281+
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
282+
);
279283
$this->loop->run();
280284
}
281285

tests/SecureConnectorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnec
187187
$promise = $this->connector->connect('example.com:80');
188188
$promise->cancel();
189189

190-
$this->setExpectedException('RuntimeException', 'Connection to tls://example.com:80 cancelled during TLS handshake');
190+
$this->setExpectedException(
191+
'RuntimeException',
192+
'Connection to tls://example.com:80 cancelled during TLS handshake',
193+
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
194+
);
191195
$this->throwRejection($promise);
192196
}
193197

tests/TcpConnectorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,11 @@ public function cancellingConnectionShouldRejectPromise()
324324
$promise = $connector->connect($server->getAddress());
325325
$promise->cancel();
326326

327-
$this->setExpectedException('RuntimeException', 'Connection to ' . $server->getAddress() . ' cancelled during TCP/IP handshake');
327+
$this->setExpectedException(
328+
'RuntimeException',
329+
'Connection to ' . $server->getAddress() . ' cancelled during TCP/IP handshake',
330+
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
331+
);
328332
Block\await($promise, $loop);
329333
}
330334

0 commit comments

Comments
 (0)