From 359cebdb6cd05e1ca3bcf9e198f34799290bb2de Mon Sep 17 00:00:00 2001 From: Hisku Date: Wed, 1 Apr 2026 10:02:29 +0100 Subject: [PATCH 1/3] fix: prevent unhandled rejection cascade when ONNX model fails to load MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `void inFlight.finally(...)` discarded the promise returned by `.finally()`. When `inFlight` rejects (e.g. onnxruntime-node native binary fails to load), the `.finally()` promise also rejects with no handler — causing an unhandled rejection. The NestJS global handler wraps and re-emits it, creating a recursive cascade (~130 deep) on every request that triggers defender. The growing error strings consume memory, contributing to pod restarts. The actual error still propagates correctly through `await inFlight` → `loadModel()` catch → caller's try/catch. The `.catch(() => {})` only silences the duplicate rejection from the `.finally()` chain. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/classifiers/onnx-classifier.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classifiers/onnx-classifier.ts b/src/classifiers/onnx-classifier.ts index bfa4947..052abf5 100644 --- a/src/classifiers/onnx-classifier.ts +++ b/src/classifiers/onnx-classifier.ts @@ -187,7 +187,7 @@ export class OnnxClassifier { _sessionCache.set(modelPath, { session, OrtTensor, tokenizer }); })(); _loadingPromises.set(this.modelPath, inFlight); - void inFlight.finally(() => _loadingPromises.delete(this.modelPath)); + inFlight.finally(() => _loadingPromises.delete(this.modelPath)).catch(() => {}); } await inFlight; From 3e8e05389016d94410e989bee83aa7a98b7e1c0b Mon Sep 17 00:00:00 2001 From: Hisku Date: Wed, 1 Apr 2026 11:18:13 +0100 Subject: [PATCH 2/3] add inline comment for catch and test for unhandled rejection - Add comment explaining why .catch(() => {}) is needed - Add test that verifies no unhandledRejection is emitted when the ONNX model fails to load Co-Authored-By: Claude Opus 4.6 (1M context) --- specs/onnx-classifier.spec.ts | 25 +++++++++++++++++++++++++ src/classifiers/onnx-classifier.ts | 2 ++ 2 files changed, 27 insertions(+) diff --git a/specs/onnx-classifier.spec.ts b/specs/onnx-classifier.spec.ts index 4996c40..9ee8f11 100644 --- a/specs/onnx-classifier.spec.ts +++ b/specs/onnx-classifier.spec.ts @@ -79,6 +79,31 @@ describe.skipIf(!!process.env.CI)('OnnxClassifier', () => { }); }); +describe('#OnnxClassifier load failure', () => { + it('does not emit unhandledRejection when model fails to load', async () => { + // arrange + const unhandledRejections: unknown[] = []; + const handler = (reason: unknown) => unhandledRejections.push(reason); + process.on('unhandledRejection', handler); + + const badClassifier = new OnnxClassifier('/nonexistent/path/to/model'); + + // act + try { + await badClassifier.loadModel(); + } catch { + // Expected — model doesn't exist + } + + // Allow microtasks to flush (unhandled rejections are reported asynchronously) + await new Promise((r) => setTimeout(r, 100)); + + // assert + process.removeListener('unhandledRejection', handler); + expect(unhandledRejections).toHaveLength(0); + }); +}); + describe.skipIf(!!process.env.CI)('Tier2Classifier ONNX mode', () => { let classifier: Tier2Classifier; diff --git a/src/classifiers/onnx-classifier.ts b/src/classifiers/onnx-classifier.ts index 052abf5..ac451f9 100644 --- a/src/classifiers/onnx-classifier.ts +++ b/src/classifiers/onnx-classifier.ts @@ -187,6 +187,8 @@ export class OnnxClassifier { _sessionCache.set(modelPath, { session, OrtTensor, tokenizer }); })(); _loadingPromises.set(this.modelPath, inFlight); + // Swallow .finally() rejection — the actual error propagates via `await inFlight` below. + // Without this, a rejected inFlight produces an unhandled rejection from the .finally() chain. inFlight.finally(() => _loadingPromises.delete(this.modelPath)).catch(() => {}); } From f11b7858005e29a8f43eab85577f109437735993 Mon Sep 17 00:00:00 2001 From: Hisku Date: Wed, 1 Apr 2026 11:24:30 +0100 Subject: [PATCH 3/3] test: ensure unhandledRejection listener cleanup via try/finally Co-Authored-By: Claude Opus 4.6 (1M context) --- specs/onnx-classifier.spec.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/specs/onnx-classifier.spec.ts b/specs/onnx-classifier.spec.ts index 9ee8f11..a047a99 100644 --- a/specs/onnx-classifier.spec.ts +++ b/specs/onnx-classifier.spec.ts @@ -86,20 +86,23 @@ describe('#OnnxClassifier load failure', () => { const handler = (reason: unknown) => unhandledRejections.push(reason); process.on('unhandledRejection', handler); - const badClassifier = new OnnxClassifier('/nonexistent/path/to/model'); - - // act try { - await badClassifier.loadModel(); - } catch { - // Expected — model doesn't exist + const badClassifier = new OnnxClassifier('/nonexistent/path/to/model'); + + // act + try { + await badClassifier.loadModel(); + } catch { + // Expected — model doesn't exist + } + + // Allow microtasks to flush (unhandled rejections are reported asynchronously) + await new Promise((r) => setTimeout(r, 100)); + } finally { + process.removeListener('unhandledRejection', handler); } - // Allow microtasks to flush (unhandled rejections are reported asynchronously) - await new Promise((r) => setTimeout(r, 100)); - // assert - process.removeListener('unhandledRejection', handler); expect(unhandledRejections).toHaveLength(0); }); });