]> Piment Noir Git Repositories - poolifier.git/commitdiff
refactor(pools): isolate batch abort signal validation (#3434)
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 28 Nov 2026 16:23:50 +0000 (17:23 +0100)
committerGitHub <noreply@github.com>
Sat, 28 Nov 2026 16:23:50 +0000 (17:23 +0100)
* refactor(pools): isolate batch abort signal validation

* fix(pools): keep batch signal validation module-local

* refactor(pools): move batch validation to utilities

* refactor(pools): type unvalidated batch signals

* refactor(pools): accept unvalidated signal elements

* refactor(pools): clarify batch input normalization

src/pools/abstract-pool.ts
src/pools/utils.ts
tests/pools/task-lifecycle.test.mjs

index 3af12ab2d5474e2fe9d91f42a3d1d2c4d6562db5..bca1cb5b35cd7c394affaef08787e2817672669e 100644 (file)
@@ -64,6 +64,7 @@ import {
   checkValidWorkerChoiceStrategy,
   checkValidWorkerNodeKeys,
   getDefaultTasksQueueOptions,
+  normalizeMapExecuteAbortSignals,
   selectStealSourceWorkerNode,
   updateEluWorkerUsage,
   updateRunTimeWorkerUsage,
@@ -801,29 +802,13 @@ export abstract class AbstractPool<
     if (name != null && typeof name === 'string' && name.trim().length === 0) {
       throw new TypeError('name argument must not be an empty string')
     }
-    if (!Array.isArray(data)) {
-      data = [...data]
-    }
-    if (abortSignals != null) {
-      if (typeof abortSignals[Symbol.iterator] !== 'function') {
-        throw new TypeError('abortSignals argument must be an iterable')
-      }
-      if (!Array.isArray(abortSignals)) {
-        abortSignals = [...abortSignals]
-      }
-      for (const abortSignal of abortSignals) {
-        if (!(abortSignal instanceof AbortSignal)) {
-          throw new TypeError(
-            'abortSignals argument must be an iterable of AbortSignal'
-          )
-        }
-      }
-      if ((data as Data[]).length !== (abortSignals as AbortSignal[]).length) {
-        throw new Error(
-          'data and abortSignals arguments must have the same length'
-        )
-      }
-    }
+    const normalizedData: readonly Data[] = Array.isArray(data)
+      ? data
+      : [...data]
+    const normalizedAbortSignals =
+      abortSignals == null
+        ? undefined
+        : normalizeMapExecuteAbortSignals(abortSignals, normalizedData)
     if (transferList != null && !Array.isArray(transferList)) {
       throw new TypeError('transferList argument must be an array')
     }
@@ -832,11 +817,8 @@ export abstract class AbstractPool<
         ? Array.from(transferList)
         : undefined
     const tasks: [Data, AbortSignal | undefined][] = Array.from(
-      { length: (data as Data[]).length },
-      (_, i) => [
-        (data as Data[])[i],
-        abortSignals != null ? (abortSignals as AbortSignal[])[i] : undefined,
-      ]
+      { length: normalizedData.length },
+      (_, i) => [normalizedData[i], normalizedAbortSignals?.[i]]
     )
     return await Promise.all(
       tasks.map(([data, abortSignal]) =>
index 08beb6d8f3dc2307d6d5a9c04c98919ebe2390a9..467d4b18163448e03d95f11eaccc21f5c5eb8101 100644 (file)
@@ -489,6 +489,29 @@ export const getDefaultTasksQueueOptions = (
   })
 }
 
+export const normalizeMapExecuteAbortSignals = (
+  abortSignals: Iterable<unknown>,
+  data: readonly unknown[]
+): readonly AbortSignal[] => {
+  if (typeof abortSignals[Symbol.iterator] !== 'function') {
+    throw new TypeError('abortSignals argument must be an iterable')
+  }
+  const signals: readonly unknown[] = Array.isArray(abortSignals)
+    ? abortSignals
+    : [...abortSignals]
+  for (const signal of signals) {
+    if (!(signal instanceof AbortSignal)) {
+      throw new TypeError(
+        'abortSignals argument must be an iterable of AbortSignal'
+      )
+    }
+  }
+  if (data.length !== signals.length) {
+    throw new Error('data and abortSignals arguments must have the same length')
+  }
+  return signals as readonly AbortSignal[]
+}
+
 export const checkFilePath = (filePath: string | undefined): void => {
   if (filePath == null) {
     throw new TypeError('The worker file path must be defined')
index 5292a781a4477621439df9b719aa918999b892ca..0494054190d19025b80706b8540d8b6a77bdccab 100644 (file)
@@ -656,6 +656,24 @@ describe('Task lifecycle regressions', () => {
     }
   })
 
+  it('honors batch cancellation even when a subclass uses the helper name', async () => {
+    class CustomPool extends FixedThreadPool {
+      normalizeMapExecuteAbortSignals () {
+        return []
+      }
+    }
+    const pool = new CustomPool(1, workerFile)
+    const reason = new Error('Cancelled before batch submission')
+    try {
+      await ready(pool)
+      await expect(
+        pool.mapExecute([{}], 'echo', [AbortSignal.abort(reason)])
+      ).rejects.toBe(reason)
+    } finally {
+      await bounded(pool.destroy())
+    }
+  })
+
   it('consumes a one-shot abort signal iterable exactly once', async () => {
     const pool = new FixedThreadPool(1, workerFile)
     try {
@@ -690,6 +708,22 @@ describe('Task lifecycle regressions', () => {
     }
   })
 
+  it('rejects an invalid batch signal before checking the batch length', async () => {
+    const pool = new FixedThreadPool(1, workerFile)
+    try {
+      await ready(pool)
+      const { error } = await settle(
+        pool.mapExecute([{}], 'echo', [new AbortController().signal, undefined])
+      )
+      expect(error).toBeInstanceOf(TypeError)
+      expect(error.message).toBe(
+        'abortSignals argument must be an iterable of AbortSignal'
+      )
+    } finally {
+      await bounded(pool.destroy())
+    }
+  })
+
   it.each([false, true])(
     'settles promise-returning functions (abortable: %s)',
     async abortable => {