Skip to content

Every channel and socket wait has a try_ twin with a limit ms, answering Poll - #1259

Merged
nicolas-abril merged 7 commits into
bendlang:mainfrom
nicolas-abril:io-try
Oct 5, 2026
Merged

nicolas-abril merged 7 commits into
bendlang:mainfrom
nicolas-abril:io-try

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Context

Base has one non-blocking API per handle type, each with its own shape:

This PR gives every channel and socket operation that can wait one shape: the plain name blocks, and a try_ twin takes a limit ms and never waits longer. It supersedes TCP.poll, UDP.poll and #1162. Closes #1147.

Change

One new type. A try_ answers Ready{x}, where x is exactly what its blocking twin answers, or Wait{rest} once ms passes (0: at once), where rest is what did not happen:

type Poll<a, b, -A: Kind(a), -R: Kind(b)> is Kind(a <&> b):
  Ready{value: A}
  Wait{rest: R}

A blocking call never answers Wait, so its type doesn't mention it. A try_ caller matches both layers at once: case Ready{Done{Some{data}}}, case Wait{rest}.

Blocking answers use Maybe and Result. Only operations that can end get a Maybe, and a failed send hands back what wasn't sent:

blocking try_
Chan.send(c, v) Result<A, Unit> (was Bool) Chan.try_send(c, v, ms): Poll<Result<A, Unit>, A>
Chan.recv(c) Maybe<A> (unchanged) Chan.try_recv(c, ms): Poll<Maybe<A>, Unit>
TCP.recv(s, max) Result<Err, Maybe<String>> (was "" at the end) TCP.try_recv(s, max, ms)
TCP.recv_bytes(s, max) Result<Err, Maybe<List<U32>>> TCP.try_recv_bytes(s, max, ms)
TCP.send(s, data) Result<Err & String, Unit> TCP.try_send(s, data, ms): Wait{rest} after a partial write
TCP.send_bytes(s, data) Result<Err & List<U32>, Unit> TCP.try_send_bytes(s, data, ms)
TCP.accept(l) Result<Err, Socket> (unchanged) TCP.try_accept(l, ms)
UDP.recv_from(s, max) Result<Err, Addr & String> (unchanged) UDP.try_recv_from(s, max, ms)
UDP.send_to(s, h, p, data) Result<Err & String, Unit> UDP.try_send_to(s, h, p, data, ms)

Every outcome of an operation lands on a distinct path:

  • Chan.send: Fail{v} means closed. try_send's Wait{v} means full. Both hand v back.
  • TCP.recv:
    • the peer's end is Done{None};
    • errors are Fail;
    • a max of 0 stays Fail{EINVAL};
    • Some never holds "".
  • UDP.recv_from: it has no Maybe, because UDP has no end and an empty datagram is valid data. A max of 0 fails with EINVAL at once, as in TCP, instead of taking a waiting datagram and dropping it.
  • What "sent" means: the kernel took the bytes, not that the peer read them. So a send's rest is what the kernel didn't take.
  • TCP peer close on send: a peer's close only shows up as EPIPE or ECONNRESET, so for TCP sends it's an error code, not a separate case. The OS reports it no other way.

Runtime.

  • Sockets: they already had a "socket or deadline, whichever comes first" wait (TCP.poll used it). Every socket effect now carries its deadline in w->time, with 0 for the blocking twin, and answers Wait once it passes. As on main since comp.ts back under its cap by removing mechanisms at their root: a global File, one way to read a shared cell, and effects that park themselves #1281, TCP.accept and UDP.recv_from wait for their socket to be readable before their first try, so a backlog can't starve the timers; their try_ twins wait the same way with their deadline, answering when the socket is ready or the deadline passes. Every effect registers with io_eff(cid, run).
  • Channels: a waiter sits on its channel's queue and can't also sit on the timer list. So a try_ waiter parks a small timer whose expiry takes it off the channel. Whichever comes first removes the other: the timer cuts the waiter from the channel, or a wake cuts the timer from the park list in O(1) (io_park_cut; io_park now links both ways). JS splices the timer from its waits list. A woken waiter leaves nothing parked, so deadlock detection and throughput are as for a blocking wait.
  • Same-pass deadlines: JS's io_wait now runs a due wait at once, as C's runs pack, instead of queueing it behind runnable fibers. So when a try_ deadline and an earlier timer fall due in the same pass, both lanes withdraw the waiter before anything can hand it Ready.
  • Poll everywhere: Poll joins RUNTIME_ADTS, so every effect can name Ready and Wait.
  • Shared helpers: comp.ts holds the deadline rule once per lane. In C, io_until(ms) gives a deadline, io_again(w) decides whether to park again, and io_poll_end(e, w, rest, r) answers Wait{rest}, Ready{r} or r. JS has io_until, io_late and io_ready. C's io_err builds the (code, message) pair that io_fail boxes and that a failed send pairs with its rest; JS's io_fail(code, ...rest) does both.

