Fix daily-log freezes and revert unrelated queue scope change

- Rewrite daily-log from synchronous fs.writeSync to async buffered
  writes (500ms flush interval), matching the main logger's pattern.
  The sync writes blocked the event loop on every log line.
- Revert the stop() queue scope change from v1.7.114 which was
  intended for a different project.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Sucukdeluxe 2026-03-24 10:03:35 +01:00
parent c51b52b86a
commit 45310f0bf7
2 changed files with 119 additions and 87 deletions

View File

@ -3,16 +3,24 @@ import path from "node:path";
import { addLogListener, removeLogListener } from "./logger"; import { addLogListener, removeLogListener } from "./logger";
const DAILY_LOG_RETENTION_DAYS = 30; const DAILY_LOG_RETENTION_DAYS = 30;
const CLEANUP_CHECK_INTERVAL_MS = 6 * 60 * 60 * 1000; // every 6 hours const CLEANUP_CHECK_INTERVAL_MS = 6 * 60 * 60 * 1000;
const FLUSH_INTERVAL_MS = 500;
const BUFFER_LIMIT_CHARS = 500_000;
let dailyLogDir = ""; let dailyLogDir = "";
let currentDayKey = ""; let currentDayKey = "";
let currentLogFd: number | null = null;
let currentRenameFd: number | null = null;
let logListener: ((line: string) => void) | null = null; let logListener: ((line: string) => void) | null = null;
let cleanupTimer: NodeJS.Timeout | null = null; let cleanupTimer: NodeJS.Timeout | null = null;
let lastCleanupAt = 0; let lastCleanupAt = 0;
// Async buffered writes — never blocks the event loop
let pendingLogLines: string[] = [];
let pendingLogChars = 0;
let pendingRenameLines: string[] = [];
let pendingRenameChars = 0;
let flushTimer: NodeJS.Timeout | null = null;
let flushInFlight = false;
function getDayKey(now = new Date()): string { function getDayKey(now = new Date()): string {
const year = now.getFullYear(); const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, "0"); const month = String(now.getMonth() + 1).padStart(2, "0");
@ -21,59 +29,84 @@ function getDayKey(now = new Date()): string {
} }
function getMonthDir(dayKey: string): string { function getMonthDir(dayKey: string): string {
return dayKey.slice(0, 7); // "YYYY-MM" return dayKey.slice(0, 7);
} }
function ensureDayFile(dayKey: string): number | null { function getDailyLogPath(dayKey: string): string {
if (currentDayKey === dayKey && currentLogFd !== null) { return path.join(dailyLogDir, getMonthDir(dayKey), `${dayKey}.log`);
return currentLogFd; }
}
// Close previous day's fd function getDailyRenameLogPath(dayKey: string): string {
if (currentLogFd !== null) { return path.join(dailyLogDir, getMonthDir(dayKey), `${dayKey}-rename.log`);
try { fs.closeSync(currentLogFd); } catch { /* ignore */ } }
currentLogFd = null;
}
if (currentRenameFd !== null) {
try { fs.closeSync(currentRenameFd); } catch { /* ignore */ }
currentRenameFd = null;
}
currentDayKey = dayKey; function scheduleFlush(): void {
const monthDir = path.join(dailyLogDir, getMonthDir(dayKey)); if (flushTimer || flushInFlight) return;
flushTimer = setTimeout(() => {
flushTimer = null;
void flushAsync();
}, FLUSH_INTERVAL_MS);
}
async function flushAsync(): Promise<void> {
if (flushInFlight) return;
flushInFlight = true;
try { try {
fs.mkdirSync(monthDir, { recursive: true }); const dayKey = currentDayKey || getDayKey();
const filePath = path.join(monthDir, `${dayKey}.log`);
currentLogFd = fs.openSync(filePath, "a"); if (pendingLogLines.length > 0) {
return currentLogFd; const chunk = pendingLogLines.join("");
} catch { pendingLogLines = [];
return null; pendingLogChars = 0;
const filePath = getDailyLogPath(dayKey);
try {
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
await fs.promises.appendFile(filePath, chunk, "utf8");
} catch { /* ignore */ }
}
if (pendingRenameLines.length > 0) {
const chunk = pendingRenameLines.join("");
pendingRenameLines = [];
pendingRenameChars = 0;
const filePath = getDailyRenameLogPath(dayKey);
try {
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
await fs.promises.appendFile(filePath, chunk, "utf8");
} catch { /* ignore */ }
}
} finally {
flushInFlight = false;
if (pendingLogLines.length > 0 || pendingRenameLines.length > 0) {
scheduleFlush();
}
} }
} }
function ensureRenameFd(dayKey: string): number | null { function flushSyncOnExit(): void {
if (currentDayKey === dayKey && currentRenameFd !== null) { const dayKey = currentDayKey || getDayKey();
return currentRenameFd;
if (pendingLogLines.length > 0) {
const chunk = pendingLogLines.join("");
pendingLogLines = [];
pendingLogChars = 0;
try {
const filePath = getDailyLogPath(dayKey);
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.appendFileSync(filePath, chunk, "utf8");
} catch { /* ignore */ }
} }
// ensureDayFile handles day transitions if (pendingRenameLines.length > 0) {
if (currentDayKey !== dayKey) { const chunk = pendingRenameLines.join("");
ensureDayFile(dayKey); pendingRenameLines = [];
} pendingRenameChars = 0;
try {
if (currentRenameFd !== null) { const filePath = getDailyRenameLogPath(dayKey);
return currentRenameFd; fs.mkdirSync(path.dirname(filePath), { recursive: true });
} fs.appendFileSync(filePath, chunk, "utf8");
} catch { /* ignore */ }
const monthDir = path.join(dailyLogDir, getMonthDir(dayKey));
try {
fs.mkdirSync(monthDir, { recursive: true });
const filePath = path.join(monthDir, `${dayKey}-rename.log`);
currentRenameFd = fs.openSync(filePath, "a");
return currentRenameFd;
} catch {
return null;
} }
} }
@ -81,31 +114,43 @@ function writeToDailyLog(line: string): void {
if (!dailyLogDir) return; if (!dailyLogDir) return;
const dayKey = getDayKey(); const dayKey = getDayKey();
const fd = ensureDayFile(dayKey); if (dayKey !== currentDayKey) {
if (fd === null) return; // Day changed — flush previous day's buffer first
if (currentDayKey && (pendingLogLines.length > 0 || pendingRenameLines.length > 0)) {
try { void flushAsync();
fs.writeSync(fd, line); }
} catch { currentDayKey = dayKey;
// Close and retry on next write
try { fs.closeSync(fd); } catch { /* ignore */ }
currentLogFd = null;
} }
pendingLogLines.push(line);
pendingLogChars += line.length;
// Shed oldest lines if buffer too large
while (pendingLogChars > BUFFER_LIMIT_CHARS && pendingLogLines.length > 1) {
const removed = pendingLogLines.shift();
if (removed) pendingLogChars -= removed.length;
}
scheduleFlush();
} }
export function writeToDailyRenameLog(line: string): void { export function writeToDailyRenameLog(line: string): void {
if (!dailyLogDir) return; if (!dailyLogDir) return;
const dayKey = getDayKey(); const dayKey = getDayKey();
const fd = ensureRenameFd(dayKey); if (dayKey !== currentDayKey) {
if (fd === null) return; currentDayKey = dayKey;
try {
fs.writeSync(fd, line);
} catch {
try { fs.closeSync(fd); } catch { /* ignore */ }
currentRenameFd = null;
} }
pendingRenameLines.push(line);
pendingRenameChars += line.length;
while (pendingRenameChars > BUFFER_LIMIT_CHARS && pendingRenameLines.length > 1) {
const removed = pendingRenameLines.shift();
if (removed) pendingRenameChars -= removed.length;
}
scheduleFlush();
} }
function cleanupOldDailyLogs(): void { function cleanupOldDailyLogs(): void {
@ -136,7 +181,6 @@ function cleanupOldDailyLogs(): void {
} catch { /* ignore */ } } catch { /* ignore */ }
} }
// Remove empty month dirs
try { try {
const remaining = fs.readdirSync(monthPath); const remaining = fs.readdirSync(monthPath);
if (remaining.length === 0) { if (remaining.length === 0) {
@ -156,16 +200,17 @@ export function initDailyLog(baseDir: string): void {
fs.mkdirSync(dailyLogDir, { recursive: true }); fs.mkdirSync(dailyLogDir, { recursive: true });
} catch { /* ignore */ } } catch { /* ignore */ }
// Attach listener to main logger currentDayKey = getDayKey();
logListener = (line: string) => writeToDailyLog(line); logListener = (line: string) => writeToDailyLog(line);
addLogListener(logListener); addLogListener(logListener);
// Initial cleanup
cleanupOldDailyLogs(); cleanupOldDailyLogs();
// Periodic cleanup
cleanupTimer = setInterval(cleanupOldDailyLogs, CLEANUP_CHECK_INTERVAL_MS); cleanupTimer = setInterval(cleanupOldDailyLogs, CLEANUP_CHECK_INTERVAL_MS);
if (cleanupTimer.unref) cleanupTimer.unref(); if (cleanupTimer.unref) cleanupTimer.unref();
process.once("exit", flushSyncOnExit);
} }
export function shutdownDailyLog(): void { export function shutdownDailyLog(): void {
@ -177,14 +222,11 @@ export function shutdownDailyLog(): void {
clearInterval(cleanupTimer); clearInterval(cleanupTimer);
cleanupTimer = null; cleanupTimer = null;
} }
if (currentLogFd !== null) { if (flushTimer) {
try { fs.closeSync(currentLogFd); } catch { /* ignore */ } clearTimeout(flushTimer);
currentLogFd = null; flushTimer = null;
}
if (currentRenameFd !== null) {
try { fs.closeSync(currentRenameFd); } catch { /* ignore */ }
currentRenameFd = null;
} }
flushSyncOnExit();
currentDayKey = ""; currentDayKey = "";
} }

