Issue #1232: Decision: pausing tasks on several threads, and who pays for it

Open and answer this issue on GitHub.

concurrency, decision, future · opened 2026-11-04 · updated 2026-11-04 · 0 comments

The question

Should tasks that pause (wait for the network, a file, a timer) be able to run on several threads, so that one task doing heavy work between two pauses does not hold back the others? And if so, how do we make sure that only the programs that gain from it pay for it?

This is not needed for the current focus. It is filed now so the reasoning is not lost, and labelled future.

How it works today

There are two kinds of task.

Waiting itself costs no thread. Sockets are watched by the operating system (epoll, kqueue, IOCP) and files go through io_uring on Linux, so a thousand tasks waiting for answers need no thousand threads.

The weak spot is work between two pauses:

async fn handle(req: Request) -> Response {
    let body = await read_body(req)      // pauses: cheap
    let report = render_pdf(body)        // 200 ms of pure computation
    return await send(report)            // pauses: cheap
}

While render_pdf runs, every other request of the same group waits, even if seven cores are idle. Only the siblings in that group are held back, not the whole program, but within the group this is the same problem a single event loop has in other languages.

What moving tasks across threads costs

Letting a pausing task continue on another thread (option B in the discussion) is not free:

  1. Shared bookkeeping. The queue of ready tasks and the “wake me up” handles must be safe across threads: atomic counters and locks instead of plain fields. Every pause and every wake pays a little, also when no task ever moves.
  2. Cold caches. A task that resumes on another core finds its data in the old core’s cache. For short tasks this can cost more than it saves.
  3. Restricted borrowing. A task may move only if what it borrows is safe to share between threads. The compiler must check this per task, and a task that borrows something that is not, stays on its home thread.
  4. Harder to predict. Order of execution and timing differ from run to run, which makes measuring and debugging harder.

So the real question is not “B or not B” but when the program pays these costs.

The principle: only who uses it pays

A program, or a group of tasks, that cannot gain from moving tasks must run exactly as today: one thread, plain fields, no atomics. The costs above appear only where a task can actually move and there is a reason to move it.

Variants

A — as today. Pausing tasks stay on the group’s thread. Heavy work between pauses is the programmer’s job: hand it to the pool explicitly. Cost: none. Weakness: the silent slowdown in the example above, found only by measuring.

B1 — the compiler decides per group, statically. When a group has several pausing tasks and every one of them borrows only shareable data, the compiler builds that group with the shared, movable machinery; every other group keeps the single-thread path. Cost: only in groups that qualify, but in those always, even if no task is ever heavy. Weakness: the compiler cannot know which tasks are heavy, so it may pay for nothing.

B2 — start at home, move when a task overruns. Every task starts on its group’s thread as today. The runtime notes how long a task runs between two pauses. Only when one run exceeds a threshold (say 1 ms) are the waiting siblings released to other threads. Cost: reading the clock once per resume (a few nanoseconds), plus the shared machinery only once a group has overrun. Weakness: the first overrun is not prevented, only the following waits; the threshold needs measuring.

B3 — others take work only when the home thread is busy. Idle threads of the pool may take a ready sibling from a group’s queue, but only while that group’s thread is occupied, and only tasks the compiler marked as movable. Cost: the group’s queue must allow a second reader, which is one atomic operation per enqueue in movable groups. Weakness: close to classic work stealing; the benefit depends on how often the home thread is busy.

B4 — the programmer asks for it. A mark on the task, for example async(movable) handle(req), says “this task may move”. Without the mark nothing changes. Cost: exactly where written. Weakness: the programmer must know where it helps; forgotten marks bring back the slowdown.

B5 — hand off the heavy call, not the task. Instead of moving the whole task, a call to a known-heavy function inside a pausing task runs on the pool, and the task pauses until it is done. In the example, render_pdf(body) would go to the pool on its own while the other requests continue. Cost: one hand-off per such call; nothing for tasks without heavy calls. Weakness: needs to know which calls are heavy (a mark on the function, or a measured profile), and body must be shareable.

C — always movable. Every pausing task may move; borrowing anything not shareable is an error. Cost: everywhere, also in programs with one task. Weakness: takes away the free borrowing that makes pausing tasks pleasant today.

D — one thread per core, tasks pinned. Each core runs its own groups, tasks never move. Cost: none per task. Weakness: one core can be overloaded while others idle; it does not solve the example.

How the variants combine

The variants are not exclusive. A sensible layering:

Situation What runs
A group with one pausing task, or with tasks that borrow unshareable data Single-thread path, unchanged (zero cost)
A group whose tasks are all movable, nothing overran Single-thread path; one clock read per resume (B2)
Such a group after an overrun Siblings may be taken by idle threads (B3)
A call the programmer marked heavy Runs on the pool, task pauses (B5)

Recommendation

B2 together with B3, with the zero-cost path for every group that cannot move, and B5 as a later addition.

Before any default (threshold, which groups qualify) is fixed, the costs are measured as docs/measuring.md says: counted instructions against a tuned Rust baseline, for a group of short tasks (where B must cost next to nothing) and a group with one heavy task (where it must help).

What a decision changes

No build work starts before the owner decides and the version that carries it is chosen.


Generated by Claude Code

Back to Open Issues