Skip to content

Commit 960431d

Browse files
committed
Fix the bug of the channel, the data that has enqueue cannot be popped after the close
1 parent dfef9a4 commit 960431d

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

ext-src/swoole_channel_coro.cc

+2-16
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,6 @@ static PHP_METHOD(swoole_channel_coro, __construct) {
167167

168168
static PHP_METHOD(swoole_channel_coro, push) {
169169
Channel *chan = php_swoole_get_channel(ZEND_THIS);
170-
if (chan->is_closed()) {
171-
zend_update_property_long(
172-
swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), SW_CHANNEL_CLOSED);
173-
RETURN_FALSE;
174-
} else {
175-
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), SW_CHANNEL_OK);
176-
}
177-
178170
zval *zdata;
179171
double timeout = -1;
180172

@@ -187,6 +179,7 @@ static PHP_METHOD(swoole_channel_coro, push) {
187179
Z_TRY_ADDREF_P(zdata);
188180
zdata = sw_zval_dup(zdata);
189181
if (chan->push(zdata, timeout)) {
182+
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), SW_CHANNEL_OK);
190183
RETURN_TRUE;
191184
} else {
192185
zend_update_property_long(swoole_channel_coro_ce,
@@ -201,14 +194,6 @@ static PHP_METHOD(swoole_channel_coro, push) {
201194

202195
static PHP_METHOD(swoole_channel_coro, pop) {
203196
Channel *chan = php_swoole_get_channel(ZEND_THIS);
204-
if (chan->is_closed()) {
205-
zend_update_property_long(
206-
swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), SW_CHANNEL_CLOSED);
207-
RETURN_FALSE;
208-
} else {
209-
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), SW_CHANNEL_OK);
210-
}
211-
212197
double timeout = -1;
213198

214199
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 1)
@@ -220,6 +205,7 @@ static PHP_METHOD(swoole_channel_coro, pop) {
220205
if (zdata) {
221206
RETVAL_ZVAL(zdata, 0, 0);
222207
efree(zdata);
208+
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), SW_CHANNEL_OK);
223209
} else {
224210
zend_update_property_long(swoole_channel_coro_ce,
225211
SW_Z8_OBJ_P(ZEND_THIS),

src/coroutine/channel.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void Channel::yield(enum opcode type) {
4747

4848
void *Channel::pop(double timeout) {
4949
Coroutine *current_co = Coroutine::get_current_safe();
50-
if (closed) {
50+
if (closed && is_empty()) {
5151
return nullptr;
5252
}
5353
if (is_empty() || !consumer_queue.empty()) {
@@ -67,7 +67,7 @@ void *Channel::pop(double timeout) {
6767
if (msg.timer) {
6868
swoole_timer_del(msg.timer);
6969
}
70-
if (msg.error || closed) {
70+
if (msg.error || (closed && is_empty())) {
7171
return nullptr;
7272
}
7373
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
swoole_channel_coro: pop after close
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../include/bootstrap.php';
8+
9+
use Swoole\Coroutine\Channel;
10+
11+
use function Swoole\Coroutine\run;
12+
13+
run(function () {
14+
$chan = new Channel();
15+
16+
go(function () use ($chan) {
17+
for ($i = 1; $i <= 3; $i++) {
18+
if ($chan->push($i)) {
19+
echo "push ok\n";
20+
}
21+
}
22+
$chan->close();
23+
});
24+
25+
go(function () use ($chan) {
26+
while (true) {
27+
$data = $chan->pop();
28+
var_dump($data);
29+
if (!$data) {
30+
break;
31+
}
32+
}
33+
});
34+
});
35+
?>
36+
--EXPECT--
37+
push ok
38+
push ok
39+
int(1)
40+
push ok
41+
int(2)
42+
int(3)
43+
bool(false)

0 commit comments

Comments
 (0)