fix(subsync): address CodeRabbit review of cancellation, cleanup, and writes

- kill a tool's whole POSIX process group on cancel (orphaned children held the
  pipes open, so the run never finished); kill running groups on exit
- refuse an in-place rewrite when the subtitle changed on disk mid-sync
- pick only a freshly added track for the secondary slot, and reset its delay
  only after selecting it
- pass cancellation to the video-reference alass and ffsubsync runs, and never
  open a picker for media the user already left
- treat a failed media-path baseline read as unknown instead of null
- align worker: honor aborts during startup and finish queued calls on the
  main thread when the worker fails to start
- temp dirs: keep failed removals registered, never sweep a dir mpv still
  plays from, and clean up when writing a cleaned reference fails
- an empty cached audio result means no usable audio, as on a fresh decode
- compare external track paths case-sensitively on POSIX
- decode ffmpeg stderr as a stream; coverage lane for subtitle tests
- make the cancellation test wait for the tool to start
This commit is contained in:
sudacode committed 2026-11-28 03:53:12 -08:00
1 parent 5b39e850f6
commit cde9362ee5
12 files changed
+244 -57

No files matched your search

+34 -3
View File
@@ -64,7 +64,12 @@ function resolveAlignWorkerPath(): string | null {
return fs.existsSync(workerPath) ? workerPath : null;
}
type Pending = { resolve: (value: unknown) => void; reject: (reason: Error) => void };
type Pending = {
kind: AlignTaskKind;
args: unknown[];
resolve: (value: unknown) => void;
reject: (reason: Error) => void;
};
class AlignWorkerRuntime {
private worker: Worker | null = null;
@@ -80,12 +85,13 @@ class AlignWorkerRuntime {
): Promise<AlignTaskResult<K>> {
signal?.throwIfAborted();
const worker = await this.getWorker();
signal?.throwIfAborted();
if (!worker) return runAlignTask(kind, args) as AlignTaskResult<K>;
const id = this.nextId++;
this.clearIdleTimer();
const result = new Promise<unknown>((resolve, reject) => {
this.pending.set(id, { resolve, reject });
this.pending.set(id, { kind, args, resolve, reject });
});
const onAbort = (): void => this.reset(signal?.reason ?? new Error('aborted'));
signal?.addEventListener('abort', onAbort, { once: true });
@@ -110,10 +116,20 @@ class AlignWorkerRuntime {
try {
const { Worker: WorkerCtor } = await import('node:worker_threads');
const worker = new WorkerCtor(workerPath);
let online = false;
worker.unref();
worker.once('online', () => {
online = true;
});
worker.on('message', (message: AlignWorkerResponse) => this.handleMessage(message));
worker.on('error', (error: Error) => {
this.markUnavailable(error);
// Failing to start (e.g. the module cannot load) is not the task's fault: finish the
// queued calls on this thread and stop using the worker.
if (!online) {
this.markUnavailable(error);
this.runPendingInThread();
return;
}
this.reset(error);
});
worker.on('exit', (code) => {
@@ -139,6 +155,21 @@ class AlignWorkerRuntime {
else pending.reject(new Error(message.error));
}
private runPendingInThread(): void {
const worker = this.worker;
this.worker = null;
if (worker) void worker.terminate().catch(() => undefined);
const queued = [...this.pending.values()];
this.pending.clear();
for (const pending of queued) {
try {
pending.resolve(runAlignTask(pending.kind, pending.args));
} catch (error) {
pending.reject(error instanceof Error ? error : new Error(String(error)));
}
}
}
/** Stop the worker (and any computation in it) and fail every in-flight call. */
private reset(reason: unknown): void {
const error = reason instanceof Error ? reason : new Error(String(reason));