From 0da1107858493b7ccf320e9b3c1d79e627adc998 Mon Sep 17 00:00:00 2001 From: Jonny Burger Date: Mon, 15 Jun 2026 16:25:02 +0200 Subject: [PATCH] [AI-generated] Fix unhandled rejection when disposing invalid input (#413) * Fix input dispose rejection handling * Move input disposal regression test --- src/input.ts | 8 ++++++-- test/node/input.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test/node/input.test.ts diff --git a/src/input.ts b/src/input.ts index 747d3f4..211478c 100644 --- a/src/input.ts +++ b/src/input.ts @@ -527,8 +527,12 @@ export class Input extends EventEmitter } this._sourceRefs.length = 0; - void this._demuxerPromise - ?.then(demuxer => demuxer.dispose()); + if (this._demuxerPromise) { + // The demuxer promise may already be rejected after failed format detection. + void this._demuxerPromise + .then(demuxer => demuxer.dispose()) + .catch(() => {}); + } } /** diff --git a/test/node/input.test.ts b/test/node/input.test.ts new file mode 100644 index 0000000..5e923af --- /dev/null +++ b/test/node/input.test.ts @@ -0,0 +1,28 @@ +import { expect, test } from 'vitest'; +import { Input, UnsupportedInputFormatError } from '../../src/input.js'; +import { ALL_FORMATS } from '../../src/input-format.js'; +import { BufferSource } from '../../src/source.js'; + +test('Disposing after failed format detection does not emit an unhandled rejection', async () => { + const input = new Input({ + source: new BufferSource(new Uint8Array([1, 2, 3, 4])), + formats: ALL_FORMATS, + }); + + await expect(input.getFormat()).rejects.toThrow(UnsupportedInputFormatError); + + const unhandledRejections: unknown[] = []; + const onUnhandledRejection = (reason: unknown) => { + unhandledRejections.push(reason); + }; + + process.on('unhandledRejection', onUnhandledRejection); + try { + input.dispose(); + await new Promise(resolve => setTimeout(resolve, 0)); + } finally { + process.off('unhandledRejection', onUnhandledRejection); + } + + expect(unhandledRejections).toEqual([]); +});