Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions specs/onnx-classifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion src/classifiers/onnx-classifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {});
Comment thread
hiskudin marked this conversation as resolved.
Comment thread
hiskudin marked this conversation as resolved.
Comment thread
hiskudin marked this conversation as resolved.
}

await inFlight;
Expand Down
Loading