diff --git a/specs/onnx-classifier.spec.ts b/specs/onnx-classifier.spec.ts index 4996c40..a047a99 100644 --- a/specs/onnx-classifier.spec.ts +++ b/specs/onnx-classifier.spec.ts @@ -79,6 +79,34 @@ 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); + + try { + 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); + } + + // assert + 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 bfa4947..ac451f9 100644 --- a/src/classifiers/onnx-classifier.ts +++ b/src/classifiers/onnx-classifier.ts @@ -187,7 +187,9 @@ export class OnnxClassifier { _sessionCache.set(modelPath, { session, OrtTensor, tokenizer }); })(); _loadingPromises.set(this.modelPath, inFlight); - void inFlight.finally(() => _loadingPromises.delete(this.modelPath)); + // 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(() => {}); } await inFlight;