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';
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());
});
});
}
+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
// 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);
}
/**
+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;
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;
+23 -1
View File
@@ -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 };
+29 -5
View File
@@ -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)
};
+10 -2
View File
@@ -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,