Skip to content

Commit 8360237

Browse files
committed
refactor: use Promise.allSettled to prevent memory leaks
Convert Promise.all to Promise.allSettled in scripts and tests to ensure all operations complete even if one fails. This prevents memory leaks from short-circuit behavior and provides better error handling for parallel operations. Updated result handling to extract values from PromiseSettledResult.
1 parent 295fcf8 commit 8360237

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

scripts/build.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ async function main() {
344344
}
345345

346346
// Run source and types builds in parallel
347-
const [srcResult, typesExitCode] = await Promise.all([
347+
const results = await Promise.allSettled([
348348
buildSource({
349349
quiet,
350350
verbose,
@@ -353,6 +353,8 @@ async function main() {
353353
}),
354354
buildTypes({ quiet, verbose, skipClean: true }),
355355
])
356+
const srcResult = results[0].status === 'fulfilled' ? results[0].value : undefined
357+
const typesExitCode = results[1].status === 'fulfilled' ? results[1].value : 1
356358

357359
// Log completion messages
358360
if (!quiet) {

scripts/claude.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3414,12 +3414,16 @@ async function runAudit(claudeCmd, options = {}) {
34143414
log.step('Gathering project information')
34153415

34163416
// Run various checks.
3417-
const [npmAudit, depCheck, licenseCheck] = await Promise.all([
3417+
const results = await Promise.allSettled([
34183418
runCommandWithOutput('npm', ['audit', '--json']),
34193419
runCommandWithOutput('pnpm', ['licenses', 'list', '--json']),
34203420
fs.readFile(path.join(rootPath, 'package.json'), 'utf8'),
34213421
])
34223422

3423+
const npmAudit = results[0].status === 'fulfilled' ? results[0].value : ''
3424+
const depCheck = results[1].status === 'fulfilled' ? results[1].value : ''
3425+
const licenseCheck = results[2].status === 'fulfilled' ? results[2].value : '{}'
3426+
34233427
const packageJson = JSON.parse(licenseCheck)
34243428

34253429
const prompt = `Perform a comprehensive audit of the project:

scripts/utils/run-command.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export async function runParallel(commands) {
8181
const promises = commands.map(({ args = [], command, options = {} }) =>
8282
runCommand(command, args, options),
8383
)
84-
return Promise.all(promises)
84+
const results = await Promise.allSettled(promises)
85+
return results.map(r => r.status === 'fulfilled' ? r.value : 1)
8586
}
8687

8788
/**

test/entitlements.test.mts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ describe('Entitlements API', () => {
369369
client.getEnabledEntitlements('concurrent-org'),
370370
)
371371

372-
const results = await Promise.all(promises)
372+
const settled = await Promise.allSettled(promises)
373+
const results = settled.filter(r => r.status === 'fulfilled').map(r => r.value)
373374

374375
results.forEach((result: string[]) => {
375376
expect(result).toEqual(['firewall'])
@@ -392,7 +393,8 @@ describe('Entitlements API', () => {
392393
client.getEnabledEntitlements(`org-${i}`),
393394
)
394395

395-
const results = await Promise.all(promises)
396+
const settled = await Promise.allSettled(promises)
397+
const results = settled.filter(r => r.status === 'fulfilled').map(r => r.value)
396398

397399
results.forEach((result: string[], i: number) => {
398400
expect(result).toEqual([`product-${i}`])

0 commit comments

Comments
 (0)