Cooperative async scheduling

The default scheduler runs other work when a task waits for an unresolved Future, when an eager task returns to its caller, or when a host polls or drains the scheduler. Awaiting an already resolved Future does not itself advance timers. A .resolve(callback) registration always defers that callback. Future.race leaves losing operations running.

Applications can opt into additional safe points:

runtime:
  cooperativeBudget: 16

RADLANG_COOPERATIVE_BUDGET=16 overrides the baked setting. The value is an integer from 0 through 2147483647. Zero, the default, disables the additional safe points. The setting is resolved on first use and cached; set its environment variable before the first async boundary.

A positive budget counts these boundaries on each scheduler thread:

  • A language await, before evaluating its Future operand.
  • sys.blockOn, before evaluating its Future operand.
  • A language .resolve(callback) registration, before it queues the new callback.

After that many boundaries, the runtime performs a nonblocking scheduler pass. This preserves callback registration order and deferral: a callback can run at a later boundary, including another registration, but never inline in its own registration. Losing race inputs keep running and are never cancelled by a safe point. Native internal await and callback-registration APIs do not insert these application safe points.

Each pass inspects at most 64 timers, resumes at most 64 ready tasks, invokes at most 64 queued microtasks, finishes at most 64 worker completions, and polls one reactor batch of at most 64 events. Pending timers rotate so long waits do not hide later ready timers. Callbacks and tasks still run cooperatively: an individual callback may take arbitrary time, and explicit blocking waits made by resumed user code retain their usual waiting behavior. Safe points do not run while runtime locks are held. Callback failures propagate to the caller of the boundary and leave subsequent safe points enabled.

The budget provides opportunities for progress, not a bound on all queued work or on execution time. A producer can enqueue work faster than the chosen budget drains it, and code with no async boundaries still needs an explicit wait or host poll. Choose a budget appropriate to the application and keep callbacks short. Compiled libraries using the older native boundary ABI keep their existing behavior until rebuilt.

Blocking native work

Worker-backed operations share one lazy blocking-I/O pool per process, bounded at sixteen threads. This is separate from CPU-range parallelism and HTTP server worker counts. A queued job is covered by at most one idle or starting worker; a burst of submissions can therefore grow the pool before its first worker has finished starting. Once the ceiling is reached, additional work stays queued. A failed extra thread start keeps accepted jobs on the existing queue.

This allows independent blocking requests to overlap without creating an unbounded number of threads. It does not guarantee an application deadline under arbitrary load. Ordinary HTTP Future cancellation still detaches the Future; it does not abort the worker’s native network operation. Losing race inputs keep their existing behavior.