You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(concurrency): reject synchronous worker errors in small batches - #49722
Two shared helpers run a list of async jobs a few at a time. If one job crashed straight away (before it even started its async work), the helper itself blew up instead of handing back a failed promise. Callers that wrote .catch(...) or await inside try never saw that error the normal way. Now the helper always hands back a failed promise, whether the batch is small or large.
What Changed
Before:mapWithConcurrency and forEachWithConcurrency have a fast path for batches no larger than the worker limit that calls items.map(fn) directly. If fn threw before returning a promise, the throw escaped the call, so the function threw instead of returning a rejected promise. The queued path (larger batches) already rejected.
After: the fast path goes through a small startWorker helper that calls fn(item, index) inside try and turns a synchronous throw into Promise.reject(error). Both paths now reject the same way. Result order, empty batches and how many workers run at once are unchanged.
Adds a regression test in src/shared/concurrency/map-with-concurrency.test.ts. It fails on main and passes with this change.
Why
These helpers are declared to return a Promise, and about 30 main and renderer callers depend on errors arriving through it. Whether a worker's error came back as a rejection or a throw depended on batch size, which is easy to miss and skips the callers' rejection handling. The original version wrapped each fast-path item in an async callback. startWorker gives the same result without allocating an extra promise per item when the worker succeeds.
I manually tested these changes locally (production-module probe above, macOS / Node 24)
Automated tests added/updated, or explained why not below
Every added test fails without this change and avoids the test anti-patterns
pnpm exec vitest run src/shared/concurrency/ passes (3 tests). With main's map-with-concurrency.ts restored, the new test fails with the synchronous throw. oxlint, oxfmt, anti-slop, code-quality native plugins, ast-grep and the changed-code quality gate are all clean on the touched files. Platform-neutral code, so no OS-specific paths. Tested on macOS.
Performance
None. Microbenchmark of the fast path (main vs this branch, three runs each) shows only noise: n=1 about 1.3 to 2.6 µs vs 1.6 to 2.2 µs, n=8 about 3.4 to 6.5 µs vs 3.8 to 5.1 µs. Microtask ticks to settle are the same (2 for n=1, 4 and 8). The only added work is a try frame per item. There is no extra promise when the worker succeeds, and no new I/O, polling or subprocesses.
Risk
Level: Low. The only behavior change is that a worker which throws synchronously in a small batch now yields a rejected promise instead of a thrown exception, which matches the queued path and the declared return type. One side effect: after one item throws, the remaining fast-path items still start, the same as a normal Promise.all over async workers. Backing this out is a plain revert.
Notes
Original fix by @Afloat16 in #49722. Their authorship is kept through a Co-authored-by trailer.
No actionable comments were generated in the recent review.
Walkthrough
The fast paths in mapWithConcurrency and forEachWithConcurrency now invoke workers through startWorker. Synchronous worker errors become rejected promises. A test checks this behavior for both helpers with one item and concurrency 2.
Severity of issue fixed: Medium
Merge Risk: Minimal · up to 7aca4
Synchronous worker errors in small batches now reject the returned promise instead of escaping the call. No in-repository caller was found that relies on the old behavior, so the merge risk is minimal.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: synchronous worker errors in small batches now reject the returned promise in mapWithConcurrency and forEachWithConcurrency. It follows the conventional-commit style a…
Description check
The description follows the repository template and covers ELI5, What Changed, Why, Linked Issue (Fixes #49686), Visual Proof (N/A with reason), Testing, Performance, Risk with level Low, AI Disclosur…
Linked Issues check
Issue #49686 requires that a synchronous worker throw in the small-batch fast path reject the returned promise. At head 7aca4ba, the whole-PR diff against merge base ab6cd7f shows both fast paths now …
Out of Scope Changes check
The whole-PR diff changes two files. map-with-concurrency.ts changes only the two fast-path invocations named in #49686 and adds the private startWorker helper used only by those invocations. `map…
Full details: Title check
Explanation
The title names the main change: synchronous worker errors in small batches now reject the returned promise in mapWithConcurrency and forEachWithConcurrency. It follows the conventional-commit style and matches the summary, the objectives, and linked issue #49686. It is specific and not misleading.
Full details: Description check
Explanation
The description follows the repository template and covers ELI5, What Changed, Why, Linked Issue (Fixes #49686), Visual Proof (N/A with reason), Testing, Performance, Risk with level Low, AI Disclosure, Review, Agent skill boundary, and Notes. The change is clearly explained, and the before and after behavior is stated. Some template items are incomplete: the Testing section has unchecked boxes for full upstream CI and full repository checks, and the test-anti-pattern check is not confirmed. The Testing section also cites a CI run and a commit that are not verifiable from the diff. Overall the description is mostly complete.
Resolution
Optionally tick or explain the unchecked items: the full upstream CI gate and the Testing checkbox confirming the added test fails without the change. Also note the mechanism mismatch: the description says an async wrapper is added per fast-path item, while the commit message and the file summary describe a startWorker helper that converts synchronous throws to rejections.
Full details: Linked Issues check
Explanation
Issue #49686 requires that a synchronous worker throw in the small-batch fast path reject the returned promise. At head 7aca4ba, the whole-PR diff against merge base ab6cd7f shows both fast paths now call startWorker(fn, item, index) inside items.map. startWorker wraps fn(item, index) in try/catch and returns Promise.reject(error) on a synchronous throw, so Promise.all rejects with the worker error. Both mapWithConcurrency and forEachWithConcurrency still return Promise.all(...), so the public promise owns the failure. Empty batches still resolve through Promise.all([]). Result order is unchanged because items.map keeps index order. The queued path is not modified. The added test in map-with-concurrency.test.ts asserts rejection for a one-item, concurrency-2 batch for both APIs. The test was not run in this review, so its pass result is not independently confirmed; the behavior is verified by source inspection. The incremental diff from the previous reviewed head 9dc199d could not be read, so the change from the earlier async-wrapper approach to the startWorker helper is assessed from the whole-PR diff only. The PR description still describes an async wrapper and one wrapper per item, which no longer matches the code. This is a description accuracy issue, not a requirement gap.
Full details: Out of Scope Changes check
Explanation
The whole-PR diff changes two files. map-with-concurrency.ts changes only the two fast-path invocations named in #49686 and adds the private startWorker helper used only by those invocations. map-with-concurrency.test.ts is a new regression test for the same behavior. No exported signatures or queued-path code change. The fast path no longer forwards the third array argument that Array.prototype.map passed to fn. The declared fn type accepts only (item, index), so this is not a concrete scope problem. The PR description's process notes (check runs, formatting, AI disclosure) describe no code changes.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: ea9aa490-4529-4ca4-bd79-31823faf53e2
Commits
Reviewing files that changed from the base of the PR and between 9dc199d and 7aca4ba.
Files selected for processing: 1
src/shared/concurrency/map-with-concurrency.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.
mapWithConcurrency and forEachWithConcurrency took a fast path for
batches no larger than the worker limit that called the worker directly
inside items.map. A worker that threw before returning a promise made
the call itself throw instead of returning a rejected promise, unlike
the queued path. Route the fast path through a small startWorker helper
that converts a synchronous throw into Promise.reject, without adding a
wrapper promise per item.
Fixesstablyai#49686
Co-authored-by: Afloat <ShuffleMonkey@163.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ELI5
Two shared helpers run a list of async jobs a few at a time. If one job crashed straight away (before it even started its async work), the helper itself blew up instead of handing back a failed promise. Callers that wrote
.catch(...)orawaitinsidetrynever saw that error the normal way. Now the helper always hands back a failed promise, whether the batch is small or large.What Changed
mapWithConcurrencyandforEachWithConcurrencyhave a fast path for batches no larger than the worker limit that callsitems.map(fn)directly. Iffnthrew before returning a promise, the throw escaped the call, so the function threw instead of returning a rejected promise. The queued path (larger batches) already rejected.startWorkerhelper that callsfn(item, index)insidetryand turns a synchronous throw intoPromise.reject(error). Both paths now reject the same way. Result order, empty batches and how many workers run at once are unchanged.src/shared/concurrency/map-with-concurrency.test.ts. It fails onmainand passes with this change.Why
These helpers are declared to return a
Promise, and about 30 main and renderer callers depend on errors arriving through it. Whether a worker's error came back as a rejection or a throw depended on batch size, which is easy to miss and skips the callers' rejection handling. The original version wrapped each fast-path item in anasynccallback.startWorkergives the same result without allocating an extra promise per item when the worker succeeds.Linked Issue
Fixes #49686
Visual Proof
N/A. This is shared promise plumbing with no UI or visible interaction change. Behavior probe through the production module (main vs this branch):
Testing
pnpm exec vitest run src/shared/concurrency/passes (3 tests). Withmain'smap-with-concurrency.tsrestored, the new test fails with the synchronous throw. oxlint, oxfmt, anti-slop, code-quality native plugins, ast-grep and the changed-code quality gate are all clean on the touched files. Platform-neutral code, so no OS-specific paths. Tested on macOS.Performance
None. Microbenchmark of the fast path (main vs this branch, three runs each) shows only noise: n=1 about 1.3 to 2.6 µs vs 1.6 to 2.2 µs, n=8 about 3.4 to 6.5 µs vs 3.8 to 5.1 µs. Microtask ticks to settle are the same (2 for n=1, 4 and 8). The only added work is a
tryframe per item. There is no extra promise when the worker succeeds, and no new I/O, polling or subprocesses.Risk
Level: Low. The only behavior change is that a worker which throws synchronously in a small batch now yields a rejected promise instead of a thrown exception, which matches the queued path and the declared return type. One side effect: after one item throws, the remaining fast-path items still start, the same as a normal
Promise.allover async workers. Backing this out is a plain revert.Notes
Original fix by @Afloat16 in #49722. Their authorship is kept through a
Co-authored-bytrailer.