From d8a099b8665eedc14a6ecd9c3f372ce5b93c9348 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 23 Sep 2026 20:22:04 +0000 Subject: [PATCH] fix: tail probe timeout is contention, not corruption - retry with doubled timeout; log unconfirmed flags as warn --- src/prober.js | 44 ++++++++++++++++++++++++++++---------------- src/sonarr.js | 3 ++- tests/prober.test.js | 17 ++++++++++++++++- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/prober.js b/src/prober.js index 231f156..ae393fc 100644 --- a/src/prober.js +++ b/src/prober.js @@ -52,6 +52,28 @@ export function evaluateIntegrity({ actualDurationSec, expectedRuntimeSec, toler }; } +/** + * Tail decode with one timeout-retry. A killed probe is CPU contention, not + * corruption evidence; retry with a doubled timeout before flagging. + * @returns {string|null} null on success, else failure text for evaluation. + */ +export async function runTailDecode(exec, args, timeoutMs) { + try { + await exec(args, { timeout: timeoutMs, maxBuffer: 512 * 1024 }); + return null; + } catch (err) { + if (!err.killed) return err.stderr || err.message; + try { + await exec(args, { timeout: timeoutMs * 2, maxBuffer: 512 * 1024 }); + return null; + } catch (err2) { + return err2.killed + ? `probe timeout after retry (${timeoutMs}ms then ${timeoutMs * 2}ms)` + : (err2.stderr || err2.message); + } + } +} + /** * Probes media file with ffprobe for duration and ffmpeg for tail decode check. * Strictly bounded in memory and execution time. @@ -106,22 +128,12 @@ export async function probeMediaFile(filePath, { let streamError = null; if (actualDurationSec && actualDurationSec > 5) { const seekSec = Math.max(0, Math.floor(actualDurationSec - tailSeconds)); - try { - // Decode last N seconds to null - await execFileAsync('ffmpeg', [ - '-v', 'error', - '-ss', String(seekSec), - '-i', filePath, - '-f', 'null', - '-', - ], { - timeout: timeoutMs, - maxBuffer: 512 * 1024, - }); - } catch (err) { - // Non-empty stderr or error return - streamError = err.stderr || err.message; - } + // Decode last N seconds to null (retries once on timeout) + streamError = await runTailDecode( + execFileAsync, + ['-v', 'error', '-ss', String(seekSec), '-i', filePath, '-f', 'null', '-'], + timeoutMs, + ); } const evalResult = evaluateIntegrity({ diff --git a/src/sonarr.js b/src/sonarr.js index 2669b6f..b0ffe04 100644 --- a/src/sonarr.js +++ b/src/sonarr.js @@ -196,7 +196,8 @@ export async function ensureUpcomingEpisodes(seriesId, season, episode, preloade } if (probeResult && !probeResult.valid) { - console.error(`[integrity] ERROR: Corrupt episode detected S${pad(ep.seasonNumber)}E${pad(ep.episodeNumber)}: ${probeResult.reason} - ${probeResult.details}`); + // Not corruption evidence yet - only the full-decode confirm decides. + console.warn(`[integrity] tail probe flagged S${pad(ep.seasonNumber)}E${pad(ep.episodeNumber)}: ${probeResult.reason} - ${probeResult.details}; running confirm decode`); if (integrityMode === 'auto_remediate') { // Confirm pass: full decode before anything is deleted (spec). diff --git a/tests/prober.test.js b/tests/prober.test.js index fb65248..b341c3e 100644 --- a/tests/prober.test.js +++ b/tests/prober.test.js @@ -1,6 +1,8 @@ import test from 'node:test'; import assert from 'node:assert/strict'; -import { resolveLocalPath, evaluateIntegrity } from '../src/prober.js'; +import { probeMediaFile, resolveLocalPath, evaluateIntegrity, runTailDecode } from '../src/prober.js'; + + test('resolveLocalPath correctly rewrites prefixes', () => { const sonarrPath = '/data/TV/Breaking Bad/Season 01/S01E01.mkv'; @@ -52,3 +54,16 @@ test('evaluateIntegrity checks duration tolerance and stream errors', () => { assert.equal(corrupt.valid, false); assert.equal(corrupt.reason, 'corrupt_stream'); }); + +test('runTailDecode retries on timeout with doubled timeout, passes stderr errors through', async () => { + let calls = 0; + const seen = []; + const killed = (t) => Object.assign(new Error('killed'), { killed: true }); + const fake = async (args, opts) => { calls += 1; seen.push(opts.timeout); throw killed(); }; + const r = await runTailDecode(fake, ['-v', 'error'], 1000); + assert.match(r, /probe timeout after retry \(1000ms then 2000ms\)/); + assert.deepEqual(seen, [1000, 2000]); + assert.equal(await runTailDecode(async () => null, [], 1000), null); + const io = Object.assign(new Error('io fail'), { killed: false, stderr: 'boomed' }); + assert.match(await runTailDecode(async () => { throw io; }, [], 1000), /boomed/); +});