Skip to content

Chan.try_send and Chan.try_recv: channel operations that answer at once #1147

Closed

Description

@LucasGois1

What Bend should do

# Answers at once. A full or closed channel hands the value back, since a
# value is affine and a failed send must not consume it.
def Chan.try_send(-A: Type, chan: Chan(A), value: A) -> IO(Maybe<&1, A>)

# Answers at once: a value, Empty{} while none is ready, Closed{} once the
# channel is closed and drained.
type Chan.Poll<a, -A: Kind(a)> is Kind(a):
  Got{value: A}
  Empty{}
  Closed{}

def Chan.try_recv(-A: Type, chan: Chan(A)) -> IO(Chan.Poll<a, A>)

# A queue that drops under load instead of blocking its producers:
def record(+queue: Chan(Span), span: Span) -> IO(Unit):
  do IO<Unit>:
    back : Maybe<&1, Span> <- Chan.try_send(Span, queue, span)
    count_dropped(back)

Why

Today a send parks while the channel is full and a receive parks while it is empty. IO.within bounds how long the caller waits, but the losing operation is not cancelled (WONTFIX, #1034), so it still acts after the deadline, on both lanes:

  • A receive under IO.within takes the next value and drops it. The first program prints within: timeout, send 1: True, send 2: True, recv: 2: the timed-out receive took 1 and handed it to IO.within's join channel, which was already closed, so 1 is gone although Chan.send answered True.
  • A send under IO.within delivers after the deadline. The second program prints within send 2: timeout, recv: 1, recv: 2. Until a receiver comes, the parked send keeps the value, so a producer that moves on after a timeout piles up parked sends.

So a bounded queue that drops under load cannot be written: the producer either blocks or loses values that were reported as sent. That queue is the core of every logging, metrics and tracing pipeline and of load shedding in a server. OpenTelemetry's batch span processor, for example, must drop spans once its queue (2048 by default) is full rather than block the application, and must not grow memory without bound. With Chan.try_send the producer answers at once and counts the drop. With Chan.try_recv an exporter drains what is queued without parking, so it needs no deadline race at all; #1122 shows that a deadline cannot be relied on while channels are busy anyway.

A library cannot add these. The channel's state lives in Base's chan effect, IO.fork, IO.join and IO.within are built on Base's Chan, and a library's own channel would need a handle type of its own (#825). Neither operation parks, so neither needs cancellation. Go (select with default), Rust (try_send/try_recv), Java (offer/poll), Kotlin (trySend/tryReceive) and .NET (TryWrite/TryRead) all provide them.

The receive: bend main.bend and bend main.bend -o main && ./main print the same
import Base

def show(got: Maybe<&1, U32>) -> String:
  match got:
    case None{}:
      "none"
    case Some{n}:
      U32.show(n)

def show_within(got: Maybe<&1, Maybe<&1, U32>>) -> String:
  match got:
    case None{}:
      "timeout"
    case Some{inner}:
      show(inner)

def producer(+c: Chan(U32)) -> IO(Unit):
  do IO<Unit>:
    u : Unit <- IO.sleep(50)
    a : Bool <- Chan.send(U32, c, 1)
    v : Unit <- IO.print("send 1: " ++ Bool.show(a))
    b : Bool <- Chan.send(U32, c, 2)
    IO.print("send 2: " ++ Bool.show(b))

def run(c: Chan(U32)) -> IO(Unit):
  +ch = c
  do IO<Unit>:
    w : Maybe<&1, Maybe<&1, U32>> <- IO.within(Maybe<&1, U32>, 10, Chan.recv(U32, ch))
    u : Unit <- IO.print("within: " ++ show_within(w))
    s : Unit <- IO.spawn(Unit, producer(ch))
    r : Maybe<&1, U32> <- Chan.recv(U32, ch)
    IO.print("recv: " ++ show(r))

def main() -> IO(Unit):
  do IO<Unit>:
    c : Chan(U32) <- Chan.new(U32, 0)
    run(c)
within: timeout
send 1: True
send 2: True
recv: 2
The send: both lanes print the same
import Base

def show(got: Maybe<&1, U32>) -> String:
  match got:
    case None{}:
      "none"
    case Some{n}:
      U32.show(n)

def show_sent(got: Maybe<&1, Bool>) -> String:
  match got:
    case None{}:
      "timeout"
    case Some{sent}:
      Bool.show(sent)

def run(c: Chan(U32)) -> IO(Unit):
  +ch = c
  do IO<Unit>:
    a : Bool <- Chan.send(U32, ch, 1)
    w : Maybe<&1, Bool> <- IO.within(Bool, 10, Chan.send(U32, ch, 2))
    u : Unit <- IO.print("within send 2: " ++ show_sent(w))
    r1 : Maybe<&1, U32> <- Chan.recv(U32, ch)
    v : Unit <- IO.print("recv: " ++ show(r1))
    r2 : Maybe<&1, U32> <- Chan.recv(U32, ch)
    IO.print("recv: " ++ show(r2))

def main() -> IO(Unit):
  do IO<Unit>:
    c : Chan(U32) <- Chan.new(U32, 1)
    run(c)
within send 2: timeout
recv: 1
recv: 2

bend 2.0.32, Darwin arm64, Apple clang version 21.0.0.

Activity

  1. LucasGois1 commented on Sep 28, 2026

    @LucasGois1
    Author

    One more data point from the ecosystem: bend-kit-concurrency builds select and timeout on Base's IO.spawn, IO.fork and Chan, and its README documents the same loss. select starts one relay per source. When the selector is closed, each relay drops the value it holds. timeout does not stop the action, so its late result is dropped.

    So a library that sets out to offer these operations ends up documenting the limit rather than removing it. It can only relay values out of a Chan it cannot inspect, and a relay that has already taken a value has nowhere to put it back. Chan.try_recv and Chan.try_send would let such a library poll without taking values it cannot return.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions