Fix blocking upload logging and recover DoodStream socket failures; release 2.1.67
CI / verify (push) Canceled after 0s

This commit is contained in:
Sucukdeluxe committed 2026-10-18 01:34:05 +02:00
1 parent 5a0cad0ca5
commit 9b06b4f852
10 files changed
+254 -114

No files matched your search

+1 -2
View File
@@ -235,8 +235,7 @@ class ConfigStore {
const tmp = this.historyPath + '.tmp'; const tmp = this.historyPath + '.tmp';
fs.writeFile(tmp, JSON.stringify(arr), 'utf-8', (err) => { fs.writeFile(tmp, JSON.stringify(arr), 'utf-8', (err) => {
if (err) return reject(err); if (err) return reject(err);
try { fs.renameSync(tmp, this.historyPath); } catch (e) { return reject(e); } fs.rename(tmp, this.historyPath, error => error ? reject(error) : resolve());
resolve();
}); });
}); });
} }
+60 -53
View File
@@ -10,7 +10,7 @@ const UPLOAD_TIMEOUT = 1800000; // 30 min
// Cap doodstream's per-hoster debug log alongside the main log files so // Cap doodstream's per-hoster debug log alongside the main log files so
// dev-mode sessions don't accumulate gigabytes of upload trace. // dev-mode sessions don't accumulate gigabytes of upload trace.
const { maybeRotateLogFile } = require('./log-rotation'); const { maybeRotateLogFileAsync } = require('./log-rotation');
const _DOODSTREAM_LOG_MAX_BYTES = 10 * 1024 * 1024; const _DOODSTREAM_LOG_MAX_BYTES = 10 * 1024 * 1024;
const _DOODSTREAM_LOG_MAX_BACKUPS = 1; const _DOODSTREAM_LOG_MAX_BACKUPS = 1;
@@ -29,15 +29,18 @@ function _doodstreamLogPath() {
} }
let _debugVerbose = false; let _debugVerbose = false;
let _debugWrite = Promise.resolve();
function setDebugVerbose(v) { _debugVerbose = !!v; } function setDebugVerbose(v) { _debugVerbose = !!v; }
function _debugLog(msg, force = false) { function _debugLog(msg, force = false) {
if (!_debugVerbose && !force) return; if (!_debugVerbose && !force) return;
try { try {
const logPath = _doodstreamLogPath(); const logPath = _doodstreamLogPath();
maybeRotateLogFile(logPath, _DOODSTREAM_LOG_MAX_BYTES, _DOODSTREAM_LOG_MAX_BACKUPS);
const ts = new Date().toISOString(); const ts = new Date().toISOString();
fs.appendFileSync(logPath, `[${ts}] ${msg}\n`); _debugWrite = _debugWrite.then(async () => {
await maybeRotateLogFileAsync(logPath, _DOODSTREAM_LOG_MAX_BYTES, _DOODSTREAM_LOG_MAX_BACKUPS);
await fs.promises.appendFile(logPath, `[${ts}] ${msg}\n`);
}).catch(() => {});
} catch {} } catch {}
} }
@@ -415,12 +418,15 @@ class DoodstreamUploader {
const totalSize = preambleBuf.length + fileSize + epilogueBuf.length; const totalSize = preambleBuf.length + fileSize + epilogueBuf.length;
let bytesRead = 0; let bytesRead = 0;
const attempt = new AbortController();
const uploadSignal = signal ? AbortSignal.any([signal, attempt.signal]) : attempt.signal;
async function* generate() { async function* generate() {
yield preambleBuf; yield preambleBuf;
for await (const chunk of readUploadFile(filePath, signal)) { for await (const chunk of readUploadFile(filePath, uploadSignal)) {
if (signal && signal.aborted) throw new Error('Aborted'); uploadSignal.throwIfAborted();
if (throttle) await throttle.consume(chunk.length, signal); if (throttle) await throttle.consume(chunk.length, uploadSignal);
uploadSignal.throwIfAborted();
bytesRead += chunk.length; bytesRead += chunk.length;
yield chunk; yield chunk;
if (progressCb) progressCb(bytesRead, fileSize); if (progressCb) progressCb(bytesRead, fileSize);
@@ -428,56 +434,57 @@ class DoodstreamUploader {
yield epilogueBuf; yield epilogueBuf;
} }
let uploadRes;
try { try {
uploadRes = await request(uploadUrl, { let uploadRes;
method: 'POST', try {
headers: { uploadRes = await request(uploadUrl, {
'Content-Type': `multipart/form-data; boundary=${boundary}`, method: 'POST',
'Content-Length': String(totalSize), headers: {
'User-Agent': USER_AGENT, 'Content-Type': `multipart/form-data; boundary=${boundary}`,
'Cookie': this._cookieHeader() 'Content-Length': String(totalSize),
}, 'User-Agent': USER_AGENT,
body: generate(), 'Cookie': this._cookieHeader()
signal, },
bodyTimeout: UPLOAD_TIMEOUT, body: generate(),
headersTimeout: 60000 signal: uploadSignal,
}); bodyTimeout: UPLOAD_TIMEOUT,
} catch (err) { headersTimeout: 60000
// Label which phase failed so a future "fetch failed"/"terminated" is });
// attributable to the big upload POST vs the small bookend requests. The } catch (err) {
// original message is preserved as a substring so upload-manager's const mb = Math.round(bytesRead / 1048576);
// transient classification still matches. NOTE: undici may surface const node = new URL(uploadUrl).hostname;
// "terminated"/"other side closed", which are not yet in that transient const error = new Error(`Doodstream Upload-POST (${mb} MB an ${node}): ${err && err.message ? err.message : err}`, { cause: err });
// list — revisit if logs show them. if (err?.code) error.code = err.code;
const mb = Math.round(bytesRead / 1048576); throw error;
throw new Error(`Doodstream Upload-POST (${mb} MB an ${uploadUrl}): ${err && err.message ? err.message : err}`);
}
const statusCode = uploadRes.statusCode;
_debugLog(`Upload response status: ${statusCode}`);
// Handle redirects from upload server (undici doesn't follow them)
if ([301, 302, 303, 307, 308].includes(statusCode)) {
const location = uploadRes.headers['location'];
try { await uploadRes.body.text(); } catch {}
_debugLog(`Upload redirect to: ${location}`);
if (location) {
return this._handleUploadResult(location);
} }
const statusCode = uploadRes.statusCode;
_debugLog(`Upload response status: ${statusCode}`);
// Handle redirects from upload server (undici doesn't follow them)
if ([301, 302, 303, 307, 308].includes(statusCode)) {
const location = uploadRes.headers['location'];
try { await uploadRes.body.text(); } catch {}
_debugLog(`Upload redirect to: ${location}`);
if (location) {
return this._handleUploadResult(location);
}
}
const resText = await uploadRes.body.text();
_debugLog(`Upload response body (first 500): ${resText.slice(0, 500)}`);
if (statusCode >= 400) {
let payload;
try { payload = JSON.parse(resText); } catch {}
const msg = payload && payload.msg ? payload.msg : resText.slice(0, 200);
throw new Error(`Doodstream Upload HTTP ${statusCode}: ${msg}`);
}
return await this._parseUploadResponse(resText);
} finally {
attempt.abort();
} }
const resText = await uploadRes.body.text();
_debugLog(`Upload response body (first 500): ${resText.slice(0, 500)}`);
if (statusCode >= 400) {
let payload;
try { payload = JSON.parse(resText); } catch {}
const msg = payload && payload.msg ? payload.msg : resText.slice(0, 200);
throw new Error(`Doodstream Upload HTTP ${statusCode}: ${msg}`);
}
return this._parseUploadResponse(resText);
} }
/** /**
+26 -1
View File
@@ -55,4 +55,29 @@ function maybeRotateLogFile(filePath, maxBytes, maxBackups = 3, log = () => {})
} }
} }
module.exports = { getRotatedLogPath, maybeRotateLogFile }; async function maybeRotateLogFileAsync(filePath, maxBytes, maxBackups = 3, log = () => {}) {
if (!filePath || !Number.isFinite(maxBytes) || maxBytes <= 0) return false;
let size;
try {
size = (await fs.promises.stat(filePath)).size;
} catch (error) {
if (error.code !== 'ENOENT') log(`logRotation: stat ${filePath} failed: ${error.message}`);
return false;
}
if (size <= maxBytes) return false;
try { await fs.promises.unlink(getRotatedLogPath(filePath, maxBackups)); } catch {}
for (let i = maxBackups - 1; i >= 1; i--) {
try { await fs.promises.rename(getRotatedLogPath(filePath, i), getRotatedLogPath(filePath, i + 1)); } catch {}
}
try {
const backupPath = getRotatedLogPath(filePath, 1);
await fs.promises.rename(filePath, backupPath);
log(`logRotation: rotated ${filePath} (${(size / 1024 / 1024).toFixed(1)} MB) → ${backupPath}`);
return true;
} catch (error) {
log(`logRotation: rename ${filePath} failed: ${error.message}`);
return false;
}
}
module.exports = { getRotatedLogPath, maybeRotateLogFile, maybeRotateLogFileAsync };
+32
View File
@@ -48,6 +48,38 @@ class PerformanceMonitor {
const durationMs = this.monotonic() - started; const durationMs = this.monotonic() - started;
this.record('renderer.roundtrip', durationMs); this.record('renderer.roundtrip', durationMs);
} }
observeSyncFileOperations(fs) {
const originals = new Map();
for (const name of ['statSync', 'statfsSync', 'existsSync', 'mkdirSync', 'openSync', 'readSync', 'readFileSync', 'writeSync', 'writeFileSync', 'appendFileSync', 'fsyncSync', 'renameSync', 'unlinkSync', 'readdirSync', 'truncateSync', 'closeSync']) {
if (typeof fs[name] !== 'function') continue;
const original = fs[name];
const monitor = this;
function measured(...args) {
const start = monitor.monotonic();
let failed = false;
try { return original.apply(this, args); }
catch (error) { failed = true; throw error; }
finally {
const durationMs = monitor.monotonic() - start;
if (durationMs >= 100) {
try {
monitor.record(`fs-sync.${name.toLowerCase()}`, durationMs, failed);
if (durationMs >= 1000) {
const caller = (new Error().stack || '').split('\n').slice(2)
.map(line => line.match(/([^/\\\s()]+\.js:\d+:\d+)\)?$/)?.[1]).find(Boolean) || null;
monitor.event('sync-file-delay', { operation: name, durationMs, failed, caller });
}
} catch {}
}
}
}
originals.set(name, { original, measured });
fs[name] = measured;
}
return () => {
for (const [name, { original, measured }] of originals) if (fs[name] === measured) fs[name] = original;
};
}
start({ version, getTransfers = () => null, pingRenderer = () => false } = {}) { start({ version, getTransfers = () => null, pingRenderer = () => false } = {}) {
if (this.timer) return; if (this.timer) return;
this.version = version; this.version = version;
+23 -1
View File
@@ -143,6 +143,28 @@ function createNativeStartupLog({ fs, path, directory, commandLine, now = Date.n
} catch { return false; } } catch { return false; }
finally { if (descriptor !== undefined) { try { fs.closeSync(descriptor); } catch {} } } finally { if (descriptor !== undefined) { try { fs.closeSync(descriptor); } catch {} } }
}; };
let maintenance = null;
const maintainAsync = () => {
if (maintenance) return maintenance;
maintenance = (async () => {
if (!enabled) return false;
let handle;
try {
const size = (await fs.promises.stat(nativePath)).size;
if (size <= maxBytes) return true;
handle = await fs.promises.open(nativePath, 'r');
const buffer = Buffer.alloc(maxBytes);
const { bytesRead } = await handle.read(buffer, 0, buffer.length, size - buffer.length);
await handle.close();
handle = null;
await fs.promises.writeFile(nativePath + '.previous', buffer.subarray(0, bytesRead), { mode: 0o600 });
await fs.promises.truncate(nativePath, 0);
return true;
} catch { return false; }
finally { if (handle) await handle.close().catch(() => {}); }
})().finally(() => { maintenance = null; });
return maintenance;
};
const files = () => list(nativePattern).sort((a, b) => Number(b.path === nativePath) - Number(a.path === nativePath)) const files = () => list(nativePattern).sort((a, b) => Number(b.path === nativePath) - Number(a.path === nativePath))
.map(file => ({ label: file.name, path: file.path, maxBytes })); .map(file => ({ label: file.name, path: file.path, maxBytes }));
const reports = () => list(reportPattern).sort((a, b) => Number(b.path === latestReport) - Number(a.path === latestReport)) const reports = () => list(reportPattern).sort((a, b) => Number(b.path === latestReport) - Number(a.path === latestReport))
@@ -159,7 +181,7 @@ function createNativeStartupLog({ fs, path, directory, commandLine, now = Date.n
} catch { return null; } } catch { return null; }
}; };
prune(reportPattern, 10); prune(reportPattern, 10);
return { enabled, nativePath, logDirectory, files, reports, saveReport, maintain }; return { enabled, nativePath, logDirectory, files, reports, saveReport, maintain, maintainAsync };
} }
module.exports = { createStartupWindow, resolveStartupLanguage, createStartupQuery, createStartupDiagnostics, summarizeStartupPixels, createNativeStartupLog }; module.exports = { createStartupWindow, resolveStartupLanguage, createStartupQuery, createStartupDiagnostics, summarizeStartupPixels, createNativeStartupLog };
+29 -5
View File
@@ -12,7 +12,7 @@ function createInternalLogPathResolver(options) {
const directories = [path.join(userDataPath, 'logs'), path.join(userDataPath, 'internal-logs')]; const directories = [path.join(userDataPath, 'logs'), path.join(userDataPath, 'internal-logs')];
return function resolveInternalLogPath(fileName, excludedPaths = new Set()) { function resolveInternalLogPath(fileName, excludedPaths = new Set()) {
if (typeof fileName !== 'string' || !fileName || path.basename(fileName) !== fileName) return null; if (typeof fileName !== 'string' || !fileName || path.basename(fileName) !== fileName) return null;
const excluded = excludedPaths instanceof Set const excluded = excludedPaths instanceof Set
? excludedPaths ? excludedPaths
@@ -26,7 +26,21 @@ function createInternalLogPathResolver(options) {
} catch {} } catch {}
} }
return null; return null;
}
resolveInternalLogPath.async = async (fileName, excludedPaths = new Set()) => {
if (typeof fileName !== 'string' || !fileName || path.basename(fileName) !== fileName) return null;
for (const directory of directories) {
const targetPath = path.join(directory, fileName);
if (excludedPaths.has(targetPath)) continue;
try {
await fs.promises.mkdir(directory, { recursive: true });
return targetPath;
} catch {}
}
return null;
}; };
return resolveInternalLogPath;
} }
function createInternalLogWriter(options) { function createInternalLogWriter(options) {
@@ -36,26 +50,29 @@ function createInternalLogWriter(options) {
const fileName = source.fileName; const fileName = source.fileName;
const resolveInternalLogPath = source.resolveInternalLogPath; const resolveInternalLogPath = source.resolveInternalLogPath;
const rotateLogFile = typeof source.rotateLogFile === 'function' ? source.rotateLogFile : () => {}; const rotateLogFile = typeof source.rotateLogFile === 'function' ? source.rotateLogFile : () => {};
const rotateLogFileAsync = typeof source.rotateLogFileAsync === 'function' ? source.rotateLogFileAsync : async () => {};
const resolveAsync = source.resolveInternalLogPathAsync || resolveInternalLogPath?.async;
const reportError = typeof source.reportError === 'function' ? source.reportError : () => {}; const reportError = typeof source.reportError === 'function' ? source.reportError : () => {};
const retryDelays = Array.isArray(source.retryDelays) && source.retryDelays.length > 0 ? source.retryDelays : [0, 100, 250]; const retryDelays = Array.isArray(source.retryDelays) && source.retryDelays.length > 0 ? source.retryDelays : [0, 100, 250];
const maxBytes = Number.isFinite(source.maxBytes) ? source.maxBytes : 10 * 1024 * 1024; const maxBytes = Number.isFinite(source.maxBytes) ? source.maxBytes : 10 * 1024 * 1024;
const maxBackups = Number.isFinite(source.maxBackups) ? source.maxBackups : 2; const maxBackups = Number.isFinite(source.maxBackups) ? source.maxBackups : 2;
let activePath = null; let activePath = null;
let pending = Promise.resolve();
if (!fs || !fs.promises || typeof fs.promises.appendFile !== 'function' || typeof fs.appendFileSync !== 'function' || typeof resolveInternalLogPath !== 'function' || typeof fileName !== 'string') { if (!fs || !fs.promises || typeof fs.promises.appendFile !== 'function' || typeof fs.appendFileSync !== 'function' || typeof resolveInternalLogPath !== 'function' || typeof fileName !== 'string') {
throw new TypeError('createInternalLogWriter requires fs, fileName and resolveInternalLogPath'); throw new TypeError('createInternalLogWriter requires fs, fileName and resolveInternalLogPath');
} }
async function append(line, label) { async function write(line, label) {
const excludedPaths = new Set(); const excludedPaths = new Set();
for (const delay of retryDelays) { for (const delay of retryDelays) {
if (delay) await new Promise(resolve => setTimeout(resolve, delay)); if (delay) await new Promise(resolve => setTimeout(resolve, delay));
const targetPath = resolveInternalLogPath(fileName, excludedPaths); const targetPath = await resolveAsync(fileName, excludedPaths);
if (!targetPath) break; if (!targetPath) break;
excludedPaths.add(targetPath); excludedPaths.add(targetPath);
try { try {
fs.mkdirSync(path.dirname(targetPath), { recursive: true }); await fs.promises.mkdir(path.dirname(targetPath), { recursive: true });
rotateLogFile(targetPath, maxBytes, maxBackups); await rotateLogFileAsync(targetPath, maxBytes, maxBackups);
await fs.promises.appendFile(targetPath, line, 'utf-8'); await fs.promises.appendFile(targetPath, line, 'utf-8');
activePath = targetPath; activePath = targetPath;
return true; return true;
@@ -66,6 +83,12 @@ function createInternalLogWriter(options) {
return false; return false;
} }
function append(line, label) {
const operation = pending.then(() => write(line, label));
pending = operation.catch(() => {});
return operation;
}
function appendSync(line, label) { function appendSync(line, label) {
const excludedPaths = new Set(); const excludedPaths = new Set();
for (let attempt = 0; attempt < retryDelays.length; attempt++) { for (let attempt = 0; attempt < retryDelays.length; attempt++) {
@@ -96,6 +119,7 @@ function createInternalLogWriter(options) {
append, append,
appendSync, appendSync,
flushSync, flushSync,
drain: () => pending,
getActivePath: () => activePath, getActivePath: () => activePath,
getPath: () => activePath || resolveInternalLogPath(fileName) getPath: () => activePath || resolveInternalLogPath(fileName)
}; };
+10 -2
View File
@@ -230,8 +230,15 @@ class UploadManager extends EventEmitter {
_isTransientNetworkError(err) { _isTransientNetworkError(err) {
if (!err) return false; if (!err) return false;
if (err.transientNetwork === true) return true; if (err.transientNetwork === true) return true;
if (!err.message) return false; const messages = [];
const m = String(err.message); const seen = new Set();
for (let current = err; current && !seen.has(current) && seen.size < 8; current = current.cause) {
seen.add(current);
if (current.name === 'AbortError' || current.code === 'ABORT_ERR') return false;
if (/^(?:UND_ERR_(?:SOCKET|CONNECT_TIMEOUT|HEADERS_TIMEOUT|BODY_TIMEOUT)|ECONNRESET|ECONNREFUSED|ETIMEDOUT|EPIPE)$/.test(current.code || '')) return true;
messages.push(String(current.message || ''));
}
const m = messages.join(' ');
const TRANSIENT = [ const TRANSIENT = [
/ENOTFOUND/i, /ENOTFOUND/i,
/ECONNRESET/i, /ECONNRESET/i,
@@ -242,6 +249,7 @@ class UploadManager extends EventEmitter {
/ENETUNREACH/i, /ENETUNREACH/i,
/EPIPE/i, /EPIPE/i,
/socket hang up/i, /socket hang up/i,
/other side closed/i,
/network (error|failure|problem)/i, /network (error|failure|problem)/i,
/dns (lookup|error|failed)/i, /dns (lookup|error|failed)/i,
/getaddrinfo/i, /getaddrinfo/i,
+70 -47
View File
@@ -32,7 +32,7 @@ const { createPortableSettingsSnapshot, prepareImportedSettings } = require('./l
const { createSettingsImportGate } = require('./lib/settings-import-gate'); const { createSettingsImportGate } = require('./lib/settings-import-gate');
const FolderMonitor = require('./lib/folder-monitor'); const FolderMonitor = require('./lib/folder-monitor');
const { walkFolderAsync } = require('./lib/file-discovery'); const { walkFolderAsync } = require('./lib/file-discovery');
const { maybeRotateLogFile } = require('./lib/log-rotation'); const { maybeRotateLogFile, maybeRotateLogFileAsync } = require('./lib/log-rotation');
const { hosterLogToFileEnabled } = require('./lib/log-policy'); const { hosterLogToFileEnabled } = require('./lib/log-policy');
const { formatUploadLogLine, iterateUploadLogEntries, summarizeBatchPlan, formatUploadPlanLogLine } = require('./lib/upload-log'); const { formatUploadLogLine, iterateUploadLogEntries, summarizeBatchPlan, formatUploadPlanLogLine } = require('./lib/upload-log');
const { createInternalLogPathResolver, createInternalLogWriter, createUploadAuditWriter, createBufferedInternalLogFlusher, getLogOpenDirectory } = require('./lib/upload-audit'); const { createInternalLogPathResolver, createInternalLogWriter, createUploadAuditWriter, createBufferedInternalLogFlusher, getLogOpenDirectory } = require('./lib/upload-audit');
@@ -48,6 +48,7 @@ const { McpService } = require('./lib/mcp-service');
const { createMcpApp } = require('./lib/mcp-app'); const { createMcpApp } = require('./lib/mcp-app');
const { McpAccessError } = require('./lib/mcp-access'); const { McpAccessError } = require('./lib/mcp-access');
const { runtimePerformance } = require('./lib/performance-monitor'); const { runtimePerformance } = require('./lib/performance-monitor');
runtimePerformance.observeSyncFileOperations(fs);
const { McpUpdate } = require('./lib/mcp-update'); const { McpUpdate } = require('./lib/mcp-update');
const mcpLocalHandlers = new Map(); const mcpLocalHandlers = new Map();
const mcpRawHandle = ipcMain.handle.bind(ipcMain); const mcpRawHandle = ipcMain.handle.bind(ipcMain);
@@ -431,7 +432,7 @@ const nativeStartupLog = _hasSingleInstanceLock ? createNativeStartupLog({ fs, p
const startupIncidentKeys = new Set(); const startupIncidentKeys = new Set();
if (nativeStartupLog) { if (nativeStartupLog) {
startupDiagnostics.write('native-logging', { enabled: nativeStartupLog.enabled }); startupDiagnostics.write('native-logging', { enabled: nativeStartupLog.enabled });
const nativeLogMaintenance = setInterval(() => nativeStartupLog.maintain(), 30000); const nativeLogMaintenance = setInterval(() => { void runtimePerformance.measure('log.native.maintain', () => nativeStartupLog.maintainAsync()); }, 30000);
nativeLogMaintenance.unref(); nativeLogMaintenance.unref();
process.once('exit', () => nativeStartupLog.maintain()); process.once('exit', () => nativeStartupLog.maintain());
} }
@@ -545,6 +546,7 @@ function getDebugLogPath() {
const _debugLogBuffer = []; const _debugLogBuffer = [];
let _debugLogFlushTimer = null; let _debugLogFlushTimer = null;
let _debugLogWriting = false; let _debugLogWriting = false;
let _debugLogWrite = Promise.resolve();
// 25 MB cap for upload-debug.log + 10 MB for account-rotation.log. Each // 25 MB cap for upload-debug.log + 10 MB for account-rotation.log. Each
// keeps 2 numbered backups, so the on-disk worst case is bounded: // keeps 2 numbered backups, so the on-disk worst case is bounded:
@@ -564,25 +566,32 @@ const _rotLogWriter = createInternalLogWriter({
fileName: 'account-rotation.log', fileName: 'account-rotation.log',
resolveInternalLogPath: _resolveInternalLogPath, resolveInternalLogPath: _resolveInternalLogPath,
rotateLogFile: maybeRotateLogFile, rotateLogFile: maybeRotateLogFile,
rotateLogFileAsync: maybeRotateLogFileAsync,
maxBytes: ROT_LOG_MAX_BYTES, maxBytes: ROT_LOG_MAX_BYTES,
maxBackups: INTERNAL_LOG_MAX_BACKUPS, maxBackups: INTERNAL_LOG_MAX_BACKUPS,
reportError: (label, error) => debugLog(`${label} append failed: ${error.message}`) reportError: (label, error) => debugLog(`${label} append failed: ${error.message}`)
}); });
function _flushDebugLog() { function _flushDebugLog() {
if (_debugLogWriting || _debugLogBuffer.length === 0) return; if (_debugLogWriting || _debugLogBuffer.length === 0) return _debugLogWrite;
const chunk = _debugLogBuffer.join(''); const chunk = _debugLogBuffer.splice(0).join('');
_debugLogBuffer.length = 0;
_debugLogWriting = true; _debugLogWriting = true;
const target = getDebugLogPath(); _debugLogWrite = runtimePerformance.measure('log.debug.write', async () => {
// Pass a noop logger here — debugLog() is THIS file's writer, recursing try {
// into it would deadlock the buffer/timer state. const target = getDebugLogPath();
maybeRotateLogFile(target, DEBUG_LOG_MAX_BYTES, INTERNAL_LOG_MAX_BACKUPS, () => {}); await maybeRotateLogFileAsync(target, DEBUG_LOG_MAX_BYTES, INTERNAL_LOG_MAX_BACKUPS);
fs.appendFile(target, chunk, 'utf-8', () => { await fs.promises.appendFile(target, chunk, 'utf-8');
_debugLogWriting = false; } catch {
// If more lines arrived during the write, flush them next tick. _debugLogBuffer.unshift(chunk);
if (_debugLogBuffer.length) setImmediate(_flushDebugLog); if (!_debugLogFlushTimer) {
_debugLogFlushTimer = setTimeout(() => { _debugLogFlushTimer = null; _flushDebugLog(); }, 1000);
}
} finally {
_debugLogWriting = false;
}
if (_debugLogBuffer.length && !_debugLogFlushTimer) setImmediate(_flushDebugLog);
}); });
return _debugLogWrite;
} }
function debugLog(msg) { function debugLog(msg) {
@@ -977,6 +986,7 @@ let _uploadLogFallbackWarned = false;
const _uploadLogBuffer = []; const _uploadLogBuffer = [];
let _uploadLogFlushTimer = null; let _uploadLogFlushTimer = null;
let _uploadLogWriting = false; let _uploadLogWriting = false;
let _uploadLogWrite = Promise.resolve();
// Cache the resolved upload-log target across flushes — mkdirSync + path // Cache the resolved upload-log target across flushes — mkdirSync + path
// assembly on every 500ms flush during uploads is wasted work once we've // assembly on every 500ms flush during uploads is wasted work once we've
@@ -1044,53 +1054,65 @@ const _uploadAuditWriter = createUploadAuditWriter({
path, path,
resolveInternalLogPath: _resolveInternalLogPath, resolveInternalLogPath: _resolveInternalLogPath,
rotateLogFile: maybeRotateLogFile, rotateLogFile: maybeRotateLogFile,
rotateLogFileAsync: maybeRotateLogFileAsync,
reportError: (label, error) => debugLog(`${label} audit append failed: ${error.message}`) reportError: (label, error) => debugLog(`${label} audit append failed: ${error.message}`)
}); });
async function _resolveUploadLogTargetAsync(excludedPath) {
const primary = getLogFilePath();
if (_cachedUploadLogKey === primary && _cachedUploadLogTarget && _cachedUploadLogTarget.path !== excludedPath) return _cachedUploadLogTarget;
const candidates = [{ path: primary, isFallback: false }];
for (const name of ['desktop', 'userData']) {
try { candidates.push({ path: buildFallbackLogName(app.getPath(name)), isFallback: true }); } catch {}
}
for (const target of candidates) {
if (target.path === excludedPath) continue;
try {
await fs.promises.mkdir(path.dirname(target.path), { recursive: true });
_cachedUploadLogTarget = target;
_cachedUploadLogKey = primary;
return target;
} catch (error) { debugLog('uploadLog directory unavailable: ' + error.message); }
}
return null;
}
function _flushUploadLog() { function _flushUploadLog() {
if (_uploadLogWriting || _uploadLogBuffer.length === 0) return; if (_uploadLogWriting || _uploadLogBuffer.length === 0) return _uploadLogWrite;
const target = _resolveUploadLogTarget(); const chunk = _uploadLogBuffer.splice(0).join('');
if (!target) { _uploadLogBuffer.length = 0; return; }
// Guard against the file's parent directory having been deleted/moved
// since the cache was filled. mkdirSync(recursive:true) is a no-op when
// the dir already exists; recreates it otherwise. Without this, every
// subsequent flush silently fails with ENOENT and entries are lost.
try { fs.mkdirSync(path.dirname(target.path), { recursive: true }); } catch {}
// Cheap size check + rotation right before the append, so we never grow
// a single log file beyond the cap regardless of session length.
maybeRotateLogFile(target.path, UPLOAD_LOG_MAX_BYTES, UPLOAD_LOG_MAX_BACKUPS, debugLog);
const chunk = _uploadLogBuffer.join('');
_uploadLogBuffer.length = 0;
_uploadLogWriting = true; _uploadLogWriting = true;
fs.appendFile(target.path, chunk, 'utf-8', (err) => { _uploadLogWrite = runtimePerformance.measure('log.upload.write', async () => {
_uploadLogWriting = false; try {
if (err) { const target = await _resolveUploadLogTargetAsync();
debugLog(`uploadLog append failed: ${err.message}`); if (!target) throw new Error('Upload log directory unavailable');
// Recovery: drop the cached target so the next flush re-resolves await fs.promises.mkdir(path.dirname(target.path), { recursive: true });
// (could be ENOENT after dir delete, ENOSPC, EBUSY etc.) and await maybeRotateLogFileAsync(target.path, UPLOAD_LOG_MAX_BYTES, UPLOAD_LOG_MAX_BACKUPS, debugLog);
// restore the chunk so we don't lose entries on the retry. await fs.promises.appendFile(target.path, chunk, 'utf-8');
_invalidateUploadLogTargetCache();
_uploadLogBuffer.unshift(chunk);
// Retry on the next event-loop tick rather than tight-looping.
if (!_uploadLogFlushTimer) {
_uploadLogFlushTimer = setTimeout(() => {
_uploadLogFlushTimer = null;
_flushUploadLog();
}, 1000);
}
} else {
_invalidateUploadLogEvidenceCache(); _invalidateUploadLogEvidenceCache();
if (target.isFallback && !_uploadLogFallbackWarned) { if (target.isFallback && !_uploadLogFallbackWarned) {
_uploadLogFallbackWarned = true; _uploadLogFallbackWarned = true;
// Auto-persist the working fallback into the user's config so the void _persistFallbackLogPath(target.path);
// next session writes here directly (no more fallback ladder) and
// the Settings input reflects reality.
_persistFallbackLogPath(target.path);
safeSend('upload-log-fallback', { fallbackPath: target.path }); safeSend('upload-log-fallback', { fallbackPath: target.path });
} }
} catch (error) {
debugLog('uploadLog append failed: ' + error.message);
_invalidateUploadLogTargetCache();
_uploadLogBuffer.unshift(chunk);
if (!_uploadLogFlushTimer) {
_uploadLogFlushTimer = setTimeout(() => { _uploadLogFlushTimer = null; _flushUploadLog(); }, 1000);
}
} finally {
_uploadLogWriting = false;
} }
if (_uploadLogBuffer.length && !_uploadLogFlushTimer) setImmediate(_flushUploadLog); if (_uploadLogBuffer.length && !_uploadLogFlushTimer) setImmediate(_flushUploadLog);
}); });
return _uploadLogWrite;
}
async function drainRuntimeLogs() {
await Promise.all([_flushDebugLog(), _flushUploadLog(), _rotLogWriter.drain(), _uploadAuditWriter.drain()]);
await _rotLogFlusher.flush('rot-log');
await Promise.all([_flushDebugLog(), _flushUploadLog()]);
} }
async function _persistFallbackLogPath(workingPath) { async function _persistFallbackLogPath(workingPath) {
@@ -3642,6 +3664,7 @@ ipcMain.handle('app:finish-close', async (event, payload = true) => {
try { try {
if (!acquireCloseQuiesce(attempt)) return false; if (!acquireCloseQuiesce(attempt)) return false;
await waitForCloseOperation(waitForConfigStoreWrites(), 1000); await waitForCloseOperation(waitForConfigStoreWrites(), 1000);
await waitForCloseOperation(drainRuntimeLogs(), 5000);
if (!isClosePreparationActive(attempt)) return false; if (!isClosePreparationActive(attempt)) return false;
if (payload && typeof payload === 'object' && Object.prototype.hasOwnProperty.call(payload, 'pendingQueue')) { if (payload && typeof payload === 'object' && Object.prototype.hasOwnProperty.call(payload, 'pendingQueue')) {
await waitForCloseOperation(configStore.savePendingQueue(payload.pendingQueue, { allowDuringQuiesce: true }), 1000); await waitForCloseOperation(configStore.savePendingQueue(payload.pendingQueue, { allowDuringQuiesce: true }), 1000);
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "multi-hoster-uploader", "name": "multi-hoster-uploader",
"version": "2.1.66", "version": "2.1.67",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "multi-hoster-uploader", "name": "multi-hoster-uploader",
"version": "2.1.66", "version": "2.1.67",
"dependencies": { "dependencies": {
"chokidar": "^4.0.3", "chokidar": "^4.0.3",
"undici": "7.29.1", "undici": "7.29.1",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "multi-hoster-uploader", "name": "multi-hoster-uploader",
"version": "2.1.66", "version": "2.1.67",
"description": "Upload files to doodstream, voe, vidmoly, byse simultaneously", "description": "Upload files to doodstream, voe, vidmoly, byse simultaneously",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {