Skip to content

Chan.try_send and Chan.try_recv answer at once: the send and receive that do not wait, shared with Chan.send and Chan.recv - #1162

Closed
aldeni wants to merge 1 commit into
bendlang:mainfrom
aldeni:chan-try-send-recv
Closed

aldeni wants to merge 1 commit into
bendlang:mainfrom
aldeni:chan-try-send-recv

Conversation

@aldeni

@aldeni aldeni commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Implements #1147, asked by @LucasGois1, with two departures from its sketch said below.

Why

Chan.send parks while the channel is full and Chan.recv while it is empty, and IO.within does not cancel the loser (WONTFIX), so a receive under a deadline still takes the next value once it arrives. Two programs need an operation that answers now: a writer that drains everything already queued into one transaction, then commits and blocks for the next; and a queue that drops under load rather than block its producers. Neither can be a hub package: the answer lives in the channel's row inside the runtime.

What changes

def Chan.try_send(-A: Type, chan: Chan(A), value: A) -> IO(Bool)
def Chan.try_recv(-A: Type, chan: Chan(A)) -> IO(Chan.Poll<&1, A>)

type Chan.Poll<a, -A: Kind(a)> is Kind(a):
  Wait{}
  Closed{}
  Got{value: A}
  • try_send answers whether the value went: to a parked receiver, or into the ring while it has room. False on a full or closed channel, and the value is dropped, as Chan.send drops it on a closed one.
  • try_recv answers Got{value} from the ring or from a parked sender, Wait{} while nothing is ready, Closed{} once the channel is closed and drained. Three answers, since Maybe cannot tell Wait from Closed, and a poller on a closed channel would never stop.

Where this departs from #1147's sketch, and why: try_send answers Bool like Chan.send rather than handing the value back in a Maybe, so the two sends read alike and a None does not mean "sent"; and the type's cases are Wait/Closed/Got (Empty is already a type in Base).

In both runtimes the step that does not wait is now one function each way, chan_put and chan_get; Chan.send and Chan.recv are that step followed by the park, and the two new effects are that step followed by an answer. chan_send_run/chan_recv_run and their JS twins lose their copies of it.

Checks (Linux x86-64)

  • tests/io/chan_try.bend: a channel with room for one (a send that goes, one that does not, a receive that takes, one that waits, one that finds it closed), and one with no room (a parked sender hands its value to try_recv, a parked receiver takes try_send's, a try_send to nobody does not go). It prints its lines on the check, interpreted, JS and C lanes.
  • The seven other channel tests (chan_*, channel_*, nat_host_chan) answer as on main; channel_send_recv is an io pin that fails on main alike.
  • gates/repo.ts passes 49 / 49. The guide is at 7968 of its 8000 ttok, so I left it alone; a clause on the two in the channels paragraph would fit if you trim elsewhere.

Not run: macOS.

…that do not wait, shared with Chan.send and Chan.recv

Chan.send parks while the channel is full and Chan.recv while it is
empty, and IO.within does not cancel the loser (WONTFIX), so a receive
under a deadline still takes the next value. A writer that drains what
is queued into one transaction, or a queue that drops under load rather
than block its producers, needs an operation that answers now (bendlang#1147).

Chan.try_send(A, chan, value) -> IO(Bool) answers whether the value
went: to a parked receiver, or into the ring while it has room. False
on a full or closed channel, and the value is dropped, as Chan.send
drops it on a closed one. Chan.try_recv(A, chan) -> IO(Chan.Poll<&1,
A>) answers Got{value} from the ring or from a parked sender, Wait{}
while nothing is ready, and Closed{} once the channel is closed and
drained; three answers, as Maybe cannot tell Wait from Closed and a
poller on a closed channel would never stop.

In both runtimes the step that does not wait is now one function each
way, chan_put and chan_get, and Chan.send and Chan.recv are that step
followed by the park; the two new effects are that step followed by an
answer. tests/io/chan_try covers a channel with room for one (a send
that goes, one that does not, a receive that takes, one that waits, one
that finds it closed) and one with no room (a parked sender hands its
value to try_recv, a parked receiver takes try_send's, and a try_send
to nobody does not go). It prints its lines on the check, interpreted,
JS and C lanes; the seven other channel tests answer as on main
(channel_send_recv is an io pin that fails on main alike);
gates/repo.ts passes 49 / 49.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@nicolas-abril nicolas-abril self-assigned this Sep 28, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

I don't like how this adds yet another variant of these same IO functions. I'll try to come up with a more general solution that can do polling in the general case, like an IO combinator to apply over IO functions that park

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks for this, the use cases and the tests are clear, and the split of the non-waiting step (chan_put/chan_get) is a good change on its own.

Before merging, I want to look at one thing: Base is starting to get one non-blocking API per type (TCP.poll, UDP.poll, and now Chan.try_send/try_recv), each with its own shape. I opened a draft, #1172, that explores one mechanism for every effect instead: IO.poll(act) answers Ready{x} or Wait{rest}, where rest holds the effect that would have waited, not started, and can be resumed or cancelled. With it, try_recv is IO.poll(Chan.recv(c)) and try_send is IO.poll(Chan.send(c, x)).

That design is not finalized. It still has open problems: a dropped rest leaks on the C lane, and cancel only covers channels. It may not go anywhere, and I might just end up adding the extra Chan functions from this PR. So please leave this open for now. If you have thoughts on #1172, especially whether it would cover your writer and drop-when-full cases, they're very welcome.

Note: this reply was written by an AI at my request. If anything here is wrong, reply and I will review it myself.

nicolas-abril added a commit that referenced this pull request Sep 29, 2026
IO.poll is the effect itself and answers Ready or Wait, with no Or and no
IO.poll.step between. io_poll_step answers io_step 1 to go on in place
(a popped frame, a hold) rather than queueing the computation again,
frames are recycled, and a polled act's last request (its continuation
the act's end, as in IO.poll(0, Chan.recv(c))) answers its frame itself,
with no Emit. On an M-series Mac, 1M loops of a send and a recv both
polled take C 90 ms (was 160), JS 350 ms (was 610), beside 50 and 230 ms
unpolled; a poll that answers Wait takes C 60 ms per 1M (was 90 to 100).

tests/io/io_poll_writer.bend and io_poll_try.bend are @aldeni's: the
writer that drains, commits and blocks on the rest, and #1162's chan_try
cases with partners already parked. The poller now goes on before a
partner it woke, as try_* did.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Closing in favor of #1259 . Please tell me if that one is good enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants