Skip to content

Commit c381fcb

Browse files
committed
Adjust abortSignal max listener count in batchScan
1 parent ff2deb5 commit c381fcb

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/utils/alert/artifact.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const {
9090
async function* createBatchGenerator(
9191
chunk: string[]
9292
): AsyncGenerator<SocketArtifact> {
93+
// Adds the first 'abort' listener to abortSignal.
9394
const req = https
9495
// Lazily access constants.BATCH_PURL_ENDPOINT.
9596
.request(constants.BATCH_PURL_ENDPOINT, {
@@ -104,6 +105,7 @@ async function* createBatchGenerator(
104105
components: chunk.map(id => ({ purl: `pkg:npm/${id}` }))
105106
})
106107
)
108+
// Adds the second 'abort' listener to abortSignal.
107109
const { 0: res } = <[IncomingMessage]>(
108110
await events.once(req, 'response', { signal: abortSignal })
109111
)
@@ -135,6 +137,16 @@ export async function* batchScan(
135137
}
136138
type ResolveFn = (value: GeneratorStep) => void
137139

140+
// The createBatchGenerator method will add 2 'abort' event listeners to
141+
// abortSignal so we multiply the concurrencyLimit by 2.
142+
const neededMaxListeners = concurrencyLimit * 2
143+
// Increase abortSignal max listeners count to avoid Node's MaxListenersExceededWarning.
144+
const oldAbortSignalMaxListeners = events.getMaxListeners(abortSignal)
145+
let abortSignalMaxListeners = oldAbortSignalMaxListeners
146+
if (oldAbortSignalMaxListeners < neededMaxListeners) {
147+
abortSignalMaxListeners = oldAbortSignalMaxListeners + neededMaxListeners
148+
events.setMaxListeners(abortSignalMaxListeners, abortSignal)
149+
}
138150
const { length: pkgIdsCount } = pkgIds
139151
const running: GeneratorEntry[] = []
140152
let index = 0
@@ -181,6 +193,10 @@ export async function* batchScan(
181193
continueGen(generator)
182194
}
183195
}
196+
// Reset abortSignal max listeners count.
197+
if (abortSignalMaxListeners > oldAbortSignalMaxListeners) {
198+
events.setMaxListeners(oldAbortSignalMaxListeners, abortSignal)
199+
}
184200
}
185201

186202
export function isArtifactAlertCveFixable(

0 commit comments

Comments
 (0)