Skip to content

with_concurrency documents a limit but silently ignores n #981

Closed

Description

@eas4ai

Classification

Silent no-op defect (audit finding TASK-001). Same file is loud everywhere else (await_first on empty input is todo()), so this one silent stub is exactly the mishandle-10%-silently shape.

Source chain

lib/std/task.w:231-233 documents 'Limit concurrent execution to at most n tasks at a time' but the body is let _ = n; tasks. Callers bounding resource use get unbounded execution. (Combinators await_all/await_first/await_any/await_settled all carry defer-based join_cleanup — reviewed disciplined.)

Repair boundary

One line (todo()) or the real limiter.

Activity

  1. eas4ai commented on Sep 3, 2026

    @eas4ai
    ContributorAuthor

    Worse than a silent no-op: the stub does not compile when called. await_all(with_concurrency(tasks, 1)) fails with error: return type mismatch pointing inside the embedded stdlib (task.w:232 let _ = n and task.w:233 tasks), cascading to no matching generic overload for 'await_all' at the call site. So the documented limiter is not merely ignored — it is unusable, and the error misdirects at the caller's overload resolution instead of the stub. This also removes the only stdlib-level mitigation callers have for #995 (batching spawns under the 1024 live-fiber cap). Repro probe available on request (1100-task build-tasks + with_concurrency(.,1), seed stage1 -O1 at 450733e).

  2. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced (both the body and the follow-up comment)

    Verdict. Established on current main. with_concurrency is documented as "Limit concurrent execution to at most n tasks at a time" and its body is let _ = n followed by tasks. Worse, as the follow-up says, calling it does not compile: the impl IntoIter[Task[T]] return type does not accept the Vec[Task[i32]] argument as written, and the diagnostic points into the embedded stdlib at task.w:232-233 before cascading to no matching generic overload for 'await_all' at the caller. So the limiter is both a no-op by construction and unusable in practice. The control without with_concurrency compiles and starts all tasks concurrently.

    Tested revision and compiler

    item value
    source main @ 5809ac1 (2026-09-14); lib/std/task.w unchanged since the reported 450733e
    compiler with build of 5809ac1 in a detached worktree, self-reports with v0.15.2.1
    platform macOS Darwin 25.6.0, arm64

    Repro

    use std.task.Task
    use std.task.await_all
    use std.task.with_concurrency
    extern fn with_fiber_live_fibers() -> i32
    async fn tick(): ()
    async fn work(value: i32) -> i32:
        print(f"start {value} live={with_fiber_live_fibers()}")
        tick().await
        tick().await
        value
    fn main:
        let tasks: Vec[Task[i32]] = Vec.new()
        for i in 0..4:
            tasks.push(work(i))
        let results = await_all(with_concurrency(tasks, 1))
        print(f"done {results.len()}")
    
    probe with check with run
    above error: return type mismatch --> <embedded-std>/std/task.w:232:5 (and :233:5), rc 1 no matching generic overload for 'await_all' at line 15, rc 1
    control: await_all(tasks) with no limiter ok start 3 live=4, start 2 live=5, start 1 live=6, start 0 live=7, done 4; all four fibers live at once

    Expected for the first: compiles, and with n = 1 the live= values show one worker at a time.

    Source

    lib/std/task.w:230-233:

    /// Limit concurrent execution to at most `n` tasks at a time.
    pub fn with_concurrency[T](tasks: impl IntoIter[Task[T]], n: i32) -> impl IntoIter[Task[T]]:
        let _ = n
        tasks
    

    The rest of the file fails loudly where unimplemented (todo() in await_first on empty input), so this is the one silent stub. The compile failure is a second defect layered on top: an impl IntoIter[...] parameter returned as an impl IntoIter[...] result does not type-check against a concrete Vec argument at the call site, and the error is attributed to the stdlib body rather than to the user's call.

    Spec / docs

    docs/with-migration-guide.md:2098-2103 advertises ids |> map(fetch_user) |> with_concurrency(10) |> await_all as the mapping for tokio's FuturesUnordered + bounded parallelism. The specification itself does not list with_concurrency in the §14.11 combinator table (line 7564), so the only normative-adjacent promise is the migration guide's, and it is false today. AGENTS.md §No Silent Fallbacks: a placeholder body that ignores its argument is the forbidden shape. The follow-up's point about #995 (the 1024 live-fiber cap) makes this the missing mitigation for a real limit, though I did not re-verify #995 here.

    Related PRs

    None since 450733e touch lib/std/task.w. No open PR does.

    Would I champion a fix? Yes.

    Acceptance criteria:

    1. Either implement the limiter (drive at most n tasks at a time, preserving input order in the result, joining or cancelling the rest on error the way await_all does) or replace the body with todo() and remove the migration-guide claim until it exists. A stub that silently returns its input is not acceptable under either.
    2. The repro above compiles; with n = 1 the live= trace never exceeds baseline plus one worker plus the awaiting main fiber; with n = 4 it matches the control.
    3. A behaviour test under test/spec next to spec_ss14_11_collection_await.w pins the concurrency bound using with_fiber_live_fibers().
    4. Separately, the "return type mismatch" attributed to <embedded-std> for a caller-side generic mismatch is a diagnostics defect worth its own issue: the user is told the stdlib is wrong.

    Untested

    Linux and Windows not executed; the code is platform-independent. The Result-returning await_all overload with a limiter was not probed since the non-fallible one already fails to compile.

  3. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced, as the review found: the limiter is a no-op and a call does not compile.

    $ cat main.w   # three tasks, with_concurrency(tasks, 1), await_all
    …
        let results = await_all(with_concurrency(tasks, 1))
    $ with run main.w
    error: no matching generic overload for 'await_all'
     --> main.w:14:19
    error: return type mismatch
     --> <embedded-std>/std/task.w:229:5
    229 |     let _ = n
    

    pre-release: a documented std function rejects its own use with a diagnostic inside the standard library; a loud refusal is a one-line fix and a real limiter is small.

  4. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Fix campaign 2026-10-25: flagged human attn needed. The signature cannot do what its doc says.

    What is proved. Calling an async function starts its task (the spawn happens at the call), so a Task[T] value is already running. with_concurrency(tasks: impl IntoIter[Task[T]], n) -> impl IntoIter[Task[T]] receives tasks and hands back tasks, and nothing it does can bound how many run. Today's body is let _ = n; tasks, and the call does not even compile (triage: "return type mismatch" inside std/task.w). Its own page, lib/std/async/with_concurrency.md, says "Current implementation is pass-through".

    A bound is possible only when the limiter pulls tasks from a lazy source one at a time and awaits as it goes. That means it must consume the iterator and await, returning results rather than tasks.

    Brief.

    1. Others. Rust futures: stream.buffer_unordered(n) / buffered(n) pull lazily from a stream of futures and yield outputs, at most n in flight. tokio: JoinSet plus a Semaphore. Go: a buffered channel of size n as a semaphore around go calls. Swift: withTaskGroup adding tasks as earlier ones finish. Each takes work not yet started.
    2. Spec. libstd-spec.md:79 lists with_concurrency under task. with-migration-guide.md:2098 shows ids |> map(fetch_user) |> with_concurrency(10) |> await_all. No semantics are specified beyond the name.
    3. Mission fit. A documented limit that silently does nothing is the forbidden silent fallback. The With-y spelling keeps the guide's pipeline: ids |> map(fetch_user) |> with_concurrency(10) gives the results in order, with at most 10 running. map is lazy, so a task starts only when the limiter pulls it.
    4. Prediction (75%). Eric rules async fn with_concurrency[T](tasks: impl IntoIter[Task[T]], n: i32) -> List[T]: it pulls lazily, keeps at most n live, and returns results in input order. The guide's |> await_all goes away.

    Narrowest question. Does with_concurrency return the results (consuming and awaiting, as above), or is it removed until a lazy task source is specified? Either way the current pass-through is wrong. Left on do-before-release.

  5. ehartford commented on Oct 27, 2026

    @ehartford
    Contributor

    Fixed on main in 89e6a7b (#2751), delegated as D164 on the 80% prediction from the second brief: with_concurrency now starts the work itself — with_concurrency[I, T](inputs: impl IntoIter[I], n: isize, f: fn(I) -> Task[T]) -> List[T], ids |> with_concurrency(10, fetch_user) — calling f(input) (which spawns) only when a slot frees, results in input order, n < 1 a loud refusal. The task-taking signature is deleted: a Task is already running (§24.1), and the lazy-source spelling could not type-check into it, so its documented form could not bound anything (measured peak 6 under limits 2 and 1). Test behav_981_with_concurrency_bounds_running_tasks: peak 2 under limit 2, peak 1 under limit 1, results in input order, debug-allocator leak 0.

    Ceremony found while writing the fixture (in the commit message, not fixed here): an unannotated list literal through an ordinary generic call binds its isize default before a later fn argument's i32 demand (map takes the demand; with_concurrency does not), so the fixture annotates List[i32].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions