From fe3797fd18cc5b7da325123fc8028baed5647813 Mon Sep 17 00:00:00 2001 From: Sucukdeluxe <259325684+Sucukdeluxe@users.noreply.github.com> Date: Tue, 20 Oct 2026 14:30:05 +0200 Subject: [PATCH] Reduce uploader shutdown stalls and add bounded runtime profiling --- lib/config-store.js | 6 +++ lib/mcp-app.js | 4 +- lib/mcp-tools.js | 2 + lib/runtime-profile.js | 74 +++++++++++++++++++++++++++++++ lib/startup-renderer.js | 26 ++++++----- lib/system-metrics.js | 11 ++++- main.js | 9 ++-- package-lock.json | 4 +- package.json | 2 +- scripts/verify-public-release.mjs | 1 + 10 files changed, 121 insertions(+), 18 deletions(-) create mode 100644 lib/runtime-profile.js diff --git a/lib/config-store.js b/lib/config-store.js index e3cdecf..33e37b2 100644 --- a/lib/config-store.js +++ b/lib/config-store.js @@ -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; diff --git a/lib/mcp-app.js b/lib/mcp-app.js index d3409b6..f98300d 100644 --- a/lib/mcp-app.js +++ b/lib/mcp-app.js @@ -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); diff --git a/lib/mcp-tools.js b/lib/mcp-tools.js index 9a8126d..f15a8bd 100644 --- a/lib/mcp-tools.js +++ b/lib/mcp-tools.js @@ -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) }, []], diff --git a/lib/runtime-profile.js b/lib/runtime-profile.js new file mode 100644 index 0000000..c7b1d11 --- /dev/null +++ b/lib/runtime-profile.js @@ -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 }; diff --git a/lib/startup-renderer.js b/lib/startup-renderer.js index 7990e13..251d1e3 100644 --- a/lib/startup-renderer.js +++ b/lib/startup-renderer.js @@ -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) { diff --git a/lib/system-metrics.js b/lib/system-metrics.js index 42ca785..ace3394 100644 --- a/lib/system-metrics.js +++ b/lib/system-metrics.js @@ -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; diff --git a/main.js b/main.js index 1ace7ee..642ce48 100644 --- a/main.js +++ b/main.js @@ -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() || [], diff --git a/package-lock.json b/package-lock.json index ede4ce0..d648933 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 50e1033..4c4c70a 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/scripts/verify-public-release.mjs b/scripts/verify-public-release.mjs index cb9a2c7..62e44a9 100644 --- a/scripts/verify-public-release.mjs +++ b/scripts/verify-public-release.mjs @@ -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',