View File

@ -4551,23 +4551,13 @@ export class DownloadManager extends EventEmitter {
active.abortReason = "stop"; active.abortReason = "stop";
active.abortController.abort("stop"); active.abortController.abort("stop");
} }
// Reset non-finished items. Items that were part of the current run // Reset all non-finished items to clean "Wartet" / "Paket gestoppt" state
// (runItemIds) go back to "Wartet" so they are picked up by the next start().
// Items that were NOT in the run set are marked "Gestoppt" so a subsequent
// start() does not accidentally include the entire queue.
const hadRunItems = this.runItemIds.size > 0;
for (const item of Object.values(this.session.items)) { for (const item of Object.values(this.session.items)) {
if (!isFinishedStatus(item.status)) { if (!isFinishedStatus(item.status)) {
const pkg = this.session.packages[item.packageId]; item.status = "queued";
const wasInRun = !hadRunItems || this.runItemIds.has(item.id);
if (wasInRun) {
item.status = "queued";
item.fullStatus = pkg && !pkg.enabled ? "Paket gestoppt" : "Wartet";
} else {
item.status = "cancelled";
item.fullStatus = "Gestoppt";
}
item.speedBps = 0; item.speedBps = 0;
const pkg = this.session.packages[item.packageId];
item.fullStatus = pkg && !pkg.enabled ? "Paket gestoppt" : "Wartet";
item.updatedAt = nowMs(); item.updatedAt = nowMs();
} }
} }