Fix Webpack erroring with worker_threads import, unref Worker so process can terminate

This commit is contained in:
Vanilagy
2026-02-17 12:34:45 +01:00
parent 4928779ba1
commit 317193452c
4 changed files with 56 additions and 10 deletions
+5 -2
View File
@@ -13,7 +13,7 @@ import {
EncodedPacket,
registerDecoder,
} from 'mediabunny';
import { sendCommand } from './worker-client';
import { sendCommand, refWorker, unrefWorker } from './worker-client';
class Ac3Decoder extends CustomAudioDecoder {
private ctx = 0;
@@ -23,6 +23,8 @@ class Ac3Decoder extends CustomAudioDecoder {
}
async init() {
await refWorker();
const result = await sendCommand({
type: 'init-decoder',
data: { codec: this.codec },
@@ -53,8 +55,9 @@ class Ac3Decoder extends CustomAudioDecoder {
await sendCommand({ type: 'flush-decoder', data: { ctx: this.ctx } });
}
close() {
async close() {
void sendCommand({ type: 'close-decoder', data: { ctx: this.ctx } });
await unrefWorker();
}
}
+7 -3
View File
@@ -13,7 +13,7 @@ import {
EncodedPacket,
registerEncoder,
} from 'mediabunny';
import { sendCommand } from './worker-client';
import { sendCommand, refWorker, unrefWorker } from './worker-client';
import { assert } from './shared';
import { AC3_SAMPLE_RATES, EAC3_REDUCED_SAMPLE_RATES } from '../../../shared/ac3-misc';
@@ -38,11 +38,14 @@ class Ac3Encoder extends CustomAudioEncoder {
return (codec === 'ac3' || codec === 'eac3')
&& config.numberOfChannels >= 1
&& config.numberOfChannels <= 8
&& sampleRates.includes(config.sampleRate);
&& sampleRates.includes(config.sampleRate)
&& config.bitrate !== undefined;
}
async init() {
assert(this.config.bitrate);
await refWorker();
assert(this.config.bitrate !== undefined);
this.sampleRate = this.config.sampleRate;
this.numberOfChannels = this.config.numberOfChannels;
@@ -130,6 +133,7 @@ class Ac3Encoder extends CustomAudioEncoder {
close() {
void sendCommand({ type: 'close-encoder', data: { ctx: this.ctx } });
void unrefWorker();
}
private async encodeOneFrame() {
+41 -2
View File
@@ -10,13 +10,51 @@ import { assert, type WorkerCommand, type WorkerResponse, type WorkerResponseDat
// @ts-expect-error An esbuild plugin handles this, TypeScript doesn't need to understand
import createWorker from './codec.worker';
let workerPromise: Promise<Worker> | null;
type ExtendedWorker = Worker & {
ref?: () => void;
unref?: () => void;
};
let workerPromise: Promise<ExtendedWorker> | null;
let nextMessageId = 0;
const pendingMessages = new Map<number, {
resolve: (value: WorkerResponseData) => void;
reject: (reason?: unknown) => void;
}>();
let refCount = 0;
let keepAliveInterval: ReturnType<typeof setInterval> | null = null;
export const refWorker = async () => {
refCount++;
if (refCount === 1) {
keepAliveInterval = setInterval(() => {}, 2 ** 31 - 1);
const worker = await ensureWorker();
worker.ref?.();
}
};
export const unrefWorker = async () => {
refCount--;
if (refCount === 0) {
if (keepAliveInterval !== null) {
clearInterval(keepAliveInterval);
keepAliveInterval = null;
}
const worker = await workerPromise;
if (worker) {
if (worker.unref) {
worker.unref(); // If we don't do this, then the Node process never terminates by itself
} else if (typeof window === 'undefined') {
// Non-browser environment without unref - terminate instead
worker.terminate();
workerPromise = null;
}
}
}
};
export const sendCommand = async <T extends string>(
command: WorkerCommand & { type: T },
transferables?: Transferable[],
@@ -41,7 +79,8 @@ export const sendCommand = async <T extends string>(
const ensureWorker = () => {
return workerPromise ??= (async () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const worker = (await createWorker()) as Worker;
const worker = (await createWorker()) as ExtendedWorker;
worker.unref?.(); // Start unreffed
const onMessage = (data: WorkerResponse) => {
const pending = pendingMessages.get(data.id);