A timed-out try_ withdraws its own wait, which IO.within can't do (its loser still runs). That means a receive with a deadline never swallows a later value.

Breaking

  • Chan.send answers Result instead of Bool.
  • TCP.recv/recv_bytes answer None{} at the peer's end instead of ""/[].
  • Sends fail with (error, rest) instead of error.
  • UDP.recv_from with a max of 0 fails with EINVAL instead of answering an empty datagram.
  • TCP.poll and UDP.poll are gone. Their replacements are TCP.try_recv(s, max, ms) and UDP.try_recv_from(s, max, 0).
  • Reserved names: no module can declare a constructor that shares a name with one of Base's. parse_fresh rejects a new constructor whose bare name is already taken, and Base's constructors are stored under bare names. So Ready and Wait are now off limits in the main file, in imported modules and in hub packages. tests/reg/show_shared_layout declared its own Ready (as an arbitrary name), so it now uses Final.

Hub packages that use these need updating.

Known limits

  • Resending part of a String. When the kernel takes part of a multi-byte character, the String rest decodes the cut as U+FFFD, so resending it isn't byte-exact. TCP.send_bytes is.
  • A String holding a surrogate comes back from a failed C send as U+FFFDs, since its rest is decoded from the UTF-8 it was sent as. JS refuses such a String before it gets that far; Char.from_u32 of a surrogate fails in JS and the interpreter but prints invalid UTF-8 natively #1080 tracks C accepting it.
  • IO.within still doesn't cancel its loser.
  • Files aren't covered. A try_read only means something on pipes, FIFOs and stdin; a follow-up can give File.read the same Maybe end.

Checks

