Skip to the content
Diaspar

Decision D006 · accepted; fibers proposed

Concurrency without function coloring and without async/await

A record of the language's design, as it stood on 2026-11-18: written for the people who build Diaspar, and kept as it was written. Its code is a sketch, not a program of the site, and an older record may show syntax that a later one changed. It names files of the language's source, which is not public yet.

Status: accepted (2026-11-15) for no coloring, no async/await, and structured scope/spawn; proposed for fibers with their own stacks, join, cancellation details, and the rule that tasks inherit context but not handlers

The model around tasks (atoms, actors, channels, the sim scheduler) is D035; its design is design/concurrency.md.

Decision

Functions are not split into synchronous and asynchronous ones. A function that waits for the network looks and is called like any other. There is no async and no await.

When a task blocks on I/O, the runtime suspends it and switches to another. Go (goroutines), Erlang, Java with virtual threads (Loom), and OCaml 5 with Eio work this way. Inside, it is an effect (D005): "wait for this socket" is an effect, and the scheduler is its handler.

Concurrency is structured: a task does not outlive the scope that started it.

Example (sketch; syntax not settled)

(defn fetch [url retries] ...)   ; an ordinary function that may wait for the network inside

(scope
  (let [a (spawn (fetch "a" 1))
        b (spawn (fetch "b" 2))]
    (+ (join a) (join b))))
;; scope waits for both tasks; if one fails, the others are cancelled and the error propagates

join is not await: it waits for one specific task and can be called from any function. A possible shorthand is @a, like deref in Clojure.

What the implementation needs

  1. A stack per task (fibers). A small growable stack, like a goroutine's. Switching takes a few dozen lines of assembly per architecture; this is part of the restricted context (D002).
  2. An event loop in the runtime on epoll, kqueue, or IOCP: a task waiting for I/O is parked, not blocking an OS thread.
  3. Blocking C calls through FFI must not stall the scheduler. Proposed in D035: the worker hands its slot to another thread for the call, as Go does for system calls.

Cancellation

Cancellation is cooperative: it takes effect at a suspension point. Inside the task it arrives as a condition, so resource cleanup works through the ordinary mechanism.

Tasks inherit context, not control handlers (accepted)

Context is values that are only read: the clock (fake in tests), the logger, the output stream, the current user, settings. A task started with spawn gets its parent's context; this copies one reference-counted pointer.

Handlers of effects that control execution (errors, restarts, generators) do not cross spawn. A task that performs such an effect without handling it itself fails; scope receives the failure and passes it to the parent as an ordinary error. A handler cannot resume a parent's computation from another fiber, so this rule keeps tasks independent.

Handlers that answer at once (kind 2 in D005: output, terminal capabilities, logging, the clock, randomness) are part of the context and are inherited: they run in the child's own fiber and never need the parent's stack. The ambient random generator is split, not shared: the child's generator is derived from the parent's key and the child's spawn index, without advancing the parent, so draws do not depend on scheduling (docs/design/std-random.md). Handlers that control execution (errors, restarts, generators) are not inherited.

(with-context #{:clock fake-clock}
  (scope
    (spawn (now))                        ; sees fake-clock: context is inherited
    (spawn (handle (parse-all lines)     ; its own handler, inside the task
             (:bad-entry [l r] (r nil))))
    (spawn (parse-all lines))))          ; :bad-entry unhandled → the task fails →
                                         ; scope cancels the others → the parent gets the error

Kotlin's coroutine context and Eio's fiber-local bindings in OCaml work similarly.

Why

  • Function coloring (async/await in JS, Python, Rust, C#) splits libraries in two and forces whole call chains to be rewritten when one wait is added.
  • In Ferron, resumable functions kept their frames on the heap, which is hidden coloring inside the compiler. Fibers avoid it.
  • Structured concurrency (Kotlin, Swift, Trio, Ferron) rules out "lost" tasks and makes errors and cancellation predictable.

Rejected alternatives

  • async/await: function coloring.
  • Rewriting into continuation-passing style (CPS), as Hoot does: slower, and it complicates the C backend.
  • Bare threads without structure: leaked tasks, unpredictable cancellation.

Answered questions (2026-11-17, D035)

  • Cancellation checks in long loops without I/O: yes. A loop that calls no compiled function polls at its back-edge; these are the polls D020 puts in --repl builds, and the same poll preempts a fiber and delivers cancellation (proposed; whether release builds keep it is D020's measurement).
  • Actors and supervisors: a library over scopes (accepted). An actor is a fiber that reads a bounded mailbox; a supervisor is a scope in which the failure of one child does not cancel the others, with a restart policy.