Repository navigation
Description
Summary
new Async\Channel(0) is documented as an unbuffered rendezvous channel, but the runtime stores one value in an internal rendezvous_value slot and lets send() return immediately when no receiver is waiting. The first send() never blocks; only the second one does.
Expected behaviour (per docs)
A channel with buffer size
0works in rendezvous mode:send()blocks until another coroutine callsrecv(), and vice versa. This ensures strict synchronization.
So on a Channel(0) with no waiting receiver, send() must block until a recv() shows up.
Actual behaviour
<?php
use Async\Channel;
use function Async\spawn;
use function Async\await_all;
$ch = new Channel(0);
$s = spawn(function () use ($ch) {
echo "send-start\n";
$ch->send(42); // expected: blocks (no receiver) — actual: returns
echo "send-ok\n";
});
await_all([$s]);
echo "count=" . $ch->count() . "\n";Output:
send-start
send-ok
count=1
send() returned without a receiver, and the channel now reports count() == 1 — i.e. the value sits in an internal slot.
Root cause
ext/async/channel.c keeps a 1-element rendezvous slot:
- struct fields
rendezvous_value(zval) andrendezvous_has_value(bool) channel_is_full/channel_countfor capacity=0 are computed fromrendezvous_has_value- the send path deposits into the slot when it is empty and returns without blocking
channel_is_buffered()returnsfalsefor capacity=0 only for the buffer accessor — it does not gate the deposit-and-return behaviour
So Channel(0) is effectively a 1-slot buffer, not a Go-style rendezvous.
Impact
-
The documented happens-before / strict synchronization guarantee between sender and receiver does not hold for the first send. User code that relies on rendezvous semantics (typical pattern: "the value is observed by the receiver before the sender progresses") gets weaker ordering than promised.
-
Per-channel deadlock detection (
noConsumerTimeout) does not fire on a single stucksend(), because the send is not stuck — it deposits and returns. The timer only fires from the second send onward.
Existing tests encode the buggy behaviour
Two existing phpt tests will need updating once the runtime is fixed:
ext/async/tests/channel/003-channel_unbuffered_rendezvous.phpt
Despite the file name, the EXPECT block locks in the non-rendezvous behaviour:
Main: done
Sender: about to send
Sender: send completed ← sender finishes BEFORE receiver
Receiver: about to recv
Receiver: got hello
Under correct rendezvous semantics, send completed must print after got hello (or the two are interleaved by the scheduler — but send completed cannot precede about to recv).
ext/async/tests/channel/026-channel_close_wakes_sender.phpt
Setup:
$ch = new Channel(0);
$ch->sendAsync(\"block\"); // comment in test: \"Fill the channel first\"
spawn(function () use ($ch) {
$ch->send(\"waiting\"); // comment: \"Sender waiting for space in full channel\"
});The test "fills" the channel with one value via sendAsync and expects the next send() to block. Under correct rendezvous semantics there is no slot to fill — sendAsync on an empty Channel(0) with no waiting receiver should return false (channel is full from a sender's point of view), and a different setup is needed to test close-wakes-sender.
A similar audit is needed for the other 9 phpt tests in ext/async/tests/channel/ that construct Channel(0) (011, 027, 028, 030, 031, 033, 039, 067, in addition to 003 and 026).
Suggested fix sketch
- Remove or repurpose
rendezvous_value/rendezvous_has_value— the value is handed over directly at the moment a sender and receiver pair up. send(capacity=0): if a waiting receiver is present, hand off directly and wake the receiver; otherwise enqueue the sender intowaiting_sendersand suspend.recv(capacity=0): if a waiting sender is present, take its value and wake it; otherwise enqueue intowaiting_receiversand suspend.sendAsync/recvAsync(capacity=0): non-blocking variants must returnfalse(or equivalent) when no counterpart is waiting — they cannot "deposit" anymore.channel_is_full/channel_count/isEmptyfor capacity=0 — express in terms ofwaiting_senders/waiting_receiversqueues.- Update tests
003and026(and any others affected) to reflect rendezvous semantics.
Side effects on chaos coverage
fuzzy_tests/channel/deadlock_detection.feature::stuck_send_into_full_channel_with_no_consumer was written around the buggy behaviour ("first send fills the slot, second blocks"). Once the runtime is fixed, that scenario can be simplified to a single send matching the corresponding stuck-recv scenario.
Environment
- Branch:
102-fuzzing-infrastructure-for-extasync(ext/asyncrepo) - Tested with WSL2 / Ubuntu 24.04, ZTS build
- Doc reference: https://true-async.github.io/en/docs/components/channels.html
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
[-]Channel(capacity=0) does not enforce rendezvous semantics — send returns without a waiting receiver[/-][+]Channel0 send returns without a waiting receiver[/+]#102: fuzzy_tests: channel/non_blocking chaos coverage
#102: fuzzy_tests: channel/scope_owned chaos coverage
#108: Channel(0) send() now blocks until a receiver takes the value
Merge pull request #113 from true-async/108-channel0-send-returns-wit…