diff --git a/lib/config-store.js b/lib/config-store.js index 73b55f9..b8eba3e 100644 --- a/lib/config-store.js +++ b/lib/config-store.js @@ -235,8 +235,7 @@ class ConfigStore { const tmp = this.historyPath + '.tmp'; fs.writeFile(tmp, JSON.stringify(arr), 'utf-8', (err) => { if (err) return reject(err); - try { fs.renameSync(tmp, this.historyPath); } catch (e) { return reject(e); } - resolve(); + fs.rename(tmp, this.historyPath, error => error ? reject(error) : resolve()); }); }); } diff --git a/lib/doodstream-upload.js b/lib/doodstream-upload.js index cc8903a..ec03055 100644 --- a/lib/doodstream-upload.js +++ b/lib/doodstream-upload.js @@ -10,7 +10,7 @@ const UPLOAD_TIMEOUT = 1800000; // 30 min // Cap doodstream's per-hoster debug log alongside the main log files so // 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_BACKUPS = 1; @@ -29,15 +29,18 @@ function _doodstreamLogPath() { } let _debugVerbose = false; +let _debugWrite = Promise.resolve(); function setDebugVerbose(v) { _debugVerbose = !!v; } function _debugLog(msg, force = false) { if (!_debugVerbose && !force) return; try { const logPath = _doodstreamLogPath(); - maybeRotateLogFile(logPath, _DOODSTREAM_LOG_MAX_BYTES, _DOODSTREAM_LOG_MAX_BACKUPS); 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 {} } @@ -415,12 +418,15 @@ class DoodstreamUploader { const totalSize = preambleBuf.length + fileSize + epilogueBuf.length; let bytesRead = 0; + const attempt = new AbortController(); + const uploadSignal = signal ? AbortSignal.any([signal, attempt.signal]) : attempt.signal; async function* generate() { yield preambleBuf; - for await (const chunk of readUploadFile(filePath, signal)) { - if (signal && signal.aborted) throw new Error('Aborted'); - if (throttle) await throttle.consume(chunk.length, signal); + for await (const chunk of readUploadFile(filePath, uploadSignal)) { + uploadSignal.throwIfAborted(); + if (throttle) await throttle.consume(chunk.length, uploadSignal); + uploadSignal.throwIfAborted(); bytesRead += chunk.length; yield chunk; if (progressCb) progressCb(bytesRead, fileSize); @@ -428,56 +434,57 @@ class DoodstreamUploader { yield epilogueBuf; } - let uploadRes; try { - uploadRes = await request(uploadUrl, { - method: 'POST', - headers: { - 'Content-Type': `multipart/form-data; boundary=${boundary}`, - 'Content-Length': String(totalSize), - 'User-Agent': USER_AGENT, - 'Cookie': this._cookieHeader() - }, - body: generate(), - signal, - bodyTimeout: UPLOAD_TIMEOUT, - headersTimeout: 60000 - }); - } catch (err) { - // Label which phase failed so a future "fetch failed"/"terminated" is - // attributable to the big upload POST vs the small bookend requests. The - // original message is preserved as a substring so upload-manager's - // transient classification still matches. NOTE: undici may surface - // "terminated"/"other side closed", which are not yet in that transient - // list — revisit if logs show them. - const mb = Math.round(bytesRead / 1048576); - 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); + let uploadRes; + try { + uploadRes = await request(uploadUrl, { + method: 'POST', + headers: { + 'Content-Type': `multipart/form-data; boundary=${boundary}`, + 'Content-Length': String(totalSize), + 'User-Agent': USER_AGENT, + 'Cookie': this._cookieHeader() + }, + body: generate(), + signal: uploadSignal, + bodyTimeout: UPLOAD_TIMEOUT, + headersTimeout: 60000 + }); + } catch (err) { + const mb = Math.round(bytesRead / 1048576); + const node = new URL(uploadUrl).hostname; + const error = new Error(`Doodstream Upload-POST (${mb} MB an ${node}): ${err && err.message ? err.message : err}`, { cause: err }); + if (err?.code) error.code = err.code; + throw error; } + + 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); } /** diff --git a/lib/log-rotation.js b/lib/log-rotation.js index a1e0b7d..52d1d5b 100644 --- a/lib/log-rotation.js +++ b/lib/log-rotation.js @@ -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 }; diff --git a/lib/performance-monitor.js b/lib/performance-monitor.js index d8f0d62..41fd2f0 100644 --- a/lib/performance-monitor.js +++ b/lib/performance-monitor.js @@ -48,6 +48,38 @@ class PerformanceMonitor { const durationMs = this.monotonic() - started; 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 } = {}) { if (this.timer) return; this.version = version; diff --git a/lib/startup-renderer.js b/lib/startup-renderer.js index e7b7c6c..6b25dfa 100644 --- a/lib/startup-renderer.js +++ b/lib/startup-renderer.js @@ -143,6 +143,28 @@ function createNativeStartupLog({ fs, path, directory, commandLine, now = Date.n } catch { return false; } 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)) .map(file => ({ label: file.name, path: file.path, maxBytes })); 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; } }; 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 }; diff --git a/lib/upload-audit.js b/lib/upload-audit.js index 24a293f..c913b91 100644 --- a/lib/upload-audit.js +++ b/lib/upload-audit.js @@ -12,7 +12,7 @@ function createInternalLogPathResolver(options) { 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; const excluded = excludedPaths instanceof Set ? excludedPaths @@ -26,7 +26,21 @@ function createInternalLogPathResolver(options) { } catch {} } 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) { @@ -36,26 +50,29 @@ function createInternalLogWriter(options) { const fileName = source.fileName; const resolveInternalLogPath = source.resolveInternalLogPath; 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 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 maxBackups = Number.isFinite(source.maxBackups) ? source.maxBackups : 2; 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') { throw new TypeError('createInternalLogWriter requires fs, fileName and resolveInternalLogPath'); } - async function append(line, label) { + async function write(line, label) { const excludedPaths = new Set(); for (const delay of retryDelays) { if (delay) await new Promise(resolve => setTimeout(resolve, delay)); - const targetPath = resolveInternalLogPath(fileName, excludedPaths); + const targetPath = await resolveAsync(fileName, excludedPaths); if (!targetPath) break; excludedPaths.add(targetPath); try { - fs.mkdirSync(path.dirname(targetPath), { recursive: true }); - rotateLogFile(targetPath, maxBytes, maxBackups); + await fs.promises.mkdir(path.dirname(targetPath), { recursive: true }); + await rotateLogFileAsync(targetPath, maxBytes, maxBackups); await fs.promises.appendFile(targetPath, line, 'utf-8'); activePath = targetPath; return true; @@ -66,6 +83,12 @@ function createInternalLogWriter(options) { return false; } + function append(line, label) { + const operation = pending.then(() => write(line, label)); + pending = operation.catch(() => {}); + return operation; + } + function appendSync(line, label) { const excludedPaths = new Set(); for (let attempt = 0; attempt < retryDelays.length; attempt++) { @@ -96,6 +119,7 @@ function createInternalLogWriter(options) { append, appendSync, flushSync, + drain: () => pending, getActivePath: () => activePath, getPath: () => activePath || resolveInternalLogPath(fileName) }; diff --git a/lib/upload-manager.js b/lib/upload-manager.js index 6d585f3..8ec9f35 100644 --- a/lib/upload-manager.js +++ b/lib/upload-manager.js @@ -230,8 +230,15 @@ class UploadManager extends EventEmitter { _isTransientNetworkError(err) { if (!err) return false; if (err.transientNetwork === true) return true; - if (!err.message) return false; - const m = String(err.message); + const messages = []; + 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 = [ /ENOTFOUND/i, /ECONNRESET/i, @@ -242,6 +249,7 @@ class UploadManager extends EventEmitter { /ENETUNREACH/i, /EPIPE/i, /socket hang up/i, + /other side closed/i, /network (error|failure|problem)/i, /dns (lookup|error|failed)/i, /getaddrinfo/i, diff --git a/main.js b/main.js index 94f4870..9d6bb21 100644 --- a/main.js +++ b/main.js @@ -32,7 +32,7 @@ const { createPortableSettingsSnapshot, prepareImportedSettings } = require('./l const { createSettingsImportGate } = require('./lib/settings-import-gate'); const FolderMonitor = require('./lib/folder-monitor'); 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 { formatUploadLogLine, iterateUploadLogEntries, summarizeBatchPlan, formatUploadPlanLogLine } = require('./lib/upload-log'); 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 { McpAccessError } = require('./lib/mcp-access'); const { runtimePerformance } = require('./lib/performance-monitor'); +runtimePerformance.observeSyncFileOperations(fs); const { McpUpdate } = require('./lib/mcp-update'); const mcpLocalHandlers = new Map(); const mcpRawHandle = ipcMain.handle.bind(ipcMain); @@ -431,7 +432,7 @@ const nativeStartupLog = _hasSingleInstanceLock ? createNativeStartupLog({ fs, p const startupIncidentKeys = new Set(); if (nativeStartupLog) { 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(); process.once('exit', () => nativeStartupLog.maintain()); } @@ -545,6 +546,7 @@ function getDebugLogPath() { const _debugLogBuffer = []; let _debugLogFlushTimer = null; let _debugLogWriting = false; +let _debugLogWrite = Promise.resolve(); // 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: @@ -564,25 +566,32 @@ const _rotLogWriter = createInternalLogWriter({ fileName: 'account-rotation.log', resolveInternalLogPath: _resolveInternalLogPath, rotateLogFile: maybeRotateLogFile, + rotateLogFileAsync: maybeRotateLogFileAsync, maxBytes: ROT_LOG_MAX_BYTES, maxBackups: INTERNAL_LOG_MAX_BACKUPS, reportError: (label, error) => debugLog(`${label} append failed: ${error.message}`) }); function _flushDebugLog() { - if (_debugLogWriting || _debugLogBuffer.length === 0) return; - const chunk = _debugLogBuffer.join(''); - _debugLogBuffer.length = 0; + if (_debugLogWriting || _debugLogBuffer.length === 0) return _debugLogWrite; + const chunk = _debugLogBuffer.splice(0).join(''); _debugLogWriting = true; - const target = getDebugLogPath(); - // Pass a noop logger here — debugLog() is THIS file's writer, recursing - // into it would deadlock the buffer/timer state. - maybeRotateLogFile(target, DEBUG_LOG_MAX_BYTES, INTERNAL_LOG_MAX_BACKUPS, () => {}); - fs.appendFile(target, chunk, 'utf-8', () => { - _debugLogWriting = false; - // If more lines arrived during the write, flush them next tick. - if (_debugLogBuffer.length) setImmediate(_flushDebugLog); + _debugLogWrite = runtimePerformance.measure('log.debug.write', async () => { + try { + const target = getDebugLogPath(); + await maybeRotateLogFileAsync(target, DEBUG_LOG_MAX_BYTES, INTERNAL_LOG_MAX_BACKUPS); + await fs.promises.appendFile(target, chunk, 'utf-8'); + } catch { + _debugLogBuffer.unshift(chunk); + if (!_debugLogFlushTimer) { + _debugLogFlushTimer = setTimeout(() => { _debugLogFlushTimer = null; _flushDebugLog(); }, 1000); + } + } finally { + _debugLogWriting = false; + } + if (_debugLogBuffer.length && !_debugLogFlushTimer) setImmediate(_flushDebugLog); }); + return _debugLogWrite; } function debugLog(msg) { @@ -977,6 +986,7 @@ let _uploadLogFallbackWarned = false; const _uploadLogBuffer = []; let _uploadLogFlushTimer = null; let _uploadLogWriting = false; +let _uploadLogWrite = Promise.resolve(); // Cache the resolved upload-log target across flushes — mkdirSync + path // assembly on every 500ms flush during uploads is wasted work once we've @@ -1044,53 +1054,65 @@ const _uploadAuditWriter = createUploadAuditWriter({ path, resolveInternalLogPath: _resolveInternalLogPath, rotateLogFile: maybeRotateLogFile, + rotateLogFileAsync: maybeRotateLogFileAsync, 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() { - if (_uploadLogWriting || _uploadLogBuffer.length === 0) return; - const target = _resolveUploadLogTarget(); - 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; + if (_uploadLogWriting || _uploadLogBuffer.length === 0) return _uploadLogWrite; + const chunk = _uploadLogBuffer.splice(0).join(''); _uploadLogWriting = true; - fs.appendFile(target.path, chunk, 'utf-8', (err) => { - _uploadLogWriting = false; - if (err) { - debugLog(`uploadLog append failed: ${err.message}`); - // Recovery: drop the cached target so the next flush re-resolves - // (could be ENOENT after dir delete, ENOSPC, EBUSY etc.) and - // restore the chunk so we don't lose entries on the retry. - _invalidateUploadLogTargetCache(); - _uploadLogBuffer.unshift(chunk); - // Retry on the next event-loop tick rather than tight-looping. - if (!_uploadLogFlushTimer) { - _uploadLogFlushTimer = setTimeout(() => { - _uploadLogFlushTimer = null; - _flushUploadLog(); - }, 1000); - } - } else { + _uploadLogWrite = runtimePerformance.measure('log.upload.write', async () => { + try { + const target = await _resolveUploadLogTargetAsync(); + if (!target) throw new Error('Upload log directory unavailable'); + await fs.promises.mkdir(path.dirname(target.path), { recursive: true }); + await maybeRotateLogFileAsync(target.path, UPLOAD_LOG_MAX_BYTES, UPLOAD_LOG_MAX_BACKUPS, debugLog); + await fs.promises.appendFile(target.path, chunk, 'utf-8'); _invalidateUploadLogEvidenceCache(); if (target.isFallback && !_uploadLogFallbackWarned) { _uploadLogFallbackWarned = true; - // Auto-persist the working fallback into the user's config so the - // next session writes here directly (no more fallback ladder) and - // the Settings input reflects reality. - _persistFallbackLogPath(target.path); + void _persistFallbackLogPath(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); }); + 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) { @@ -3642,6 +3664,7 @@ ipcMain.handle('app:finish-close', async (event, payload = true) => { try { if (!acquireCloseQuiesce(attempt)) return false; await waitForCloseOperation(waitForConfigStoreWrites(), 1000); + await waitForCloseOperation(drainRuntimeLogs(), 5000); if (!isClosePreparationActive(attempt)) return false; if (payload && typeof payload === 'object' && Object.prototype.hasOwnProperty.call(payload, 'pendingQueue')) { await waitForCloseOperation(configStore.savePendingQueue(payload.pendingQueue, { allowDuringQuiesce: true }), 1000); diff --git a/package-lock.json b/package-lock.json index 9298acd..105f3e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "multi-hoster-uploader", - "version": "2.1.66", + "version": "2.1.67", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "multi-hoster-uploader", - "version": "2.1.66", + "version": "2.1.67", "dependencies": { "chokidar": "^4.0.3", "undici": "7.29.1", diff --git a/package.json b/package.json index 46e4767..6e3e5e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "multi-hoster-uploader", - "version": "2.1.66", + "version": "2.1.67", "description": "Upload files to doodstream, voe, vidmoly, byse simultaneously", "main": "main.js", "scripts": {