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
44 changes: 28 additions & 16 deletions src/prober.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion src/sonarr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
17 changes: 16 additions & 1 deletion tests/prober.test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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/);
});
Loading