Skip to content

Commit 630536d

Browse files
committed
Fix crash when http2 client connects concurrently
1 parent 3e7770f commit 630536d

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

ext-src/swoole_http2_client_coro.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ class Client {
119119
}
120120

121121
inline bool is_available() {
122-
if (sw_unlikely(!client)) {
122+
if (sw_unlikely(!client || !client->is_connect())) {
123123
swoole_set_last_error(SW_ERROR_CLIENT_NO_CONNECTION);
124124
zend_update_property_long(
125-
swoole_http2_client_coro_ce, SW_Z8_OBJ_P(zobject), ZEND_STRL("errCode"), ECONNRESET);
125+
swoole_http2_client_coro_ce, SW_Z8_OBJ_P(zobject), ZEND_STRL("errCode"), SW_ERROR_CLIENT_NO_CONNECTION);
126126
zend_update_property_string(swoole_http2_client_coro_ce,
127127
SW_Z8_OBJ_P(zobject),
128128
ZEND_STRL("errMsg"),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
swoole_http2_client_coro: connect twice
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../include/bootstrap.php';
8+
9+
use function Swoole\Coroutine\run;
10+
use function Swoole\Coroutine\go;
11+
12+
run(function () {
13+
$client = new \Swoole\Coroutine\Http2\Client('www.zhihu.com', 443, true);
14+
$chan = new \Swoole\Coroutine\Channel(1);
15+
go(function () use ($client, $chan) {
16+
$client->connect();
17+
$req = new \Swoole\Http2\Request();
18+
$req->method = 'GET';
19+
$req->path = '/io?io=' . str_repeat('xxx', 1000);
20+
$client->send($req);
21+
$chan->push(true);
22+
$resp = $client->recv();
23+
Assert::eq($resp->statusCode, 200);
24+
Assert::contains($resp->data, '知乎');
25+
$chan->pop();
26+
});
27+
go(function () use ($client, $chan) {
28+
Assert::eq($client->connect(), false);
29+
$req = new \Swoole\Http2\Request();
30+
$req->method = 'GET';
31+
$req->path = '/io?io=xxx';
32+
$client->send($req);
33+
$chan->push(true);
34+
Assert::eq($client->recv(), false);
35+
$chan->pop();
36+
});
37+
});
38+
39+
?>
40+
--EXPECT--

tests/swoole_http2_client_coro/error.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ go(function () {
1111
Assert::false($cli->connect());
1212
Assert::same($cli->errCode, SOCKET_ETIMEDOUT);
1313
Assert::false($cli->send(new Swoole\Http2\Request));
14-
Assert::same($cli->errCode, SOCKET_ECONNRESET);
14+
Assert::same($cli->errCode, SWOOLE_ERROR_CLIENT_NO_CONNECTION);
1515
Assert::false($cli->recv(1));
16-
Assert::same($cli->errCode, SOCKET_ECONNRESET);
16+
Assert::same($cli->errCode, SWOOLE_ERROR_CLIENT_NO_CONNECTION);
1717
});
1818
Swoole\Event::wait();
1919
?>

0 commit comments

Comments
 (0)