Skip to content

Commit eaaa2ae

Browse files
committed
refactor(scan): use Promise.allSettled to prevent memory leaks
Switch from Promise.all to Promise.allSettled in fetch-report-data to prevent memory leaks from short-circuit behavior. If one promise fails unexpectedly (e.g., bug in catch handler), Promise.all would immediately reject while the other promise continues running in background, holding resources and memory. Promise.allSettled ensures both API calls complete before proceeding, properly cleaning up all resources regardless of success/failure.
1 parent db85ef2 commit eaaa2ae

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

packages/cli/src/commands/scan/fetch-report-data.mts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ export async function fetchScanData(
128128

129129
updateProgress()
130130

131-
const [scan, securityPolicy]: [
132-
CResult<SocketArtifact[]>,
133-
CResult<SocketSdkSuccessResult<'getOrgSecurityPolicy'>['data']>,
134-
] = await Promise.all([
131+
const results = await Promise.allSettled([
135132
fetchScanResult().catch(e => {
136133
updateScan('failure; unknown blocking error occurred')
137134
return {
@@ -157,6 +154,22 @@ export async function fetchScanData(
157154
updateProgress()
158155
})
159156

157+
const scan: CResult<SocketArtifact[]> = results[0].status === 'fulfilled'
158+
? results[0].value
159+
: {
160+
ok: false as const,
161+
message: 'Unexpected error',
162+
cause: 'Promise rejected unexpectedly',
163+
}
164+
165+
const securityPolicy: CResult<SocketSdkSuccessResult<'getOrgSecurityPolicy'>['data']> = results[1].status === 'fulfilled'
166+
? results[1].value
167+
: {
168+
ok: false as const,
169+
message: 'Unexpected error',
170+
cause: 'Promise rejected unexpectedly',
171+
}
172+
160173
if (!scan.ok) {
161174
return scan
162175
}

0 commit comments

Comments
 (0)