Skip to content

Channel0 send returns without a waiting receiver #108

Closed

Description

@EdmondDantes

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)

Official documentation:

A channel with buffer size 0 works in rendezvous mode: send() blocks until another coroutine calls recv(), 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) and rendezvous_has_value (bool)
  • channel_is_full / channel_count for capacity=0 are computed from rendezvous_has_value
  • the send path deposits into the slot when it is empty and returns without blocking
  • channel_is_buffered() returns false for 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

  1. 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.

  2. Per-channel deadlock detection (noConsumerTimeout) does not fire on a single stuck send(), 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

  1. Remove or repurpose rendezvous_value / rendezvous_has_value — the value is handed over directly at the moment a sender and receiver pair up.
  2. send (capacity=0): if a waiting receiver is present, hand off directly and wake the receiver; otherwise enqueue the sender into waiting_senders and suspend.
  3. recv (capacity=0): if a waiting sender is present, take its value and wake it; otherwise enqueue into waiting_receivers and suspend.
  4. sendAsync / recvAsync (capacity=0): non-blocking variants must return false (or equivalent) when no counterpart is waiting — they cannot "deposit" anymore.
  5. channel_is_full / channel_count / isEmpty for capacity=0 — express in terms of waiting_senders / waiting_receivers queues.
  6. Update tests 003 and 026 (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

Activity

  1. self-assigned this
    on May 10, 2026
  2. added
    bug
    on May 10, 2026
  3. added theissue type on May 10, 2026
  4. added this to the TrueAsync 0.7.0 milestone on May 10, 2026
  5. changed the title [-]Channel(capacity=0) does not enforce rendezvous semantics — send returns without a waiting receiver[/-] [+]Channel0 send returns without a waiting receiver[/+] on May 10, 2026
  6. added 3 commits that reference this issue on May 10, 2026
  7. added a commit that references this issue on May 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions