Fix support bundle export freeze and resume prealloc recovery

This commit is contained in:
Sucukdeluxe
2026-03-29 03:25:58 +02:00
parent 6105a08728
commit 650dafb535
6 changed files with 533 additions and 61 deletions
+107 -38
View File
@@ -126,7 +126,9 @@ const MAX_SAME_DIRECT_URL_ATTEMPTS = 3;
const RESUME_REWIND_BYTES = 256 * 1024;
const REALDEBRID_TOTAL_MISMATCH_TOLERANCE_BYTES = 64 * 1024;
const PREALLOC_RESUME_MISMATCH_THRESHOLD_BYTES = 1024 * 1024;
const LARGE_BINARY_FILE_RE = /\.(?:part\d+\.rar|rar|r\d{2,3}|zip(?:\.\d+)?|7z(?:\.\d+)?|tar|gz|bz2|xz|iso|mkv|mp4|avi|mov|wmv|m4v|ts|m2ts|webm|mp3|flac|aac|wav)$/i;
function expectedMinBytes(totalBytes: number | null | undefined, strict: boolean): number {
@@ -140,6 +142,12 @@ function itemExpectedMinBytes(item: DownloadItem): number {
const strict = isLargeBinaryLikePath(item.targetPath || item.fileName || "");
return expectedMinBytes(item.totalBytes, strict);
}
function resolvePreallocResumeMismatchThreshold(pathHint: string): number {
return isLargeBinaryLikePath(pathHint)
? 0
: PREALLOC_RESUME_MISMATCH_THRESHOLD_BYTES;
}
function resolvePackageItemDiskPath(pkg: PackageEntry, item: DownloadItem): string | null {
if (item.targetPath) {
@@ -8479,19 +8487,41 @@ export class DownloadManager extends EventEmitter {
} else if (resumeRewindBytesNextAttempt > 0) {
resumeRewindBytesNextAttempt = 0;
}
const persistedBytes = Math.max(0, Math.floor(Number(item.downloadedBytes) || 0));
const preallocMismatchThreshold = resolvePreallocResumeMismatchThreshold(item.fileName || effectiveTargetPath || "");
// Guard against pre-allocated sparse files from a crashed session:
// if file size exceeds persisted downloadedBytes by >1MB, the file was
// likely pre-allocated but only partially written before a hard crash.
if (existingBytes > 0 && item.downloadedBytes > 0 && existingBytes > item.downloadedBytes + 1048576) {
// if file size exceeds persisted downloadedBytes beyond the allowed
// mismatch threshold, the file was likely pre-allocated but only
// partially written before a hard crash.
// This must also run for persistedBytes=0, otherwise startup-resume can
// send Range=full-size and incorrectly accept HTTP 416 as "complete".
if (existingBytes > 0 && existingBytes > persistedBytes + preallocMismatchThreshold) {
try {
await fs.promises.truncate(effectiveTargetPath, item.downloadedBytes);
existingBytes = item.downloadedBytes;
} catch { /* best-effort */ }
}
const headers: Record<string, string> = {};
if (existingBytes > 0) {
headers.Range = `bytes=${existingBytes}-`;
}
const previousBytes = existingBytes;
await fs.promises.truncate(effectiveTargetPath, persistedBytes);
existingBytes = persistedBytes;
logAttemptEvent("WARN", "Pre-alloc-Rest erkannt, Teil-Datei auf persistierte Bytes gekuerzt", {
attempt,
previousBytes,
persistedBytes
});
} catch {
if (persistedBytes === 0) {
try {
await fs.promises.rm(effectiveTargetPath, { force: true });
existingBytes = 0;
} catch {
// ignore
}
}
}
}
const suspiciousResumeFootprint = existingBytes > 0
&& existingBytes > persistedBytes + preallocMismatchThreshold;
const headers: Record<string, string> = {};
if (existingBytes > 0) {
headers.Range = `bytes=${existingBytes}-`;
}
logAttemptEvent("INFO", "HTTP-Download-Versuch vorbereitet", {
attempt,
maxAttempts: maxAttempts === Number.MAX_SAFE_INTEGER ? "infinite" : maxAttempts,
@@ -8565,23 +8595,31 @@ export class DownloadManager extends EventEmitter {
const sizeToleranceBytes = isLargeBinaryLikePath(item.fileName || effectiveTargetPath) ? 0 : ALLOCATION_UNIT_SIZE;
const closeEnoughToExpected = expectedTotal != null
&& Math.abs(existingBytes - expectedTotal) <= sizeToleranceBytes;
if (expectedTotal != null && closeEnoughToExpected) {
const finalizedTotal = Math.max(existingBytes, expectedTotal);
item.totalBytes = finalizedTotal;
item.downloadedBytes = existingBytes;
item.progressPercent = 100;
item.speedBps = 0;
if (expectedTotal != null && closeEnoughToExpected && !suspiciousResumeFootprint) {
const finalizedTotal = Math.max(existingBytes, expectedTotal);
item.totalBytes = finalizedTotal;
item.downloadedBytes = existingBytes;
item.progressPercent = 100;
item.speedBps = 0;
item.updatedAt = nowMs();
logAttemptEvent("INFO", "HTTP 416 als vollständig behandelt", {
existingBytes,
expectedTotal: finalizedTotal
});
return { resumable: true };
}
try {
await fs.promises.rm(effectiveTargetPath, { force: true });
} catch {
expectedTotal: finalizedTotal
});
return { resumable: true };
}
if (expectedTotal != null && closeEnoughToExpected && suspiciousResumeFootprint) {
logAttemptEvent("WARN", "HTTP 416 trotz Vollgroesse nicht als fertig gewertet (vermutlich pre-alloc)", {
attempt,
existingBytes,
persistedBytes,
expectedTotal
});
}
try {
await fs.promises.rm(effectiveTargetPath, { force: true });
} catch {
// ignore
}
this.dropItemContribution(active.itemId);
@@ -10459,19 +10497,50 @@ export class DownloadManager extends EventEmitter {
}
try {
const stat = await fs.promises.stat(item.targetPath);
// Require file to be essentially complete — within one allocation unit of the
// expected size. The old 50% threshold incorrectly recovered partial downloads
// (e.g. 627 MB of 1001 MB) and triggered hybrid extraction on incomplete archives.
// Require file to be essentially complete — within one allocation unit of the
// expected size. The old 50% threshold incorrectly recovered partial downloads
// (e.g. 627 MB of 1001 MB) and triggered hybrid extraction on incomplete archives.
const minSize = expectedMinBytes(item.totalBytes, isLargeBinaryLikePath(item.fileName || item.targetPath));
if (stat.size >= minSize) {
// Re-check: another task may have started this item during the await
const latestItem = this.session.items[item.id];
if (!latestItem || this.activeTasks.has(item.id) || latestItem.status === "downloading"
|| latestItem.status === "validating" || latestItem.status === "integrity_check") {
continue;
}
// Guard against pre-allocated sparse files from a hard crash: file has
// the full expected size but downloadedBytes is significantly behind.
const persistedBytes = Math.max(0, Math.floor(Number(item.downloadedBytes) || 0));
const preallocMismatchThreshold = resolvePreallocResumeMismatchThreshold(item.fileName || item.targetPath || "");
const suspiciousPreallocFootprint = item.totalBytes != null
&& item.totalBytes > 0
&& stat.size >= minSize
&& stat.size > persistedBytes + preallocMismatchThreshold;
if (stat.size >= minSize) {
// Re-check: another task may have started this item during the await
const latestItem = this.session.items[item.id];
if (!latestItem || this.activeTasks.has(item.id) || latestItem.status === "downloading"
|| latestItem.status === "validating" || latestItem.status === "integrity_check") {
continue;
}
if (suspiciousPreallocFootprint) {
logger.warn(
`Item-Recovery: ${item.fileName} uebersprungen pre-alloc-Verdacht ` +
`(stat=${humanSize(stat.size)}, bytes=${humanSize(persistedBytes)}, total=${humanSize(item.totalBytes)})`
);
try {
if (persistedBytes > 0) {
fs.truncateSync(item.targetPath, persistedBytes);
} else {
fs.rmSync(item.targetPath, { force: true });
}
} catch {
// best-effort
}
item.status = "queued";
item.attempts = 0;
item.downloadedBytes = persistedBytes;
item.progressPercent = item.totalBytes > 0
? Math.max(0, Math.min(99, Math.floor((persistedBytes / item.totalBytes) * 100)))
: 0;
item.speedBps = 0;
item.fullStatus = "Wartet (Auto-Recovery: pre-alloc)";
item.updatedAt = nowMs();
continue;
}
// Guard against pre-allocated sparse files from a hard crash: file has
// the full expected size but downloadedBytes is significantly behind.
if (item.downloadedBytes > 0 && item.totalBytes && item.totalBytes > 0
&& stat.size >= minSize
&& item.downloadedBytes < item.totalBytes * 0.95) {