Reduce uploader shutdown stalls and add bounded runtime profiling
CI / verify (push) Canceled after 0s
CI / verify (push) Canceled after 0s
This commit is contained in:
1 parent
80eb878f1b
commit
fe3797fd18
10 files changed
+121
-18
No files matched your search
@@ -359,6 +359,12 @@ class ConfigStore {
|
||||
return out.join('<') || '?';
|
||||
}
|
||||
|
||||
getGlobalSetting(key) {
|
||||
if (!this._cache) this.load();
|
||||
this._scheduleConfigRefresh();
|
||||
return this._clone(this._cache.globalSettings?.[key]);
|
||||
}
|
||||
|
||||
load() {
|
||||
if (!this._perfLog) return this._loadImpl();
|
||||
const hadCache = !!this._cache;
|
||||
|
||||
+3
-1
@@ -16,7 +16,7 @@ function page(items, args) {
|
||||
}
|
||||
|
||||
function createMcpApp({ loadConfig, invoke, invokeGuarded = (_context, channel, ...args) => invoke(channel, ...args), renderer, collectors, getGrants, trashFile, canWrite,
|
||||
getCapabilities, getProtectedPaths, getInstallationId, getDiagnosticSources, getRuntimeDiagnostics = () => null, getPerformance = () => null, getSystemMetrics = () => null, updates }) {
|
||||
getCapabilities, getProtectedPaths, getInstallationId, getDiagnosticSources, getRuntimeDiagnostics = () => null, getPerformance = () => null, getSystemMetrics = () => null, profile, updates }) {
|
||||
const transfers = createMcpTransfers({ getGrants, getCapabilities, getProtectedPaths, getInstallationId });
|
||||
const sanitize = createMcpSanitizer(loadConfig);
|
||||
const guard = context => {
|
||||
@@ -78,6 +78,8 @@ function createMcpApp({ loadConfig, invoke, invokeGuarded = (_context, channel,
|
||||
})), boundaries: ['Permissions and directory grants can only be changed locally.', 'Destructive actions require an exact local confirmation; MCP cannot approve it.', 'No arbitrary shell, SQL, mouse/keyboard or screen control.', 'Paused automation is resumed with mhu_automation_resume. A disabled monitor requires mhu_automation_start.'] };
|
||||
},
|
||||
mhu_performance_get: () => getPerformance(),
|
||||
mhu_profile_start: args => profile.start(args.seconds),
|
||||
mhu_profile_get: () => profile.snapshot(),
|
||||
mhu_io_limits_get: () => getUploadReadSnapshot(),
|
||||
mhu_io_limits_update: ({ readConcurrency }, context) => {
|
||||
canWrite(); guard(context);
|
||||
|
||||
@@ -19,6 +19,8 @@ const definitions = [
|
||||
['app_info', 'Read application version and runtime information.', 'read', {}, []],
|
||||
['capabilities', 'Read effective permissions, the supported action catalog and local security boundaries. This cannot grant permissions or approve actions.', 'read', {}, []],
|
||||
['performance_get', 'Read bounded in-memory main-thread, renderer, CPU, memory, I/O and transfer-slot measurements. Missing samples do not prove a freeze.', 'read', {}, []],
|
||||
['profile_start', 'Start one bounded main-thread sampling profile. No debugger port, source text, arguments or arbitrary commands are exposed.', 'diagnostics', { seconds: integer(10, 120) }, []],
|
||||
['profile_get', 'Read the latest bounded main-thread profile summary with function names, source basenames and sampling gaps.', 'diagnostics', {}, []],
|
||||
['io_limits_get', 'Read active, waiting and maximum concurrent upload file reads.', 'read', {}, []],
|
||||
['io_limits_update', 'Adjust bounded upload file-read concurrency in memory. Does not abort transfers or persist settings; default returns after restart.', 'control', { readConcurrency: integer(1, 32) }, ['readConcurrency']],
|
||||
['system_metrics', 'Read cached system CPU, RAM, disk activity, read/write throughput, cumulative byte counters and volume capacity. No shell commands. Samples every 10 seconds; null or stale values are not freeze evidence.', 'read', { limit: integer(1, 60) }, []],
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
const { Session } = require('node:inspector');
|
||||
const path = require('node:path');
|
||||
|
||||
function summarizeProfile(profile) {
|
||||
const nodes = new Map((profile.nodes || []).map(node => [node.id, node]));
|
||||
const parents = new Map();
|
||||
for (const node of nodes.values()) for (const child of node.children || []) parents.set(child, node.id);
|
||||
const frame = id => {
|
||||
const source = nodes.get(id)?.callFrame || {};
|
||||
return { name: String(source.functionName || '(anonymous)').slice(0, 160),
|
||||
file: path.basename(String(source.url || '').replace(/\\/g, '/').split(/[?#]/)[0]), line: (source.lineNumber ?? -1) + 1 };
|
||||
};
|
||||
const stack = id => {
|
||||
const result = [], seen = new Set();
|
||||
while (id !== undefined && !seen.has(id) && result.length < 12) {
|
||||
seen.add(id); result.push(frame(id)); id = parents.get(id);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
const totals = new Map(), gaps = [];
|
||||
let elapsedMs = 0;
|
||||
for (let index = 0; index < (profile.samples || []).length; index++) {
|
||||
const id = profile.samples[index], durationMs = Math.max(0, (profile.timeDeltas?.[index] || 0) / 1000);
|
||||
elapsedMs += durationMs;
|
||||
const total = totals.get(id) || { ...frame(id), samples: 0, sampledMs: 0 };
|
||||
total.samples++; total.sampledMs += durationMs; totals.set(id, total);
|
||||
if (durationMs >= 100) gaps.push({ elapsedMs, durationMs, stack: stack(id), previousStack: stack(profile.samples[index - 1]) });
|
||||
}
|
||||
return { sampleCount: profile.samples?.length || 0, elapsedMs,
|
||||
top: [...totals.values()].sort((a, b) => b.sampledMs - a.sampledMs).slice(0, 40),
|
||||
gaps: gaps.sort((a, b) => b.durationMs - a.durationMs).slice(0, 20),
|
||||
note: 'Sampling gaps can include OS scheduling and native waits; sampled stacks alone do not prove their cause.' };
|
||||
}
|
||||
|
||||
class RuntimeProfile {
|
||||
constructor({ createSession = () => new Session(), schedule = setTimeout, now = Date.now } = {}) {
|
||||
this.createSession = createSession; this.schedule = schedule; this.now = now;
|
||||
this.state = { status: 'idle' };
|
||||
}
|
||||
async start(seconds = 60) {
|
||||
if (!Number.isInteger(seconds) || seconds < 10 || seconds > 120) throw Error('PROFILE_DURATION');
|
||||
if (this.session) throw Error('PROFILE_BUSY');
|
||||
const session = this.createSession();
|
||||
this.session = session;
|
||||
this.state = { status: 'starting', startedAt: this.now(), seconds };
|
||||
this.post = (method, params = {}) => new Promise((resolve, reject) => session.post(method, params, (error, result) => error ? reject(error) : resolve(result)));
|
||||
try {
|
||||
session.connect();
|
||||
await this.post('Profiler.enable');
|
||||
await this.post('Profiler.setSamplingInterval', { interval: 10000 });
|
||||
await this.post('Profiler.start');
|
||||
this.state.status = 'recording';
|
||||
this.timer = this.schedule(() => this.stop(), seconds * 1000);
|
||||
this.timer.unref?.();
|
||||
return this.snapshot();
|
||||
} catch {
|
||||
session.disconnect(); this.session = null; this.state.status = 'failed';
|
||||
throw Error('PROFILE_UNAVAILABLE');
|
||||
}
|
||||
}
|
||||
async stop() {
|
||||
if (!this.session) return this.snapshot();
|
||||
const session = this.session;
|
||||
try {
|
||||
const { profile } = await this.post('Profiler.stop');
|
||||
this.state = { ...this.state, status: 'completed', completedAt: this.now(), result: summarizeProfile(profile) };
|
||||
} catch { this.state.status = 'failed'; }
|
||||
finally { session.disconnect(); this.session = null; }
|
||||
return this.snapshot();
|
||||
}
|
||||
snapshot() { return structuredClone(this.state); }
|
||||
}
|
||||
|
||||
module.exports = { RuntimeProfile, summarizeProfile };
|
||||
+16
-10
@@ -50,21 +50,27 @@ function createStartupDiagnostics({ fs, path, directory, metadata = {}, now = Da
|
||||
const startedAt = now();
|
||||
const session = `${startedAt}-${process.pid}`;
|
||||
let sequence = 0;
|
||||
let pending = Promise.resolve(true);
|
||||
const reportedPhases = new Set();
|
||||
const write = (event, details = {}) => {
|
||||
const entry = { session, sequence: sequence++, timestamp: new Date(now()).toISOString(), elapsedMs: Math.max(0, now() - startedAt), event, ...details };
|
||||
try {
|
||||
fs.mkdirSync(directory, { recursive: true });
|
||||
if (fs.existsSync(filePath) && fs.statSync(filePath).size >= 1024 * 1024) {
|
||||
if (!fs.promises) return false;
|
||||
const line = JSON.stringify(entry) + '\n';
|
||||
pending = pending.then(async () => {
|
||||
await fs.promises.mkdir(directory, { recursive: true });
|
||||
const stat = await fs.promises.stat(filePath).catch(error => {
|
||||
if (error.code !== 'ENOENT') throw error;
|
||||
return null;
|
||||
});
|
||||
if (stat && stat.size >= 1024 * 1024) {
|
||||
const backup = filePath + '.1';
|
||||
if (fs.existsSync(backup)) fs.unlinkSync(backup);
|
||||
fs.renameSync(filePath, backup);
|
||||
await fs.promises.unlink(backup).catch(error => { if (error.code !== 'ENOENT') throw error; });
|
||||
await fs.promises.rename(filePath, backup);
|
||||
}
|
||||
fs.appendFileSync(filePath, JSON.stringify(entry) + '\n', 'utf8');
|
||||
await fs.promises.appendFile(filePath, line, 'utf8');
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}).catch(() => false);
|
||||
return true;
|
||||
};
|
||||
const recordPhase = (phase, generation) => {
|
||||
if (typeof phase !== 'string' || !STARTUP_PHASES.has(phase)) return false;
|
||||
@@ -75,7 +81,7 @@ function createStartupDiagnostics({ fs, path, directory, metadata = {}, now = Da
|
||||
return write('renderer-phase', { phase, generation });
|
||||
};
|
||||
write('process-start', metadata);
|
||||
return { filePath, write, recordPhase };
|
||||
return { filePath, write, recordPhase, drain: () => pending };
|
||||
}
|
||||
|
||||
function summarizeStartupPixels(image) {
|
||||
|
||||
+10
-1
@@ -1,13 +1,22 @@
|
||||
const os = require('node:os');
|
||||
const { execFile } = require('node:child_process');
|
||||
const { promisify } = require('node:util');
|
||||
const { performance } = require('node:perf_hooks');
|
||||
const { runtimePerformance } = require('./performance-monitor');
|
||||
|
||||
const WINDOWS_PROBE = "$ErrorActionPreference='Stop'; $ProgressPreference='SilentlyContinue'; $d=@(Get-CimInstance Win32_PerfRawData_PerfDisk_PhysicalDisk | ForEach-Object { @{ Name=$_.Name; Timestamp_PerfTime=[string]$_.Timestamp_PerfTime; Frequency_PerfTime=[string]$_.Frequency_PerfTime; Timestamp_Sys100NS=[string]$_.Timestamp_Sys100NS; PercentIdleTime=[string]$_.PercentIdleTime; DiskReadBytesPersec=[string]$_.DiskReadBytesPersec; DiskWriteBytesPersec=[string]$_.DiskWriteBytesPersec; DiskReadsPersec=[string]$_.DiskReadsPersec; DiskWritesPersec=[string]$_.DiskWritesPersec; CurrentDiskQueueLength=[string]$_.CurrentDiskQueueLength } }); $v=@(Get-CimInstance Win32_LogicalDisk -Filter 'DriveType=3' | ForEach-Object { @{ DeviceID=$_.DeviceID; Size=[string]$_.Size; FreeSpace=[string]$_.FreeSpace } }); @{disks=$d;volumes=$v} | ConvertTo-Json -Depth 4 -Compress";
|
||||
|
||||
async function readWindowsCounters({ runner = promisify(execFile), signal } = {}) {
|
||||
const { stdout } = await runner('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
|
||||
const started = performance.now();
|
||||
let request;
|
||||
try {
|
||||
request = runner('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
|
||||
['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', WINDOWS_PROBE],
|
||||
{ windowsHide: true, shell: false, timeout: 8000, maxBuffer: 1024 * 1024, encoding: 'utf8', signal });
|
||||
} finally {
|
||||
runtimePerformance.record('host.spawn.sync', performance.now() - started);
|
||||
}
|
||||
const { stdout } = await request;
|
||||
const value = JSON.parse(stdout.replace(/^\uFEFF/, ''));
|
||||
if (!Array.isArray(value.disks) || !Array.isArray(value.volumes) || value.disks.length > 256 || value.volumes.length > 256) throw Error('INVALID_METRICS');
|
||||
return value;
|
||||
|
||||
@@ -14,7 +14,7 @@ const startupDiagnostics = createStartupDiagnostics({
|
||||
fs, path, directory: app.getPath('userData'),
|
||||
metadata: { version: app.getVersion(), electron: process.versions.electron, platform: process.platform, osRelease: require('os').release(), remoteSession: /^RDP-/i.test(process.env.SESSIONNAME || ''), packaged: app.isPackaged }
|
||||
});
|
||||
process.once('exit', code => startupDiagnostics.write('process-exit', { code }));
|
||||
app.once('before-quit', () => startupDiagnostics.write('process-quit-requested'));
|
||||
const ConfigStore = require('./lib/config-store');
|
||||
const UploadManager = require('./lib/upload-manager');
|
||||
const { createSourceFileCleanup } = require('./lib/source-file-cleanup');
|
||||
@@ -53,6 +53,8 @@ const { createMcpApp } = require('./lib/mcp-app');
|
||||
const { requiresMcpConfirmation } = require('./lib/mcp-tools');
|
||||
const { McpAccessError } = require('./lib/mcp-access');
|
||||
const { runtimePerformance } = require('./lib/performance-monitor');
|
||||
const { RuntimeProfile } = require('./lib/runtime-profile');
|
||||
const runtimeProfile = new RuntimeProfile();
|
||||
const { SystemMetrics } = require('./lib/system-metrics');
|
||||
const systemMetrics = new SystemMetrics({ shouldCollect: () => mcpService?.active === true });
|
||||
runtimePerformance.observeSyncFileOperations(fs);
|
||||
@@ -1128,7 +1130,7 @@ function _flushUploadLog() {
|
||||
}
|
||||
|
||||
async function drainRuntimeLogs() {
|
||||
await Promise.all([_flushDebugLog(), _flushUploadLog(), _rotLogWriter.drain(), _uploadAuditWriter.drain()]);
|
||||
await Promise.all([_flushDebugLog(), _flushUploadLog(), _rotLogWriter.drain(), _uploadAuditWriter.drain(), startupDiagnostics.drain()]);
|
||||
await _rotLogFlusher.flush('rot-log');
|
||||
await Promise.all([_flushDebugLog(), _flushUploadLog()]);
|
||||
}
|
||||
@@ -2689,7 +2691,7 @@ ipcMain.handle('start-upload', async (_event, payload) => {
|
||||
fs,
|
||||
path,
|
||||
platform: process.platform,
|
||||
isEnabled: () => configStore.load().globalSettings?.deleteSourceAfterSuccessfulUpload === true,
|
||||
isEnabled: () => configStore.getGlobalSetting('deleteSourceAfterSuccessfulUpload') === true,
|
||||
audit: appendSourceCleanupAudit,
|
||||
journal: sourceDeleteJournal
|
||||
});
|
||||
@@ -4194,6 +4196,7 @@ async function startMcpService() {
|
||||
const handlers = createMcpApp({ loadConfig: () => configStore.load(), invoke, invokeGuarded, renderer: mcpRendererAction, collectors: createAppCollectors(),
|
||||
updates: mcpUpdate,
|
||||
getPerformance: () => runtimePerformance.snapshot(),
|
||||
profile: runtimeProfile,
|
||||
getSystemMetrics: args => systemMetrics.snapshot(args),
|
||||
getGrants: () => mcpService?.store.value.grants || [], trashFile: file => require('electron').shell.trashItem(file), canWrite: assertConfigWriteAllowed,
|
||||
getCapabilities: () => mcpService?.capabilities() || [],
|
||||
|
||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "multi-hoster-uploader",
|
||||
"version": "2.1.75",
|
||||
"version": "2.1.76",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "multi-hoster-uploader",
|
||||
"version": "2.1.75",
|
||||
"version": "2.1.76",
|
||||
"dependencies": {
|
||||
"chokidar": "^4.0.3",
|
||||
"undici": "7.29.1",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "multi-hoster-uploader",
|
||||
"version": "2.1.75",
|
||||
"version": "2.1.76",
|
||||
"description": "Upload files to doodstream, voe, vidmoly, byse simultaneously",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -51,6 +51,7 @@ const sourceFiles = [
|
||||
'lib/mcp-transfers.js',
|
||||
'lib/mcp-update.js',
|
||||
'lib/performance-monitor.js',
|
||||
'lib/runtime-profile.js',
|
||||
'lib/system-metrics.js',
|
||||
'lib/upload-file-io.js',
|
||||
'lib/online-backup.js',
|
||||
|
||||
Reference in new issue
Block a user