// No child process may outlive the monitor. node t_children.js // Starts an edition with a scratch config (local server + a server over the stand-in ssh, dry run), opens several connection // tests and a console "sleep 30" (HANG on the Windows stand-in), ends the server and looks for survivors. Every process started // below the server carries SM_REVIEW_MARK in its environment, so survivors are found even after they were re-parented. const fs = require('fs'), path = require('path'), { spawn } = require('child_process'), { sleep, client, check, done } = require('./lib'); const [edition, port, how] = process.argv.slice(2), win = edition === 'W', T = __dirname, cfg = path.join(T, `cfgK${edition}`), mark = `mark-${process.pid}-${Date.now()}`; const root = path.join(process.env.HOME, 'Desktop', win ? 'Server Monitor(web-window)' : 'Server Monitor(web-linux)'); const marked = () => fs.readdirSync('/proc').filter(d => /^\d+$/.test(d)).filter(pid => { try { return fs.readFileSync(`/proc/${pid}/environ`, 'utf8').includes(mark); } catch { return false; } }) .map(pid => { try { return `${pid}:${fs.readFileSync(`/proc/${pid}/cmdline`, 'utf8').replace(/\0/g, ' ').trim().replace(/^.*\/(python3|node) /, '$1 ').slice(0, 60)}`; } catch { return ''; } }).filter(Boolean); (async () => { const tmp0 = fs.readdirSync(require('os').tmpdir()); fs.mkdirSync(path.join(cfg, 'ServerMonitorWeb'), { recursive: true }); fs.writeFileSync(path.join(cfg, 'ServerMonitorWeb', 'servers.json'), JSON.stringify({ version: 1, servers: [ { id: 'local1', name: 'local', transport: 0, interval: 1, enabled: true, autopilot: { enabled: true, dryRun: true } }, { id: 'ssh1', name: 'over ssh', transport: 1, host: 'fakehost', user: 'me', interval: 1, enabled: true, autopilot: { enabled: true, dryRun: true } }] })); fs.writeFileSync(cfg + '.ctl', 'ok'), fs.writeFileSync(cfg + '.log', ''), fs.writeFileSync(cfg + '.sshlog', ''); const env = { ...process.env, PATH: `${T}/fakessh:${win ? T + '/fake:' : ''}${process.env.PATH}`, FAKESSH_LOG: cfg + '.sshlog', FAKE_CTL: cfg + '.ctl', FAKE_LOG: cfg + '.log', XDG_CONFIG_HOME: cfg, APPDATA: cfg, PORT: port, SM_REVIEW_MARK: mark }; const srv = spawn('node', [path.join(root, 'server.js')], { env, stdio: ['ignore', 'ignore', fs.openSync(cfg + '.err', 'w')] }); const { post, state } = client(port); for (let i = 0; i < 40; ++i) { await sleep(250); try { if ((await state()).servers.every(s => s.online)) break; } catch {} } const st = await state(); check('both servers online (local and over the stand-in ssh)', st.servers.every(s => s.online), st.servers.map(s => s.cfg.name + '=' + s.status).join(', ')); // work in flight: three connection tests and two commands that would run for a long time const pending = [post('server/test', { name: 't1', transport: 0 }), post('server/test', { name: 't2', transport: 1, host: 'fakehost' }), post('server/test', { name: 't3', transport: 1, host: 'fakehost', password: 'pw' }), post('exec', { id: 'local1', cmd: win ? 'HANG' : 'sleep 30; echo late' }), post('exec', { id: 'ssh1', cmd: win ? 'HANG' : 'sleep 31' })].map(p => p.catch(() => null)); await sleep(700); const before = marked(); check('children are running', before.length >= 8, `${before.length}: ${before.map(x => x.replace(/^\d+:/, '')).join(' | ')}`); srv.kill(how); const t0 = Date.now(); await new Promise(r => srv.on('exit', r)); let left = marked(); for (; left.length && Date.now() - t0 < 4000; left = marked()) await sleep(200); check(`${how}: no child process is left`, !left.length, left.length ? `after 4 s still: ${left.join(' | ')}` : `all ${before.length} gone after ${((Date.now() - t0) / 1000).toFixed(1)} s`); await Promise.all(pending); for (const pid of marked().map(x => +x.split(':')[0])) try { process.kill(pid, 'SIGKILL'); } catch {} // never leave test processes behind const tmp = fs.readdirSync(require('os').tmpdir()).filter(f => /^sm-[A-Za-z0-9]{6}$/.test(f) && !tmp0.includes(f)); check('askpass helper folder is removed' + (how === 'SIGKILL' ? ' (not possible after a hard kill: reported only)' : ''), how === 'SIGKILL' || !tmp.length, tmp.join(' ')); console.log('server stderr:', JSON.stringify(fs.readFileSync(cfg + '.err', 'utf8').slice(0, 300))); done(); })();