After rebasing onto main d052d4f (with #1281's effect API and #1340's fetch loop), on an Apple M4:

  • Adaptation: the effects register with two-argument io_eff, JS keeps no need functions, accept and receive wait for readability first as on main (their try_ twins with the deadline), and JS io_fail keeps main's io_strerror fallback. Otherwise every file carries the same changes as before the rebase. Main's udp_backlog_timer test now matches UDP.send_to's failure with its rest. fix(demo): read fragmented HTTP responses until EOF #1340's read-until-EOF loop in demos/io_http_fetch and its copy in tests/io/http_fetch_eof end on Done{None} and match a failed send's rest.
  • Static checks: every commit typechecks; across all tests, demos and benches, the only check failure the branch adds over main was udp_backlog_timer, now fixed.
  • All tests (gates/test.ts's shard script on this Mac): 1,574 of 1,576 pass, the two gfx tests failing as on main.
  • Cluster gates: repo 50/50, test 1,576/1,576, perf 121/124 (GPU cells around the 1.15x line, as on main). comp.ts is 63,464 ttok against main's 63,132.

Before the rebase:

On an Apple M-series Mac, against main (f2cc47c):

  • All tests: all 1,535 tests under tests/ on main, and all 1,539 on the branch, ran on every lane. Each went through the interpreter, and every program that builds also ran in C and JS.
    • Expected lines: 1,386 of 1,388 compiled runs print their #| lines. The other two are gfx/clicks and gfx/window, which expect no display (this Mac has one) and fail identically on main.
    • Output differences: with timings masked, only 14 tests print differently from main, and every one is intended.
      • New: chan_try, tcp_try, tcp_try_bytes and udp_try cover each try_ with ms 0, with a deadline that passes, and with a wake before the deadline. They also check that a timed-out receive doesn't swallow a later value, that try_send and try_send_bytes answer Wait{rest} against a full kernel buffer, and that closed channels and failed sends hand their value back (a rejected byte list comes back whole).
      • Renamed: tcp_poll and udp_poll became tcp_try_recv and udp_try_recv_from. They check the same things through the new functions.
      • Changed expected lines:
        • chan_close_send: TrueFalse becomes DoneFail2.
        • tcp_bytes: the rejected list comes back, 22, kept 1 256.
        • tcp_recv_max_zero: poll: becomes try_recv:.
        • tcp_short_recv: end: becomes ended.
        • udp_bad_address: each failure prints the datagram it hands back.
        • show_shared_layout: Ready becomes Final.
    • Repeated runs: the new tests passed 5 to 10 runs in a row on each lane.
  • Demos: io_tcp_echos, io_http_server, io_http_fetch and io_rollback_netcode build in C and JS. Run by hand, they behave as before: the echo server echoes, curl gets the page, the fetch prints a response, and two netcode bots stay in sync.
  • Size: comp.ts goes from 63,502 to 63,834 ttok (63,868 merged with current main); the review fixes below add 158. Poll joins RUNTIME_ADTS, and the deadline rule each effect shares is defined there once per lane: io_until, io_again and io_poll_end in C, and io_until, io_late and io_ready in JS. Those helpers cost 171 ttok in comp.ts and save 594 across the effects. GUIDE.md is unchanged: it is 32 ttok under its cap, too few to describe try_ without cutting something else.
  • Static checks: bun gates/repo.ts passes (50/50). tsc reports the same 12 errors as on main, none in comp.ts.
  • After Lorenzo's review (fbe3861, 111e1ef):
    • All tests: gates/test.ts's own shard script, run on this Mac (interpreter, JS and C): 1,529 of 1,531 pass, the same two gfx tests failing as on main.
    • New tests, each failing on 987d60d:
      • chan_try_deadlock: a try_recv(c, 60000) woken by a sender, then a recv nobody answers, reports the deadlock at once (it waited out the 60 s on every lane).
      • chan_try_late: a 10 ms try_recv and a sender's 1 ms sleep fall due in one pass, held there by a busy computation; every lane answers Wait (JS and the interpreter answered Ready{7}).
      • udp_recv_max_zero: try_recv_from and recv_from with a max of 0 fail with EINVAL, on a quiet socket and with a datagram waiting, which a later recv_from still reads.
      • udp_bad_address also sends to "127.0.0.1\u{0}x", which fails with EINVAL (without the check, C sent it).
    • Throughput: a try_recv(c, 30000) per message, 1M messages: JS 12.05 s and 1.3 GB on 987d60d, 0.31 s and 84 MB now; C's peak memory 55 MB, now 1 MB.
    • Memory: 400k rounds of a UDP send, receive and failed send stay at 1.6 MB peak (8c151b9 reached 7.9 MB; e385493's io_poll_end had already fixed that).
    • Demos: demos/io_tcp_echos/PROOF.bend checks again; every demo checks as on main.
  • Cluster gates (gates/_run.ts, on this branch at 111e1ef merged with main at 57b6c15, 27.5 s):
    • repo 49/49, test 1,533/1,533.
    • perf 106/117, failing the same cells as main alone (also 106/117): PAR-CPU space on tree-matmul, mandelbrot, tree-radix, nbody and gameoflife (1.2x to 4.6x of their pins on both), and GPU times around the 1.15x line that flip between runs. Every other cell is within a few percent of main, and every output matches its pin.
    • ping skipped: the site repo isn't checked out on this Mac.
  • Not run: Linux, and the ping gate. The socket effects compare errno values that differ between macOS and Linux, as the code they replace did.

🤖 Generated with Claude Code

@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

A review turned up some correctness problems. Each one below has a reproduction on both the C and JS lanes, and none of them is caught by the new tests.

  1. An early-woken channel try_ leaves its timer parked, which hides deadlocks and slows the loop (effs/chan.c:96,108, effs/chan.js:58)

When a try_ waiter is woken before its deadline, its timer is only disarmed, so it stays in io_park (C) or io.waits (JS) for the full ms. That breaks two things:

  • Deadlock detection. The deadlock checks (comp.ts:5974, comp.ts:6382) can't fire while the dead timer is parked. For example, a fiber does Chan.try_recv(c, 6000), gets answered at once, then blocks in Chan.recv on a channel nobody sends to. main reports the deadlock in 0.01 s; this branch takes 6 s on both lanes. With ms = 4294967295 it hangs for about 49.7 days instead of exiting 1. That contradicts tests/io/deadlock.bend and the GUIDE.
  • Throughput. A consumer doing try_recv(c, 30000) per message took 31.7 s and 266 MB on C for 8M messages, against 0.5 s and 1.7 MB when the timer is unlinked on wake. With one fiber parked in a blocking UDP.recv_from, the cost grows quadratically, because io_park_add walks past every dead timer. On JS, 1M messages took 12.1 s and 1.45 GB.

This came in with 8c151b9. At 311881b the timer was unlinked on wake, and the same programs ran at blocking speed. A park entry that can be cancelled with an O(1) unlink would fix both problems. It would also stop overloading the waiter's made field as both the timer pointer and the "is try_" flag.

  1. C and JS disagree when a try_ deadline and a wake land in the same loop pass (effs/chan.js:50)

On C, chan_late removes the timed-out waiter inside io_wait itself. On JS, the waiter's io_wake only joins the run queue, so a fiber already queued ahead of it still finds the expired waiter in row.wait and hands it Ready. Take a capacity-0 channel. Fiber S does IO.sleep(20) then Chan.send(c, 7). The main fiber does IO.sleep(0), then Chan.try_recv(c, 20), then a Chan.recv if it got Wait. C prints A: wait / A: then got 7 / S: sent. JS prints S: sent / A: ready got 7, on every run. So JS delivers to a waiter whose deadline already passed, which contradicts the Poll docs. Any test that hits this ordering passes on one lane and fails on the other.

  1. C heap leaks: a result is built before the outcome is known, then overwritten
  • effs/udp_recv_from.c:17 builds Wait{Unit} up front and replaces it whenever a datagram or error arrives. That leaks 8 bytes on every received datagram, including in the blocking UDP.recv_from, which doesn't leak on main. Over 3M datagrams, max RSS reached 25.8 MB, against a flat 1.8 MB with the fix. The netcode relay's Server.loop would grow about 290 MB/hour at 10k datagrams/s.
  • effs/udp_send_to.c:19 and effs/tcp_send.c:22 build Done{Unit} up front and replace it on failure, and udp_send_to.c also replaces it on Wait. That leaks 8 bytes per failed send and per UDP.try_send_to that answers Wait. 4M failing sends reached 33.7 MB against 1.7 MB.

Building only the branch that is actually returned fixes all three, which is what io_res did on main.

  1. UDP.recv_from with max = 0 doesn't fail with EINVAL (effs/udp_recv_from.c:29, and the JS version)

base.bend now says a max of 0 "fails with EINVAL, as in every receive below", but the UDP effects never check for it:

  • With a datagram queued, UDP.recv_from(s, 0) answers Done{(127.0.0.1, p, "")} and the datagram is silently lost.
  • On a quiet socket on macOS, try_recv_from(s, 0, 50) answers at once with Ready{Done{("0.0.0.0", 0, "")}}, a datagram that was never sent.

The f[1] == 0 guard in tcp_recv_start can be copied over.

  1. The io_tcp_echos demo's proofs no longer type-check (demos/io_tcp_echos/LAWS.bend:10,16)

echo_step now takes Result<…, Maybe>, but the laws still pass Done{SCon{c, t}} and Done{""}. bend demos/io_tcp_echos/PROOF.bend --check-only fails with expected: Maybe<&1, String> / observed: String. Changing them to Done{Some{SCon{c, t}}} and Done{None{}} restores main's result. The (an empty recv) comment at line 12 is also out of date. No gate checks demos, so CI won't catch this.

Non-blocking

  • Repeated try_ code. The Ready wrapping and the deadline logic are copied into every effect on both lanes, in a few slightly different forms (at != 0, by != 0, w->time ?). The leaks in item 3 come from one of these copies. Helpers such as io_ready(e, at, r) and io_after(ms) next to io_res would keep them in one place.
  • Repeated error builder. The Fail{((code, msg), rest)} payload repeats io_fail's body three times in C, and JS patches .error in three places. An io_err for the (code, msg) pair, plus an optional rest argument on JS's io_fail, would make each site one expression.
  • send_bytes walks its list twice. tcp_send_bytes_start checks for values above 255 in its own pass before io_cbuf walks the list again, costing about 20–25% more CPU on large lists. An io_cbuf that checks before consuming the list, as JS io_unlist already does, would remove this. It would also remove the unexplained strdup("-") host workaround in udp_send_to_start.
  • Small things in chan.c. Wait{rest} is built in two places, where JS has a single chan_rest. The last ternary arm in chan_cut assigns *q to itself. The made = 0 stores after a wake are never read.

@aldeni

aldeni commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

The design covers everything #1162 was for, and more (the rest on a send and on a partial write). Below is a Linux x86-64 run of 987d60d: the tests, the cost, and how @Lorenzobattistela's points show up there.

Tests. On the check, interpreted, JS and C lanes, these print their #| lines: chan_try, tcp_try, tcp_try_bytes, tcp_try_recv, udp_try, udp_try_recv_from, and the changed chan_close_send, tcp_bytes, tcp_recv_max_zero, tcp_short_recv, udp_bad_address, tcp_loopback. I did not run the whole tree.

Cost. 1M operations, 5 runs each, f2cc47c against this head:

1M × f2cc47c this PR
blocking Chan.send + Chan.recv, both ready C 104–107 ms, JS 575–600 ms C 108–109 ms, JS 587–608 ms
try_recv(c, 0) on an empty channel (#1162's try_recv: C 51 ms, JS 300 ms) C 52–56 ms, JS 283–297 ms

The step that does not wait costs what #1162's did, and the blocking path pays about 3% on C and 0–4% on JS, close to the spread between runs.

The review's points on Linux.

  • 1, the parked timer, throughput. One producer, a channel with no room, and a consumer that takes each message with Chan.try_recv(c, 30000), on C:

    messages Chan.recv Chan.try_recv(c, 30000)
    300k 0.09 s, 24 MB
    500k 1.9 s, 35 MB
    700k 30.1 s, 40 MB
    1M 0.11 s, 14 MB 41–46 s, 41–43 MB

    With ms 5000 the 700k run takes 5.3 s, so past some count of parked timers the run lasts as long as the deadline. On JS, 1M take 62 s and 1.08 GB, against 0.72 s and 90 MB with Chan.recv.

  • 1, the deadlock report. A fiber answered at once by Chan.try_recv(c, 6000) that then blocks for good is reported after 6.0 s on C and JS, against 2 ms without the try_. This is the cost the description names.

  • 2, C and JS disagree. His program prints A: wait / A: then got 7 / S: sent on C and S: sent / A: ready got 7 on JS, three runs out of three each.

  • 3, the leaks. They don't show here. Max RSS stays at 13.9 MB on both f2cc47c and this head over 3M UDP.send_to + UDP.recv_from on loopback, and over 4M UDP.send_to that fail with EINVAL. My programs may miss his shape.

  • 4, UDP max 0. With two datagrams queued, UDP.recv_from(s, 0) answers Done with "" and the first datagram is gone, on C and JS. f2cc47c does the same, so the behavior is not new; what is new is Base's comment that every receive fails with EINVAL.

  • 5, the demo's proofs. bend demos/io_tcp_echos/PROOF.bend --check-only fails at echoes_data, as he says.

Fit. The shapes match what a networked program needs: a receive with a deadline is TCP.try_recv, a byte-level one is TCP.try_recv_bytes, and a send with a deadline hands back what the kernel did not take. Moving code off TCP.poll is one line per call. The reserved Ready and Wait names are the one cost to existing programs worth stating in the changelog.

nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Oct 2, 2026
…s in the pass that finds it, and a UDP receive of max 0 fails with EINVAL

From Lorenzo's review of bendlang#1259:

- io_park links both ways, so chan_wake cuts and frees a try_ waiter's
  timer in O(1) (io_park_cut); JS splices it from the waits. A dead timer
  no longer hides a deadlock for its whole ms, and a try_recv per message
  runs at blocking speed (1M messages: JS 12.05 s, 1.3 GB -> 0.31 s, 84 MB;
  C 55 MB -> 1 MB). made is only the waiter's timer, 0 if not try_.
- JS io_wait calls a due wait's more at once, as C's io_wait calls pack,
  so a try_ deadline that passes in the same pass as an earlier timer
  withdraws its waiter before a fiber ahead of it in the run queue can
  hand it Ready. io_wake goes.
- UDP.recv_from and try_recv_from fail with EINVAL for a max of 0, as
  base.bend says every receive does; recv_from parks itself, like
  try_recv_from, instead of waiting for a readable socket first.
- The echo demo's laws pass Done{Some{..}} and Done{None{}}, so PROOF.bend
  checks again.
- io_err builds the (code, message) pair once; JS io_fail takes the rest
  a failed send hands back. udp_send_to_end answers a NUL host with
  EINVAL instead of a "-" host. chan_wait builds Wait{rest} once per lane.

New tests: chan_try_deadlock, chan_try_late and udp_recv_max_zero, which
fail (time out, or print the other lane's order) on 987d60d.

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

nicolas-abril commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Pushed fbe3861 and 111e1ef; each item has a test that fails on 987d60d.

  1. Woken try_ timers. io_park now links both ways, so a wake cuts and frees the waiter's timer in O(1) (io_park_cut), and JS splices it from its waits. made is only the waiter's timer, 0 when not try_. chan_try_deadlock (a try_recv(c, 60000) woken by a sender, then a dead recv) reports the deadlock at once on every lane, where it waited out the 60 s. A try_recv(c, 30000) per message, 1M messages: JS 12.05 s / 1.3 GB → 0.31 s / 84 MB, C 55 MB → 1 MB peak.
  2. Same-pass deadline and wake. JS's io_wait runs a due wait at once, as C's runs pack, rather than queueing it behind runnable fibers (io_wake is gone). chan_try_late reproduces your case: a busy computation holds the loop past a sender's 1 ms sleep and a 10 ms try_recv. JS and the interpreter printed S: sent / A: ready got 7; now every lane prints A: wait / A: then got 7 / S: sent.
  3. Leaks. These were in 8c151b9; e385493's io_poll_end already builds only the branch it returns. 400k rounds of a UDP send, receive and failed send peak at 7.9 MB on 8c151b9 and stay at 1.6 MB on 987d60d and now.
  4. UDP max 0. Both receives fail with EINVAL at once, as TCP's do. The blocking recv_from now parks itself like try_recv_from instead of waiting for a readable socket first, so it fails right away on a quiet socket too. Covered by udp_recv_max_zero.
  5. Echo laws. They pass Done{Some{..}} and Done{None{}}, and the comment says None; PROOF.bend checks, and every demo checks as on main.

Non-blocking:

  • Error builder: C's io_err builds the (code, message) pair once (io_fail is a macro over it), and JS's io_fail(code, ...rest) takes a send's rest. Together they save 112 ttok across the effects and add 25 to comp.ts, 87 net.
  • strdup("-"): gone. A host with a NUL is blanked, which io_sys_addr refuses as any malformed host; udp_bad_address now sends to "127.0.0.1\u{0}x", which C used to send.
  • chan.c: chan_wait builds Wait{rest} once per lane, chan_cut lost its self-assigning arm, and the made = 0 stores went.
  • send_bytes' two walks: left as they are. C must hand a rejected list back whole, so one pass would need io_cbuf to stop at the bad value and rebuild the list's front, which costs more in comp.ts than it saves outside large byte lists.

gates/test.ts's shard script, run locally: 1,529 of 1,531 pass, the same two display-dependent gfx tests failing as on main. comp.ts is 63,834 ttok (63,868 merged with main). Not run: the cluster gates and Linux.

Note: this comment was written by an AI after it fixed the PR for me. If anything here is wrong, reply and I will review it myself.

nicolas-abril added a commit to nicolas-abril/bend that referenced this pull request Oct 5, 2026
…s in the pass that finds it, and a UDP receive of max 0 fails with EINVAL

From Lorenzo's review of bendlang#1259:

- io_park links both ways, so chan_wake cuts and frees a try_ waiter's
  timer in O(1) (io_park_cut); JS splices it from the waits. A dead timer
  no longer hides a deadlock for its whole ms, and a try_recv per message
  runs at blocking speed (1M messages: JS 12.05 s, 1.3 GB -> 0.31 s, 84 MB;
  C 55 MB -> 1 MB). made is only the waiter's timer, 0 if not try_.
- JS io_wait calls a due wait's more at once, as C's io_wait calls pack,
  so a try_ deadline that passes in the same pass as an earlier timer
  withdraws its waiter before a fiber ahead of it in the run queue can
  hand it Ready. io_wake goes.
- UDP.recv_from and try_recv_from fail with EINVAL for a max of 0, as
  base.bend says every receive does; recv_from parks itself, like
  try_recv_from, instead of waiting for a readable socket first.
- The echo demo's laws pass Done{Some{..}} and Done{None{}}, so PROOF.bend
  checks again.
- io_err builds the (code, message) pair once; JS io_fail takes the rest
  a failed send hands back. udp_send_to_end answers a NUL host with
  EINVAL instead of a "-" host. chan_wait builds Wait{rest} once per lane.

New tests: chan_try_deadlock, chan_try_late and udp_recv_max_zero, which
fail (time out, or print the other lane's order) on 987d60d.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril and others added 7 commits October 5, 2026 21:12
…ing Poll: Ready{what the blocking twin answers} or Wait{what did not happen}

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…iting JS send builds its rest only then, the bytes twins have a test, and the guide stays as on main

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…d each new comment one line

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…again and io_poll_end in C, io_until, io_late and io_ready in JS

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…s in the pass that finds it, and a UDP receive of max 0 fails with EINVAL

From Lorenzo's review of bendlang#1259:

- io_park links both ways, so chan_wake cuts and frees a try_ waiter's
  timer in O(1) (io_park_cut); JS splices it from the waits. A dead timer
  no longer hides a deadlock for its whole ms, and a try_recv per message
  runs at blocking speed (1M messages: JS 12.05 s, 1.3 GB -> 0.31 s, 84 MB;
  C 55 MB -> 1 MB). made is only the waiter's timer, 0 if not try_.
- JS io_wait calls a due wait's more at once, as C's io_wait calls pack,
  so a try_ deadline that passes in the same pass as an earlier timer
  withdraws its waiter before a fiber ahead of it in the run queue can
  hand it Ready. io_wake goes.
- UDP.recv_from and try_recv_from fail with EINVAL for a max of 0, as
  base.bend says every receive does; recv_from parks itself, like
  try_recv_from, instead of waiting for a readable socket first.
- The echo demo's laws pass Done{Some{..}} and Done{None{}}, so PROOF.bend
  checks again.
- io_err builds the (code, message) pair once; JS io_fail takes the rest
  a failed send hands back. udp_send_to_end answers a NUL host with
  EINVAL instead of a "-" host. chan_wait builds Wait{rest} once per lane.

New tests: chan_try_deadlock, chan_try_late and udp_recv_max_zero, which
fail (time out, or print the other lane's order) on 987d60d.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…NVAL through its one end

udp_send_to_end cost 58 ttok more than blanking the host, which
io_sys_addr then refuses as it refuses any malformed one. udp_bad_address
probes "127.0.0.1\u{0}x": without the check, C reads the quad before the
NUL and sends.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril merged commit 9cab39a into bendlang:main Oct 5, 2026
1 check passed
@nicolas-abril
nicolas-abril deleted the io-try branch October 5, 2026 21:24
Giulio2002 added a commit to Giulio2002/bend-collections that referenced this pull request Oct 28, 2026
…-verdict (#59)

* fix(intrusive list): rename Root's Ready to RootReady, for Bend 2.0.36+

Bend 2.0.36 added `type Poll ... Ready{value} Wait{rest}` to Base
(bendlang/bend#1259), and Base's names are global, so `type Root is Data:
Ready{} Waiting{}` fails with "duplicate declaration: Ready" on 2.0.36 and
2.0.37. That stops the intrusive list's proof, its example and every root
that imports them (laws_containers.bend, proofs/PROOF.bend).

array_adapter.bend is regenerated from array_adapter.src with
tools/generators/mac.py (which reproduces the committed file byte for byte
before the change).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* proofs: concrete calls of the generic laws --verdict needs a model for

From bendlang/bend#1580 (on bend main, after 2.0.37), --verdict takes a ~
constant's model from a call of its def at concrete ~ arguments, and no
longer searches for one. Nothing called prepend_valid, remove_head_valid,
optional_suffix, remove_nonhead_valid, prepend_preserves,
remove_head_preserves or remove_nonhead_preserves (at ~reflex) or set_post
(at ~hp) at any, so the kernel never saw them. One call each, at Bool (with
a local bool_eq) and at the empty map with m2 = set(m, key, x), gives them
models; the laws still hold for every argument. On 2.0.37 these calls
check and change nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(proofs): preserve verdict instances in edits source template

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Giulio Rebuffo <[email protected]>
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.

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

3 participants