Skip to content

A deadline or a timer fires while computations keep the loop busy (#1122) - #1245

Merged
nicolas-abril merged 3 commits into
bendlang:mainfrom
nicolas-abril:loop-glance
Oct 1, 2026
Merged

nicolas-abril merged 3 commits into
bendlang:mainfrom
nicolas-abril:loop-glance

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Collaborator

Context

io_loop only calls io_wait, which checks parked timers and sockets, when io_runs is empty. Computations that wake each other over a channel keep io_runs from ever emptying. So every parked wait (IO.sleep, IO.within's deadline, a socket's readiness) waits for the whole chain to end.

In the issue's program, within(10) and a 10 ms timer both fire only when a 2M-round channel pipeline ends: at ~100–130 ms in C and ~510 ms in JS. The JS lane has the same loop. Fixes #1122.

Change

The loop glances at its waits while it is busy. It reads the clock every 64 steps and calls io_wait without blocking when either:

  • a timer is due: io_park keeps deadlines sorted, so this check is O(1); or
  • 10 ms have passed: for sockets, since the glance's select walks every parked fd.

io_wait takes back a block flag, which bf745e3 removed; with it unset, the poll's timeout is 0. The JS lane does the same with io.waits[0].at. The rate limit is there because of bf745e3: polling every 64 steps cost 650 ms over 64,000 steps beside 2,000 parked sockets. This is the shape the reporter proposed in the issue, with the timer check added so a timer isn't up to 10 ms late.

Other runtimes bound starvation the same way: Go's sysmon polls the network when nothing has for 10 ms, and Tokio checks timers and I/O every 61 scheduler ticks (event_interval). Ordering runnable steps and timers in one priority queue would be exact for timers, but it needs a clock read per step; the check every 64 steps gives the same order to within 64 steps. A queue can't order sockets at all: their readiness is only known by polling, so they need a rate either way.

tests/io/within_busy.bend races IO.within's 1 ms deadline against a 500k-round channel pipeline and expects the deadline to answer first. On main the interpreter, JS and C all print the pipeline first.

Checks

On an Apple M-series Mac, all on this PR's head against its base (607a095). For the benchmarks the machine was quiet (load 1.0–3.8), and each run alternated base and branch.

  • The issue's program: within(10) and the 10 ms timer answer at 10–11 ms in C (base: 122–138 ms) and 10–14 ms in JS (base: 502–506 ms). The pipeline itself takes the same time: 124–130 ms against 122–137 ms in C, and 503–509 ms against 496–500 ms in JS.

  • Echo server (demos/io_tcp_echos, a C load client):

    Load base branch
    1 connection 65.2k–66.4k rt/s 64.8k–65.6k rt/s
    100 connections 300.6k–306.9k rt/s 303.0k–307.2k rt/s
    2000 idle + 1 active 12.8k–13.4k rt/s 12.2k–13.3k rt/s
    2000 idle + 100 active 239.8k–243.0k rt/s 240.4k–241.5k rt/s
  • Cost of the glance: a 1M-round pipeline beside 2000 parked TCP receives runs in 47–49 ms, against 47–50 ms on base. Base never glances there.

  • Tests: all 1,530 tests under tests/ ran on both trees. Each went through the interpreter, and the 688 that build also ran in C and JS.

    • With timings and temp paths masked, every test prints the same thing on both trees in every lane, including the builds that fail. The only difference is the new test.
    • 1,368 of 1,376 compiled runs print their #| lines. The other 8 are the same on base:
      • gfx/clicks and gfx/window expect no display, and this Mac has one;
      • select_eintr, tcp_connect_black_hole and tcp_send_slow_peer end with an extra blank line, which the gate trims.
  • Size: comp.ts goes from 63,213 to 63,406 ttok (+193).

  • Static checks: bun gates/repo.ts passes (50/50). tsc reports no errors in comp.ts, and the same 12 elsewhere as on base.

  • Not run: the cluster gates, and the Linux lane.

Alternative measured

kqueue/epoll instead of select, with the same glance, removes the O(parked) cost of each wait:

  • Many idle connections: with 2000 idle and 1 active, it does 51–91k rt/s against 5–14k.
  • Busy connections: 100 busy connections lose about 17% (~253k rt/s). Each wait then costs a registration call, plus an fstat because kqueue misses a FIFO's close on macOS (macOS: poll() does not report a FIFO close, so a parked read never sees EOF #928).
  • Size: it costs +889 ttok, leaving 4 under the cap, and adds a platform split and a helper fallback for FIFOs.

That is a separate decision about Bend as a server for many idle clients, so it is not part of this PR.

🤖 Generated with Claude Code

nicolas-abril and others added 3 commits October 1, 2026 18:17
…e loop glances at its waits when a timer is due and every 10 ms (bendlang#1122)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[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.

IO.within's deadline, and every timer, waits while two computations talk over a channel

1 participant