bounded-concurrency, and std mapLimit: a few tasks at a time #14
Loading…
Reference in a new issue
No description provided.
Delete branch "rules/bounded-concurrency"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
New rule
bounded-concurrency(warn inrecommended.json). It reportsPromise.all(...)orPromise.allSettled(...)when the argument is directlylist.map(cb)orArray.from(list, cb)and:constbound to one, or a binding or parameter with a tuple type ([A, B],readonly [A, B]); those are left alone.new,await).jobs.map((job) => job.done)only picks promises that already exist and is not reported.mapstarts every task beforePromise.allsees one, so a list of 10,000 items means 10,000 requests, open files or queries at once. That leads toEMFILE, rate limits, or the pool queueing everything anyway.There is no quick fix.
mapLimitresolves to aResult, so swapping it in changes the type at the use site, and a mechanical rewrite would leave the code broken.New std helper
mapLimit(items, limit, fn, signal?)in@evobug/safe-ts-std/async. Its error handling follows the other helpers in async.ts: it never rejects.U[]in the order ofitems, or toErrwith the first failure (a rejection, or a synchronous throw fromfn).fn(item, index, signal)gets a signal. After the first failure, or when the caller'ssignalaborts, no new item starts and that signal aborts. TheErrthen holds the failure or the caller's abort reason.limitthat isn't a positive integer resolves toErr(RangeError).allSettled-style "one outcome per item", the docs show(item) => unsafeAsync(() => work(item)). I didn't add a second helper for that.Analysis change (analyze.rs): a
mapLimitimported from@evobug/safe-ts-std/asyncis treated likeunsafeAsync. Its callback runs right away (async allowed), and whatever the callback throws or rejects with counts as caught. Without this,mapLimit(files, 4, (f) => upload(f))with a propagatinguploadwas reported asuncaught-propagation, since any callback to an unknown function counts as detached. Only function arguments are guarded, so a throwing expression initemsis still reported. A discardedawait mapLimit(...)still getsmust-use-result; I checked that manually inside the demo project.Examples that are now reported, rightly:
docs/guide/async.md"uploadTogether" didPromise.all(files.map(...)). It now usesmapLimit(files, 4, ...), and the prose says that this version returns anErrrather than propagating. No other example changed:examples/demo,examples/vue, the docs Vue demo components and the playground presets report exactly what they did before (diffed per file and location).Docs: rule page
docs/rules/bounded-concurrency.md, amapLimitsection in the std-helpers guide, both README tables,docs/README.md, the sidebar (Failures group) and the oxlint plugin.Tests
fixtures/bounded_concurrency/main.ts: 7 reported cases and 6 accepted ones (literal, const literal, tuple, existing promises,Promise.all([a, b]),Promise.race).fixtures/propagation_async:mapLimitcallbacks (expression and async bodies) recordcaught: Gone.packages/std/test/async.test.ts: 8 newmapLimittests. They cover order and the concurrency bound, the empty list, fail-fast (nothing new started, running signal aborted with the reason), a synchronous throw, the caller's signal before and during the run, listener removal, bad limits, and per-item Results viaunsafeAsync.mainat1fe2d1d(headc41eeaf), which includes #5 (call-following), #11 (rejections vs throws, narrowing catches) and #6 (fetch-signal). The textual conflicts were only in the rule lists, README tables and sidebar:bounded-concurrencygoes right afterfetch-signalin each, andanalyze.rsrunsmegamorphic,fetch_signal, thenconcurrency. Three things had to change for main:ns.fn()through namespace imports, soimport * as asyncStd from "@evobug/safe-ts-std/async"; asyncStd.mapLimit(ids, 4, (id) => load(id))reported the callback'sPropagateErrorasuncaught-propagation.is_map_limitnow recognises the namespace form too, andfixtures/propagation_asynchas a case for it. I also checked by hand that what #5 newly follows (methods such asstore.get(id)andapi.find(id), namespace-imported functions, nested helpers) is recorded as caught inside amapLimitcallback, the same as insideunsafeAsync.promisesfixture doesawait Promise.all(ids.map((id) => viaReturn(id)))over areadonly string[].bounded-concurrencyreports it, rightly, so that line now expects it. Under #11's analysis, amapLimitcallback that returns a rejecting promise from a non-async function, or throws before making its promise, is recorded as caught (checked by hand).scripts/verify-packages.mjsnow importsmapLimitfrom the published@evobug/safe-ts-std/asyncand runs it../asyncwas already in std's exports, so nothing else needed wiring.cargo nextest run --workspace: 104 passed, 0 failed (the earlierfeaturesfailure was fixed on main by #23).npm run test:std: 138 passed, 0 failed.npm run test:vite,npm run typecheck,npm run test:docs: passed.node scripts/verify-packages.mjs: all package checks passed (Linux x64 addon only).safe-ts checkonexamples/demo(22 problems, the demo's intended ones) andexamples/vue(none): nobounded-concurrencyreports.Left out / uncertain
mapLimit.promises.push(f(x))),settleAll(items.map(...)),Promise.race/any, and.mapchains with something in between (items.filter(...).map(...)is checked, butitems.map(...).filter(...)is not).mapLimitre-exported through a project's own barrel file is not recognised (only a direct import or a namespace import of@evobug/safe-ts-std/async), so its callbacks are still treated as detached.🤖 Generated with Claude Code
0989be65catoa55de265dea55de265dec41eeaf051