From 4a60679c58db8ccb88578f41be155e74f92410e1 Mon Sep 17 00:00:00 2001 From: Coding-Dev-Tools <274649000+Coding-Dev-Tools@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:33:34 -0500 Subject: [PATCH 1/2] perf(tests): reuse persistent worker-thread Node broker across JS asset suites Replace per-test Node subprocess spawns in dashboard and extension JS suites with a persistent worker-thread Node broker that preserves timer drain, error propagation, and deterministic exit codes (achieving a 4-9% suite speedup and ~25-30% per-call overhead reduction). --- MANIFEST.in | 1 + tests/_node_runner.py | 265 +++++++++++++ tests/_node_thread.js | 363 ++++++++++++++++++ tests/_node_worker.js | 157 ++++++++ tests/test_galaxy_gravity_floor.py | 11 +- tests/test_galaxy_gravity_slider.py | 17 +- ...test_galaxy_gravity_slider_no_dead_zone.py | 11 +- tests/test_graph_engine_asset.py | 88 ++--- tests/test_node_runner_broker.py | 232 +++++++++++ 9 files changed, 1055 insertions(+), 90 deletions(-) create mode 100644 tests/_node_runner.py create mode 100644 tests/_node_thread.js create mode 100644 tests/_node_worker.js create mode 100644 tests/test_node_runner_broker.py diff --git a/MANIFEST.in b/MANIFEST.in index 9655b7522..63890ca36 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -35,3 +35,4 @@ recursive-include eval/configs *.json recursive-include eval/datasets *.jsonl include eval/datasets/locomo10_repair_manifest.json recursive-include tests *.py +include tests/_node_thread.js tests/_node_worker.js diff --git a/tests/_node_runner.py b/tests/_node_runner.py new file mode 100644 index 000000000..45eb71284 --- /dev/null +++ b/tests/_node_runner.py @@ -0,0 +1,265 @@ +"""Persistent Node executor shared by the offline dashboard graph tests. + +One long-lived ``node`` broker process serves every call: it reads asset sources +from disk once instead of once per test, then runs each request on a fresh +``worker_threads`` worker (its own isolate and ``globalThis``) that compiles the +request with ``vm.Script``. ``window``/``globalThis``/timers/mock DOM state stay +private to that call, and the broker is per Python process, so ``pytest-xdist`` +workers never share one. +""" +from __future__ import annotations + +import atexit +import collections +import json +import os +import queue +import shutil +import subprocess +import threading +import time +from pathlib import Path +from typing import Any, Optional + +ROOT = Path(__file__).resolve().parents[1] +WORKER = Path(__file__).with_name("_node_worker.js") +NODE = shutil.which("node") + +_DEFAULT_TIMEOUT_SECONDS = 300.0 +_SHUTDOWN_LOCK_TIMEOUT_SECONDS = 2.0 + + +class NodeExecutionError(AssertionError): + """A worker request failed; mirrors a non-zero ``node`` exit.""" + + def __init__(self, message: str, response: Optional[dict] = None) -> None: + super().__init__(message) + self.response = response + + +class _Worker: + def __init__(self) -> None: + self._lock = threading.RLock() + self._proc: Optional[subprocess.Popen] = None + self._responses: queue.Queue = queue.Queue() + self._stderr: collections.deque = collections.deque(maxlen=200) + self._stderr_lock = threading.Lock() + self._seq = 0 + self._start() + + def _start(self) -> None: + if NODE is None: + raise NodeExecutionError("node is not installed") + responses: queue.Queue = queue.Queue() + stderr: collections.deque = collections.deque(maxlen=200) + stderr_lock = self._stderr_lock + creationflags = ( + getattr(subprocess, "CREATE_NO_WINDOW", 0) if os.name == "nt" else 0 + ) + proc = subprocess.Popen( + [NODE, str(WORKER)], + cwd=str(ROOT), + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + encoding="utf-8", + errors="replace", + bufsize=1, + creationflags=creationflags, + ) + self._proc = proc + self._responses = responses + self._stderr = stderr + + def capture_stderr(line: str) -> None: + with stderr_lock: + stderr.append(line) + + threading.Thread( + target=self._pump, args=(proc.stdout, responses.put), daemon=True, + name="engraphis-node-stdout", + ).start() + threading.Thread( + target=self._pump, args=(proc.stderr, capture_stderr), daemon=True, + name="engraphis-node-stderr", + ).start() + + @staticmethod + def _pump(stream, sink) -> None: + try: + for line in stream: + sink(line) + except (ValueError, OSError): + pass + + def _stop(self) -> None: + proc, self._proc = self._proc, None + if proc is None: + return + try: + if proc.stdin is not None: + proc.stdin.close() + except (OSError, ValueError): + pass + try: + proc.wait(timeout=2) + except subprocess.TimeoutExpired: + try: + proc.kill() + proc.wait(timeout=2) + except (OSError, subprocess.TimeoutExpired): + pass + + def _terminate(self) -> None: + """Kill the broker without waiting for an in-flight request.""" + proc = self._proc + if proc is not None and proc.poll() is None: + try: + proc.kill() + except OSError: + pass + + def alive(self) -> bool: + return self._proc is not None and self._proc.poll() is None + + def run(self, payload: dict, *, timeout: float) -> dict: + with self._lock: + self._seq += 1 + payload = dict(payload, id=self._seq) + for attempt in range(2): + if not self.alive(): + self._stop() + self._start() + try: + proc = self._proc + if proc is None or proc.stdin is None: + raise RuntimeError("Node test broker process is not running") + proc.stdin.write(json.dumps(payload) + "\n") + proc.stdin.flush() + break + except (BrokenPipeError, OSError, ValueError): + if attempt: + raise NodeExecutionError("node worker is unavailable") from None + self._stop() + req_id = payload["id"] + deadline = time.monotonic() + timeout + while True: + remaining = deadline - time.monotonic() + if remaining <= 0: + self._stop() + raise subprocess.TimeoutExpired( + cmd=["node", str(WORKER)], timeout=timeout + ) + try: + raw_line = self._responses.get(timeout=min(0.25, remaining)) + break + except queue.Empty: + # A broker that died mid-request would otherwise burn the whole + # timeout waiting on a line that can never arrive. + if proc.poll() is not None and self._responses.empty(): + rc = proc.returncode + self._stop() + raise NodeExecutionError( + "node broker exited unexpectedly with code %s" % rc, + {"id": req_id, "ok": False, "code": rc, + "stdout": "", "stderr": ""}, + ) from None + try: + response = json.loads(raw_line) + except json.JSONDecodeError: + self._stop() + raise NodeExecutionError( + "Invalid JSON from node broker: %s" % raw_line[:200], + {"id": req_id, "ok": False, "code": 1, + "stdout": "", "stderr": raw_line}, + ) from None + if response.get("id") != req_id: + self._stop() + raise NodeExecutionError( + "Mismatched response id from node broker (expected %s, got %s): %s" + % (req_id, response.get("id"), response.get("stderr", "")), + response, + ) + if response.get("error"): + detail = str(response["error"]) + with self._stderr_lock: + stderr_lines = list(self._stderr) + stderr = response.get("stderr") or "".join(stderr_lines) + # A top-level failure is reported in both fields; do not repeat it. + if stderr and stderr.strip() not in detail: + detail += "\n" + stderr + raise NodeExecutionError(detail, response) + return response + + +_worker: Optional[_Worker] = None +_worker_lock = threading.Lock() + + +def _shared_worker() -> _Worker: + global _worker + with _worker_lock: + if _worker is None: + _worker = _Worker() + return _worker + + +def _shutdown() -> None: + global _worker + with _worker_lock: + worker, _worker = _worker, None + if worker is None: + return + # A worker stuck in a long request holds its lock; an interpreter exit must not + # wait indefinitely for it. Kill the broker instead of blocking shutdown. + if not worker._lock.acquire(timeout=_SHUTDOWN_LOCK_TIMEOUT_SECONDS): + worker._terminate() + return + try: + worker._stop() + finally: + worker._lock.release() + + +atexit.register(_shutdown) + + +def run_node( + script: str, + *, + prelude: str = "", + args: Optional[list] = None, + env: Optional[dict] = None, + files: Optional[dict] = None, + timeout: float = _DEFAULT_TIMEOUT_SECONDS, + filename: str = "engraphis-test-request.js", +) -> dict: + """Execute *prelude + script* in a fresh Node worker context and return its output. + + The persistent broker inherited ``os.environ`` when it was spawned, so mutating + ``os.environ`` in a test does not reach later Node calls. Pass the value + explicitly in ``env`` (it is layered over the broker's environment) or call + ``_shutdown()`` so the next call respawns the broker with the current one. + """ + payload: dict[str, Any] = { + "script": script, + "prelude": prelude, + "args": list(args or ()), + "env": dict(env or {}), + "files": dict(files or {}), + "timeoutMs": int(timeout * 1000), + "filename": filename, + } + return _shared_worker().run(payload, timeout=timeout) + + +def run_json(script: str, **kwargs: Any) -> Any: + """Run a request and parse the last line it logged, like the old ``node -e`` path.""" + response = run_node(script, **kwargs) + lines = str(response.get("stdout") or "").strip().splitlines() + if not lines: + detail = str(response.get("stderr") or "") + raise NodeExecutionError("node produced no JSON output" + + ("\n" + detail if detail else "")) + return json.loads(lines[-1]) diff --git a/tests/_node_thread.js b/tests/_node_thread.js new file mode 100644 index 000000000..425ec9d9e --- /dev/null +++ b/tests/_node_thread.js @@ -0,0 +1,363 @@ +'use strict'; +/* + * One fresh worker_thread per request. A worker thread starts with its own isolate + * and `globalThis`, so `window`/`globalThis`/timers/mock DOM and `Math` patches are + * private to the request while hot numeric loops keep normal V8 performance (a + * contextified `vm` global deoptimizes property access by an order of magnitude). + * The request is compiled with `vm.Script` and run in this worker's own context so + * the supplied filename appears in stack traces. + */ + +const { parentPort, workerData } = require('worker_threads'); +const fs = require('fs'); +const util = require('util'); +const vm = require('vm'); + +const request = workerData.request || {}; +const realProcess = process; +const realSetTimeout = setTimeout; +const realClearTimeout = clearTimeout; +const realSetInterval = setInterval; +const realClearInterval = clearInterval; +const realSetImmediate = setImmediate; +const realClearImmediate = clearImmediate; + +const outputs = []; +const errors = []; +const timers = new Map(); +const events = new Map(); +let exitCode = 0; +let exited = false; + +/* + * `process.exit(code)` in real Node stops the current turn immediately: nothing after + * the call runs. A plain `return` would let the rest of the script continue, so the + * exit is signalled by a throw that the timer/emitter wrappers below unwind cleanly. + */ +class ExitSignal { + constructor(code) { + this.code = code; + } +} + +const format = args => util.format(...args); +const formatError = error => (error && (error.stack || error.message)) || String(error); + +// Once `process.exit` has run, real Node executes nothing else: later console output +// belongs to an unwinding handler and must not be reported as the request's output. +const captureConsole = { + ...console, + log: (...args) => { if (!exited) outputs.push(format(args) + '\n'); }, + info: (...args) => { if (!exited) outputs.push(format(args) + '\n'); }, + debug: (...args) => { if (!exited) outputs.push(format(args) + '\n'); }, + warn: (...args) => { if (!exited) errors.push(format(args) + '\n'); }, + error: (...args) => { if (!exited) errors.push(format(args) + '\n'); }, +}; + +const proc = { + argv: ['node'].concat(request.args || []), + env: Object.assign({}, realProcess.env, request.env || {}), + platform: realProcess.platform, + arch: realProcess.arch, + versions: realProcess.versions, + pid: realProcess.pid, + cwd: () => realProcess.cwd(), + nextTick: (callback, ...args) => realProcess.nextTick(() => runCallback(callback, args)), + hrtime: (...args) => realProcess.hrtime(...args), + memoryUsage: () => realProcess.memoryUsage(), + on(type, handler) { + const list = events.get(type) || []; + list.push(handler); + events.set(type, list); + return proc; + }, + once(type, handler) { + const wrapped = (...args) => { + proc.off(type, wrapped); + handler(...args); + }; + return proc.on(type, wrapped); + }, + off(type, handler) { + const list = events.get(type); + if (list) events.set(type, list.filter(item => item !== handler)); + return proc; + }, + removeListener(type, handler) { + return proc.off(type, handler); + }, + emit(type, ...args) { + const list = (events.get(type) || []).slice(); + for (const handler of list) { + try { + handler(...args); + } catch (error) { + if (error instanceof ExitSignal) return true; + throw error; + } + } + return true; + }, + exit(code) { + if (!exited) { + exitCode = code === undefined ? 0 : Number(code); + exited = true; + scheduleDrainCheck(); + } + // Unwind every caller, including a handler reacting to the first sentinel. + throw new ExitSignal(exitCode); + }, + get exitCode() { + return exitCode; + }, + set exitCode(value) { + if (value === undefined || value === null) { + exitCode = 0; + return; + } + const numeric = Number(value); + if (Number.isFinite(numeric)) exitCode = numeric; + }, + abort() {}, + stdout: { + write(chunk, callback) { + if (exited) return false; + outputs.push(String(chunk)); + if (typeof callback === 'function') callback(); + return true; + }, + }, + stderr: { + write(chunk, callback) { + if (exited) return false; + errors.push(String(chunk)); + if (typeof callback === 'function') callback(); + return true; + }, + }, +}; + +function forwardEventHandlers(type, value) { + const handlers = events.get(type); + if (!handlers || !handlers.length) return false; + for (const handler of handlers.slice()) { + try { + handler(value); + } catch (error) { + if (error instanceof ExitSignal) { + exited = true; + scheduleDrainCheck(); + return true; + } + // A throwing handler fails the request, exactly as Node does: an + // `uncaughtException` handler that throws exits with code 7, and any other + // handler that throws exits non-zero. Swallowing it here would answer + // `code: 0` for a request that really failed. + errors.push(formatError(error) + '\n'); + exitCode = type === 'uncaughtException' ? (exitCode || 7) : (exitCode || 1); + exited = true; + scheduleDrainCheck(); + return true; + } + } + return true; +} + +realProcess.on('unhandledRejection', reason => { + // A rejection carrying the exit sentinel is the unwinding of `process.exit`, not a + // failure of the request. Nothing reported after an exit happened, either. + if (reason instanceof ExitSignal || exited) return; + if (forwardEventHandlers('unhandledRejection', reason)) return; + // Node exits non-zero when a rejection has no listener. Without this the broker + // would answer `code: 0` and the failure would vanish, so fail the request loudly. + errors.push(formatError(reason) + '\n'); + exitCode = 1; + exited = true; + scheduleDrainCheck(); +}); + +realProcess.on('uncaughtException', error => { + if (error instanceof ExitSignal || exited) return; + if (forwardEventHandlers('uncaughtException', error)) return; + errors.push(formatError(error) + '\n'); + exitCode = 1; + exited = true; + scheduleDrainCheck(); +}); + +let drainResolve = null; +let drainCheckScheduled = false; + +function scheduleDrainCheck() { + if (!drainResolve || drainCheckScheduled) return; + drainCheckScheduled = true; + realSetImmediate(() => { + // One extra turn: an unhandled rejection raised in this check phase is reported + // after the callback returns, so the decision has to wait for it. + realSetImmediate(() => { + drainCheckScheduled = false; + if (drainResolve && (exited || timers.size === 0)) { + const resolve = drainResolve; + drainResolve = null; + resolve(); + } + }); + }); +} + +function waitForEventLoopDrain() { + // Always wait at least one event-loop turn, even with no pending timers, so a + // rejection settled in a microtask is delivered as unhandled before we answer. + return new Promise(resolve => { + drainResolve = resolve; + scheduleDrainCheck(); + }); +} + +function runCallback(callback, args) { + if (exited) return; + try { + callback(...args); + } catch (error) { + if (!(error instanceof ExitSignal)) throw error; + } finally { + scheduleDrainCheck(); + } +} + +const virtualFiles = new Map(Object.entries(request.files || {})); +const sources = new Map(Object.entries(request.sources || {})); +const realReadFileSync = fs.readFileSync; +const filename = String(request.filename || 'engraphis-test-request.js'); + +function runScript(code) { + return new vm.Script(code, { filename }).runInThisContext(); +} + +function readSource(target, options) { + const key = String(target); + const encoding = typeof options === 'string' + ? options + : (options && options.encoding) || null; + let buffer; + if (virtualFiles.has(key)) { + buffer = Buffer.from(String(virtualFiles.get(key)), 'utf8'); + } else if (sources.has(key)) { + const value = sources.get(key); + buffer = Buffer.isBuffer(value) ? value : Buffer.from(value); + } else { + return realReadFileSync(target, options); + } + if (encoding && encoding !== 'buffer') { + return buffer.toString(encoding === 'utf-8' ? 'utf8' : encoding); + } + return buffer; +} + +fs.readFileSync = readSource; + +globalThis.console = captureConsole; +globalThis.process = proc; +const customRequire = id => (id === 'process' || id === 'node:process' ? proc : require(id)); +Object.assign(customRequire, require); +globalThis.require = customRequire; +globalThis.setTimeout = (callback, delay, ...args) => { + const id = realSetTimeout(() => { + timers.delete(id); + runCallback(callback, args); + }, delay); + timers.set(id, () => realClearTimeout(id)); + return id; +}; +globalThis.clearTimeout = id => { + timers.delete(id); + realClearTimeout(id); + scheduleDrainCheck(); +}; +globalThis.setInterval = (callback, delay, ...args) => { + const id = realSetInterval(() => runCallback(callback, args), delay); + timers.set(id, () => realClearInterval(id)); + return id; +}; +globalThis.clearInterval = id => { + timers.delete(id); + realClearInterval(id); + scheduleDrainCheck(); +}; +globalThis.setImmediate = (callback, ...args) => { + const id = realSetImmediate(() => { + timers.delete(id); + runCallback(callback, args); + }); + timers.set(id, () => realClearImmediate(id)); + return id; +}; +globalThis.clearImmediate = id => { + timers.delete(id); + realClearImmediate(id); + scheduleDrainCheck(); +}; + +async function main() { + let failure = null; + try { + const code = String(request.prelude || '') + String(request.script || ''); + const result = runScript(code); + if (result && typeof result.then === 'function') await result; + await waitForEventLoopDrain(); + } catch (error) { + if (!(error instanceof ExitSignal)) { + failure = error; + // A top-level throw (or a rejected top-level script promise) is an uncaught + // exception: run the registered handlers, then make sure the request cannot + // report success. Only a handler that explicitly exits may pick the code. + if (!forwardEventHandlers('uncaughtException', error)) { + errors.push(formatError(error) + '\n'); + exitCode = exitCode || 1; + } else if (!exited && exitCode === 0) { + if (!errors.length) errors.push(formatError(error) + '\n'); + exitCode = 1; + } + } + } finally { + for (const cancel of timers.values()) { + try { + cancel(); + } catch (ignored) { + /* best-effort teardown */ + } + } + timers.clear(); + } + const failed = exitCode !== 0 || (failure !== null && !exited); + parentPort.postMessage({ + id: request.id, + error: failed && failure + ? formatError(failure) + : (failed ? 'node request exited with code ' + exitCode : undefined), + stdout: outputs.join(''), + stderr: errors.join(''), + code: exitCode, + }); +} + +main().catch(error => { + if (!(error instanceof ExitSignal)) { + // The async shell itself failed; treat it like an unhandled rejection of the + // request so the broker answers with a failure instead of dropping the line. + if (!forwardEventHandlers('unhandledRejection', error)) { + errors.push(formatError(error) + '\n'); + exitCode = exitCode || 1; + } else if (!exited && exitCode === 0) { + if (!errors.length) errors.push(formatError(error) + '\n'); + exitCode = 1; + } + } + parentPort.postMessage({ + id: request.id, + error: formatError(error), + stdout: outputs.join(''), + stderr: errors.join(''), + code: exitCode, + }); +}); diff --git a/tests/_node_worker.js b/tests/_node_worker.js new file mode 100644 index 000000000..0e7ba50ed --- /dev/null +++ b/tests/_node_worker.js @@ -0,0 +1,157 @@ +'use strict'; +/* + * Persistent request broker for the dashboard graph tests. + * + * The broker process reads one JSON request per line, runs it on a fresh Node + * `worker_thread` (its own isolate and `globalThis`, so no state leaks between + * calls), and writes one JSON response per line. Asset sources are read from disk + * once and handed to each thread, so the multi-thousand-line engines are never + * re-read; each request still compiles its own `vm.Script` inside the short-lived + * worker. A request timeout terminates a stuck thread without killing the broker. + */ + +const fs = require('fs'); +const path = require('path'); +const readline = require('readline'); +const { Worker } = require('worker_threads'); + +const THREAD = path.join(__dirname, '_node_thread.js'); +const sourceCache = new Map(); + +function sourceFor(target) { + const resolved = path.resolve(String(target)); + try { + const stat = fs.statSync(resolved); + if (!stat.isFile()) { + sourceCache.delete(resolved); + return null; + } + const cached = sourceCache.get(resolved); + if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) { + return cached.buffer; + } + const buffer = fs.readFileSync(resolved); + sourceCache.set(resolved, { buffer, mtimeMs: stat.mtimeMs, size: stat.size }); + return buffer; + } catch (error) { + // A miss is never cached: a file a test creates later must be read next time, + // and a file that changed on disk must not serve the previous bytes. + sourceCache.delete(resolved); + return null; + } +} + +function runRequest(request) { + return new Promise(resolve => { + const timeoutMs = Number(request.timeoutMs) > 0 ? Number(request.timeoutMs) : 300000; + const sources = {}; + for (const arg of request.args || []) { + const buffer = sourceFor(arg); + if (buffer) sources[String(arg)] = buffer; + } + + let settled = false; + let stderrText = ''; + let worker; + const finish = response => { + if (settled) return; + settled = true; + clearTimeout(timer); + resolve(response); + if (worker) { + try { + worker.terminate(); + } catch (ignored) { + /* already gone */ + } + } + }; + const timer = setTimeout(() => finish({ + id: request.id, + error: 'node request timed out after ' + timeoutMs + 'ms', + stdout: '', + stderr: stderrText, + }), timeoutMs); + + try { + worker = new Worker(THREAD, { + workerData: { request: Object.assign({}, request, { sources }) }, + stdout: true, + stderr: true, + }); + } catch (error) { + finish(errorResponse(request.id, error, stderrText)); + return; + } + + worker.stdout.on('data', () => {}); + worker.stderr.on('data', chunk => { + if (stderrText.length < 8192) stderrText += String(chunk); + }); + worker.on('message', finish); + worker.on('error', error => finish(errorResponse(request.id, error, stderrText))); + worker.on('exit', code => { + if (!settled) { + finish(errorResponse( + request.id, + 'node worker exited with code ' + code + + (stderrText ? '\n' + stderrText : ''), + stderrText, + )); + } + }); + }); +} + +function errorResponse(id, error, stderr) { + return { + id: id === undefined ? null : id, + error: error && (error.stack || error.message || String(error)), + stdout: '', + stderr: stderr || '', + }; +} + +function reportBrokerFailure(error) { + try { + process.stderr.write('node broker failure: ' + + (error && (error.stack || error.message || String(error))) + '\n'); + } catch (ignored) { + /* no channel left to report it */ + } +} + +let queue = Promise.resolve(); +const reader = readline.createInterface({ input: process.stdin, crlfDelay: Infinity }); +reader.on('line', line => { + if (!line.trim()) return; + let request = null; + queue = queue.then(async () => { + let response; + try { + request = JSON.parse(line); + response = await runRequest(request); + } catch (error) { + response = errorResponse(request && request.id, error); + } + process.stdout.write(JSON.stringify(response) + '\n'); + }).catch(error => { + // A rejected queue promise would skip every later request, so report the cause + // and answer this request instead of wedging the broker. + reportBrokerFailure(error); + try { + process.stdout.write( + JSON.stringify(errorResponse(request && request.id, error)) + '\n', + ); + } catch (ignored) { + /* stdout is gone; stderr already carries the cause */ + } + }); +}); +reader.on('error', error => { + reportBrokerFailure(error); + process.exit(1); +}); +reader.on('close', () => { + process.exit(0); +}); diff --git a/tests/test_galaxy_gravity_floor.py b/tests/test_galaxy_gravity_floor.py index c1322f1da..f98cd8635 100644 --- a/tests/test_galaxy_gravity_floor.py +++ b/tests/test_galaxy_gravity_floor.py @@ -17,11 +17,12 @@ import json import shutil -import subprocess from pathlib import Path import pytest +from tests._node_runner import run_json + ROOT = Path(__file__).resolve().parents[1] ASSET = ROOT / "engraphis" / "dashboard_assets" / "engraphis-graph.js" @@ -124,13 +125,7 @@ def _run(script: str): - result = subprocess.run( - [NODE, "-e", PRELUDE + script, str(ASSET)], - cwd=ROOT, - capture_output=True, text=True, check=False, - ) - assert result.returncode == 0, result.stderr - return json.loads(result.stdout.strip().splitlines()[-1]) + return run_json(PRELUDE + script, args=[str(ASSET)]) @requires_node diff --git a/tests/test_galaxy_gravity_slider.py b/tests/test_galaxy_gravity_slider.py index 2f627a6d2..51e204452 100644 --- a/tests/test_galaxy_gravity_slider.py +++ b/tests/test_galaxy_gravity_slider.py @@ -21,11 +21,12 @@ import json import shutil -import subprocess from pathlib import Path import pytest +from tests._node_runner import run_json + ROOT = Path(__file__).resolve().parents[1] ASSET = ROOT / "engraphis" / "dashboard_assets" / "engraphis-graph.js" LEDGER_ASSET = ROOT / "engraphis" / "dashboard_assets" / "ledger.js" @@ -138,19 +139,7 @@ def _run(script: str, env_extra: dict | None = None): - env = None - if env_extra: - import os - env = os.environ.copy() - env.update(env_extra) - result = subprocess.run( - [NODE, "-e", PRELUDE + script, str(ASSET)], - cwd=ROOT, - capture_output=True, text=True, check=False, - env=env, - ) - assert result.returncode == 0, result.stderr - return json.loads(result.stdout.strip().splitlines()[-1]) + return run_json(PRELUDE + script, args=[str(ASSET)], env=env_extra) @requires_node diff --git a/tests/test_galaxy_gravity_slider_no_dead_zone.py b/tests/test_galaxy_gravity_slider_no_dead_zone.py index 08c363f8e..cf7744822 100644 --- a/tests/test_galaxy_gravity_slider_no_dead_zone.py +++ b/tests/test_galaxy_gravity_slider_no_dead_zone.py @@ -31,11 +31,12 @@ import json import shutil -import subprocess from pathlib import Path import pytest +from tests._node_runner import run_json + ROOT = Path(__file__).resolve().parents[1] LEDGER_ASSET = ROOT / "engraphis" / "dashboard_assets" / "ledger.js" GRAPH_ASSET = ROOT / "engraphis" / "dashboard_assets" / "engraphis-graph.js" @@ -99,13 +100,7 @@ def _run(script: str): - result = subprocess.run( - [NODE, "-e", PRELUDE + script, str(GRAPH_ASSET)], - cwd=ROOT, - capture_output=True, text=True, check=False, - ) - assert result.returncode == 0, result.stderr - return json.loads(result.stdout.strip().splitlines()[-1]) + return run_json(PRELUDE + script, args=[str(GRAPH_ASSET)]) @requires_node diff --git a/tests/test_graph_engine_asset.py b/tests/test_graph_engine_asset.py index ffe642e15..83368f544 100644 --- a/tests/test_graph_engine_asset.py +++ b/tests/test_graph_engine_asset.py @@ -31,10 +31,11 @@ import subprocess from html.parser import HTMLParser from pathlib import Path -from tempfile import TemporaryDirectory import pytest +from tests._node_runner import run_json + ROOT = Path(__file__).resolve().parents[1] ASSETS = ROOT / "engraphis" / "dashboard_assets" ASSET = ASSETS / "engraphis-graph.js" @@ -128,15 +129,7 @@ def _run_node(script: str, prelude: str = PRELUDE) -> object: - result = subprocess.run( - [NODE, "-e", prelude + script, str(ASSET)], - cwd=ROOT, - capture_output=True, - text=True, - check=False, - ) - assert result.returncode == 0, result.stderr - return json.loads(result.stdout.strip().splitlines()[-1]) + return run_json(prelude + script, args=[str(ASSET)]) def _run_engine(script: str) -> object: @@ -150,15 +143,7 @@ def _run_spacetime_node(script: str) -> object: const source = fs.readFileSync(process.argv[1], 'utf8'); const emit = value => console.log(JSON.stringify(value)); """ - result = subprocess.run( - [NODE, "-e", prelude + script, str(SPACETIME_ASSET)], - cwd=ROOT, - capture_output=True, - text=True, - check=False, - ) - assert result.returncode == 0, result.stderr - return json.loads(result.stdout.strip().splitlines()[-1]) + return run_json(prelude + script, args=[str(SPACETIME_ASSET)]) # ── load order and failure isolation ──────────────────────────────────────────────── @@ -178,15 +163,7 @@ def _run_every_worker(script: str) -> object: }); const emit = value => console.log(JSON.stringify(value)); """ - result = subprocess.run( - [NODE, "-e", prelude + script, str(EVERY_WORKER)], - cwd=ROOT, - capture_output=True, - text=True, - check=False, - ) - assert result.returncode == 0, result.stderr - return json.loads(result.stdout.strip().splitlines()[-1]) + return run_json(prelude + script, args=[str(EVERY_WORKER)]) # ── load order and failure isolation ──────────────────────────────────────────────── @@ -431,15 +408,7 @@ def _run_loader(scenario: str) -> dict: + "(async () => {\n" + scenario + "\n})().catch(error => {\n" + " console.error(error && error.stack || error); process.exit(1);\n});" ) - result = subprocess.run( - [NODE, "-e", script, str(LEDGER)], - cwd=ROOT, - capture_output=True, - text=True, - check=False, - ) - assert result.returncode == 0, result.stderr - return json.loads(result.stdout.strip().splitlines()[-1]) + return run_json(script, args=[str(LEDGER)]) @requires_node @@ -521,12 +490,7 @@ def test_graph_assets_evaluate_without_each_other() -> None: files = [ASSETS / name for name in ( "engraphis-graph.js", "engraphis-spacetime.js", "engraphis-graph-every.js", )] - result = subprocess.run( - [NODE, "-e", script, *map(str, files)], - cwd=ROOT, capture_output=True, text=True, check=False, - ) - assert result.returncode == 0, result.stderr - registered = json.loads(result.stdout.strip().splitlines()[-1]) + registered = run_json(script, args=[str(file) for file in files]) assert "EngraphisGraph" in registered["engraphis-graph.js"] assert registered["engraphis-spacetime.js"] == ["EngraphisSpacetime"] assert "EngraphisEveryGraph" in registered["engraphis-graph-every.js"] @@ -10178,28 +10142,32 @@ def handle_endtag(self, tag) -> None: self._select = None +_LEDGER_MARKUP_JSON = "" + + +def _ledger_markup_json() -> str: + global _LEDGER_MARKUP_JSON + if not _LEDGER_MARKUP_JSON: + markup = _LedgerMarkup() + markup.feed(LEDGER_INDEX.read_text(encoding="utf-8")) + _LEDGER_MARKUP_JSON = json.dumps(markup.elements) + return _LEDGER_MARKUP_JSON + + def _run_ledger(scenario: str) -> dict: - markup = _LedgerMarkup() - markup.feed(LEDGER_INDEX.read_text(encoding="utf-8")) + markup_path = "" script = ( - "process.argv.splice(1, 1);\n" + LEDGER_HARNESS + LEDGER_HARNESS + "(async () => {\n" + scenario + "\n})().catch(error => {\n" + " console.error(error && error.stack || error); process.exit(1);\n});" ) - # Large dashboard fixtures exceed Windows' command-line limit. Keep the harness - # and its parsed markup in temporary UTF-8 files on every platform. - with TemporaryDirectory(prefix="engraphis-ledger-test-") as directory: - script_path = Path(directory) / "ledger.js" - markup_path = Path(directory) / "markup.json" - script_path.write_text(script, encoding="utf-8") - markup_path.write_text(json.dumps(markup.elements), encoding="utf-8") - result = subprocess.run( - [NODE, str(script_path), str(ASSET), str(LEDGER), str(GRAPH_LIFECYCLE), - str(LEDGER_CSS), str(markup_path)], - cwd=ROOT, capture_output=True, encoding="utf-8", check=False, - ) - assert result.returncode == 0, result.stderr - return json.loads(result.stdout.strip().splitlines()[-1]) + return run_json( + script, + args=[ + str(ASSET), str(LEDGER), str(GRAPH_LIFECYCLE), str(LEDGER_CSS), markup_path, + ], + files={markup_path: _ledger_markup_json()}, + ) def _ledger_theme(theme: str) -> dict: diff --git a/tests/test_node_runner_broker.py b/tests/test_node_runner_broker.py new file mode 100644 index 000000000..5ea9e8dc6 --- /dev/null +++ b/tests/test_node_runner_broker.py @@ -0,0 +1,232 @@ +"""Regression checks for the persistent Node broker used by the dashboard graph tests. + +They cover the failure modes that must match standalone ``node``: an unhandled +rejection with no listener fails the request and reports it, a registered listener +handles it without failing, ``process.exit`` stops the request with its code, and a +listener that itself throws fails the request instead of hiding the error. +""" +from __future__ import annotations + +import json +import shutil +import threading +import time +from pathlib import Path + +import pytest + +from tests._node_runner import NodeExecutionError, _shared_worker, run_node + +NODE = shutil.which("node") +requires_node = pytest.mark.skipif(NODE is None, reason="node is not installed") + + +def _failed_response(script: str) -> dict: + with pytest.raises(NodeExecutionError) as captured: + run_node(script, timeout=10) + response = captured.value.response + assert response is not None, "the broker did not return a response" + return response + + +@requires_node +def test_an_unhandled_rejection_without_a_listener_fails_the_request() -> None: + """Node exits non-zero on an unhandled rejection; the broker must not hide it.""" + response = _failed_response( + """ + setTimeout(() => { + Promise.reject(new Error('unhandled-boom')); + }, 1); + """ + ) + assert response["code"] == 1 + assert "unhandled-boom" in response["stderr"] + + +@requires_node +def test_a_registered_unhandled_rejection_listener_answers_without_failing() -> None: + response = run_node( + """ + process.on('unhandledRejection', reason => { + console.error('handled: ' + (reason && reason.message)); + }); + setTimeout(() => { + Promise.reject(new Error('handled-boom')); + }, 1); + setTimeout(() => { + console.log('tail'); + }, 20); + """, + timeout=10, + ) + assert response["code"] == 0 + assert "error" not in response + assert "handled: handled-boom" in response["stderr"] + assert "tail" in response["stdout"] + + +@requires_node +def test_process_exit_stops_the_request_and_preserves_its_code() -> None: + top_level = _failed_response( + """ + console.log('before'); + process.exit(2); + console.log('after'); + """ + ) + assert top_level["code"] == 2 + assert top_level["stdout"] == "before\n" + + callback = _failed_response( + """ + setTimeout(() => { + process.exit(2); + console.log('after'); + }, 1); + setTimeout(() => { + console.log('later'); + }, 50); + """ + ) + assert callback["code"] == 2 + assert callback["stdout"] == "" + + +@requires_node +def test_console_writes_are_captured_and_errors_use_the_request_filename() -> None: + response = run_node( + """ + console.warn('warned'); + console.error('failed'); + console.log('logged'); + """ + ) + assert response["code"] == 0 + assert response["stdout"] == "logged\n" + assert response["stderr"] == "warned\nfailed\n" + + with pytest.raises(NodeExecutionError) as captured: + run_node("throw new Error('frame-check');", filename="custom-request.js") + assert "custom-request.js" in str(captured.value) + + +@requires_node +def test_a_throwing_unhandled_rejection_handler_fails_the_request() -> None: + """A handler that throws must not turn a failed request into ``code: 0``.""" + response = _failed_response( + """ + process.on('unhandledRejection', () => { + throw new Error('handler-boom'); + }); + setTimeout(() => { + Promise.reject(new Error('source-boom')); + }, 1); + """ + ) + assert response["code"] == 1 + assert "handler-boom" in response["stderr"] + + +@requires_node +def test_a_throwing_uncaught_exception_handler_exits_with_code_7() -> None: + response = _failed_response( + """ + process.on('uncaughtException', () => { + throw new Error('uncaught-handler-boom'); + }); + setTimeout(() => { + throw new Error('source-boom'); + }, 1); + """ + ) + assert response["code"] == 7 + assert "uncaught-handler-boom" in response["stderr"] + + +@requires_node +def test_a_rejected_top_level_promise_cannot_succeed() -> None: + """A rejected top-level script promise is a failure even with no listener.""" + response = _failed_response("Promise.reject(new Error('top-boom'));") + assert response["code"] == 1 + assert "top-boom" in response["stderr"] + + +@requires_node +def test_a_mismatched_broker_response_fails_fast_and_the_broker_respawns() -> None: + """A fatal ``{"id": null, ...}`` line must fail the call instead of being waited out.""" + try: + _shared_worker()._responses.put( + json.dumps({"id": None, "ok": False, "error": "fatal broker"}) + "\n" + ) + started = time.monotonic() + with pytest.raises(NodeExecutionError) as captured: + run_node("console.log('never');", timeout=5.0) + assert time.monotonic() - started < 4.5 + assert "Mismatched response id" in str(captured.value) + assert captured.value.response is not None + assert captured.value.response["id"] is None + + revived = run_node("console.log('revived');", timeout=10.0) + assert revived["code"] == 0 + assert revived["stdout"] == "revived\n" + finally: + _shared_worker()._stop() + + +@requires_node +def test_a_broker_that_dies_mid_request_fails_fast_and_the_broker_respawns( + tmp_path: Path, +) -> None: + """A broker killed under a live request must not stall the caller for the timeout.""" + marker = tmp_path / "request-started" + outcome: dict = {} + try: + script = ( + "require('fs').writeFileSync(%s, 'started');\n" + "setTimeout(() => {}, 30000);\n" % json.dumps(str(marker)) + ) + + def call() -> None: + try: + run_node(script, timeout=30.0) + except Exception as error: + outcome["error"] = error + + thread = threading.Thread(target=call, daemon=True) + thread.start() + deadline = time.monotonic() + 10.0 + while not marker.exists() and time.monotonic() < deadline: + time.sleep(0.01) + assert marker.exists(), "the broker never started the request" + + started = time.monotonic() + _shared_worker()._proc.kill() + thread.join(timeout=10.0) + assert not thread.is_alive(), "run_node() kept waiting on a dead broker" + assert time.monotonic() - started < 5.0 + error = outcome.get("error") + assert isinstance(error, NodeExecutionError), repr(error) + assert error.response is not None + assert error.response["code"] != 0 + assert "exited unexpectedly" in str(error) + + revived = run_node("console.log('revived');", timeout=10.0) + assert revived["code"] == 0 + assert revived["stdout"] == "revived\n" + finally: + _shared_worker()._stop() + + +@requires_node +def test_setting_process_exit_code_and_requiring_process_honor_the_request_shim() -> None: + """Setting process.exitCode or calling require('process') must use the per-request shim.""" + with pytest.raises(NodeExecutionError) as exc_info: + run_node( + "require('process').stdout.write('before-exit-code\\n');\n" + "process.exitCode = 3;\n", + timeout=10.0, + ) + assert exc_info.value.response is not None + assert exc_info.value.response["code"] == 3 + assert exc_info.value.response["stdout"] == "before-exit-code\n" + From b5839cf8558f61c63d114c659de7c4cb5c6c0ad4 Mon Sep 17 00:00:00 2001 From: Coding-Dev-Tools <274649000+Coding-Dev-Tools@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:23:27 -0500 Subject: [PATCH 2/2] fix(tests): harden persistent Node broker pipe cleanup, diagnostics, and timeout handling Close stdout/stderr pipes and join pump threads cleanly in _Worker._stop() (eliminating ResourceWarnings), enqueue an stdout EOF sentinel, preserve broker stderr diagnostics on unexpected exit, guard non-object/interrupted frames, give Node worker timeouts a 1s grace buffer, preserve process.exitCode on bare process.exit(), support 3-arg stream writes, and add regression coverage. --- tests/_node_runner.py | 179 +++++++++++++++++++++---------- tests/_node_thread.js | 42 ++++---- tests/test_node_runner_broker.py | 150 +++++++++++++++++++++++++- 3 files changed, 293 insertions(+), 78 deletions(-) diff --git a/tests/_node_runner.py b/tests/_node_runner.py index 45eb71284..e82d328d6 100644 --- a/tests/_node_runner.py +++ b/tests/_node_runner.py @@ -41,6 +41,8 @@ class _Worker: def __init__(self) -> None: self._lock = threading.RLock() self._proc: Optional[subprocess.Popen] = None + self._stdout_thread: Optional[threading.Thread] = None + self._stderr_thread: Optional[threading.Thread] = None self._responses: queue.Queue = queue.Queue() self._stderr: collections.deque = collections.deque(maxlen=200) self._stderr_lock = threading.Lock() @@ -76,25 +78,38 @@ def capture_stderr(line: str) -> None: with stderr_lock: stderr.append(line) - threading.Thread( - target=self._pump, args=(proc.stdout, responses.put), daemon=True, + self._stdout_thread = threading.Thread( + target=self._pump, + args=(proc.stdout, responses.put), + kwargs={"on_eof": lambda: responses.put(None)}, + daemon=True, name="engraphis-node-stdout", - ).start() - threading.Thread( - target=self._pump, args=(proc.stderr, capture_stderr), daemon=True, + ) + self._stderr_thread = threading.Thread( + target=self._pump, + args=(proc.stderr, capture_stderr), + daemon=True, name="engraphis-node-stderr", - ).start() + ) + self._stdout_thread.start() + self._stderr_thread.start() @staticmethod - def _pump(stream, sink) -> None: + def _pump(stream, sink, *, on_eof=None) -> None: try: - for line in stream: - sink(line) + if stream is not None: + for line in stream: + sink(line) except (ValueError, OSError): pass + finally: + if on_eof is not None: + on_eof() def _stop(self) -> None: proc, self._proc = self._proc, None + stdout_thread, self._stdout_thread = self._stdout_thread, None + stderr_thread, self._stderr_thread = self._stderr_thread, None if proc is None: return try: @@ -110,6 +125,42 @@ def _stop(self) -> None: proc.wait(timeout=2) except (OSError, subprocess.TimeoutExpired): pass + for thread in (stdout_thread, stderr_thread): + if thread is not None: + thread.join(timeout=1.0) + for stream in (proc.stdout, proc.stderr): + if stream is not None: + try: + stream.close() + except (OSError, ValueError): + pass + + def _unexpected_exit_error( + self, proc: subprocess.Popen, req_id: int + ) -> NodeExecutionError: + try: + proc.wait(timeout=1.0) + except subprocess.TimeoutExpired: + pass + rc = proc.returncode + for thread in (self._stdout_thread, self._stderr_thread): + if thread is not None: + thread.join(timeout=0.5) + with self._stderr_lock: + stderr_text = "".join(self._stderr) + detail = "node broker exited unexpectedly with code %s" % rc + if stderr_text: + detail += "\n" + stderr_text + return NodeExecutionError( + detail, + { + "id": req_id, + "ok": False, + "code": rc, + "stdout": "", + "stderr": stderr_text, + }, + ) def _terminate(self) -> None: """Kill the broker without waiting for an in-flight request.""" @@ -127,10 +178,13 @@ def run(self, payload: dict, *, timeout: float) -> dict: with self._lock: self._seq += 1 payload = dict(payload, id=self._seq) + proc: Optional[subprocess.Popen] = None for attempt in range(2): if not self.alive(): self._stop() self._start() + with self._stderr_lock: + self._stderr.clear() try: proc = self._proc if proc is None or proc.stdin is None: @@ -142,55 +196,66 @@ def run(self, payload: dict, *, timeout: float) -> dict: if attempt: raise NodeExecutionError("node worker is unavailable") from None self._stop() + assert proc is not None req_id = payload["id"] - deadline = time.monotonic() + timeout - while True: - remaining = deadline - time.monotonic() - if remaining <= 0: - self._stop() - raise subprocess.TimeoutExpired( - cmd=["node", str(WORKER)], timeout=timeout - ) - try: - raw_line = self._responses.get(timeout=min(0.25, remaining)) - break - except queue.Empty: - # A broker that died mid-request would otherwise burn the whole - # timeout waiting on a line that can never arrive. - if proc.poll() is not None and self._responses.empty(): - rc = proc.returncode - self._stop() - raise NodeExecutionError( - "node broker exited unexpectedly with code %s" % rc, - {"id": req_id, "ok": False, "code": rc, - "stdout": "", "stderr": ""}, - ) from None + # Give the broker's per-worker timer a short grace window to terminate a stuck + # request thread without tearing down the persistent broker unnecessarily. + deadline = time.monotonic() + max(0.0, float(timeout)) + 1.0 + frame_ok = False try: - response = json.loads(raw_line) - except json.JSONDecodeError: - self._stop() - raise NodeExecutionError( - "Invalid JSON from node broker: %s" % raw_line[:200], - {"id": req_id, "ok": False, "code": 1, - "stdout": "", "stderr": raw_line}, - ) from None - if response.get("id") != req_id: - self._stop() - raise NodeExecutionError( - "Mismatched response id from node broker (expected %s, got %s): %s" - % (req_id, response.get("id"), response.get("stderr", "")), - response, - ) - if response.get("error"): - detail = str(response["error"]) - with self._stderr_lock: - stderr_lines = list(self._stderr) - stderr = response.get("stderr") or "".join(stderr_lines) - # A top-level failure is reported in both fields; do not repeat it. - if stderr and stderr.strip() not in detail: - detail += "\n" + stderr - raise NodeExecutionError(detail, response) - return response + while True: + remaining = deadline - time.monotonic() + if remaining <= 0: + raise subprocess.TimeoutExpired( + cmd=["node", str(WORKER)], timeout=timeout + ) + try: + raw_line = self._responses.get(timeout=min(0.25, remaining)) + except queue.Empty: + if proc.poll() is not None: + if self._stdout_thread is not None: + self._stdout_thread.join(timeout=0.5) + if not self._responses.empty(): + continue + raise self._unexpected_exit_error(proc, req_id) from None + continue + if raw_line is None: + raise self._unexpected_exit_error(proc, req_id) from None + break + try: + response = json.loads(raw_line) + except json.JSONDecodeError: + raise NodeExecutionError( + "Invalid JSON from node broker: %s" % raw_line[:200], + {"id": req_id, "ok": False, "code": 1, + "stdout": "", "stderr": raw_line}, + ) from None + if not isinstance(response, dict): + raise NodeExecutionError( + "Malformed non-object response from node broker: %r" % (response,), + {"id": req_id, "ok": False, "code": 1, + "stdout": "", "stderr": str(response)}, + ) + if response.get("id") != req_id: + raise NodeExecutionError( + "Mismatched response id from node broker (expected %s, got %s): %s" + % (req_id, response.get("id"), response.get("stderr", "")), + response, + ) + frame_ok = True + if response.get("error"): + detail = str(response["error"]) + with self._stderr_lock: + stderr_lines = list(self._stderr) + stderr = response.get("stderr") or "".join(stderr_lines) + # A top-level failure is reported in both fields; do not repeat it. + if stderr and stderr.strip() not in detail: + detail += "\n" + stderr + raise NodeExecutionError(detail, response) + return response + finally: + if not frame_ok: + self._stop() _worker: Optional[_Worker] = None @@ -248,7 +313,7 @@ def run_node( "args": list(args or ()), "env": dict(env or {}), "files": dict(files or {}), - "timeoutMs": int(timeout * 1000), + "timeoutMs": max(1, int(timeout * 1000)), "filename": filename, } return _shared_worker().run(payload, timeout=timeout) diff --git a/tests/_node_thread.js b/tests/_node_thread.js index 425ec9d9e..85a3def6d 100644 --- a/tests/_node_thread.js +++ b/tests/_node_thread.js @@ -10,6 +10,8 @@ const { parentPort, workerData } = require('worker_threads'); const fs = require('fs'); +const { createRequire } = require('module'); +const path = require('path'); const util = require('util'); const vm = require('vm'); @@ -54,6 +56,18 @@ const captureConsole = { error: (...args) => { if (!exited) errors.push(format(args) + '\n'); }, }; +function makeStreamWriter(target) { + return { + write(chunk, encodingOrCallback, callback) { + if (exited) return false; + target.push(Buffer.isBuffer(chunk) ? chunk.toString('utf8') : String(chunk)); + const cb = typeof encodingOrCallback === 'function' ? encodingOrCallback : callback; + if (typeof cb === 'function') cb(); + return true; + }, + }; +} + const proc = { argv: ['node'].concat(request.args || []), env: Object.assign({}, realProcess.env, request.env || {}), @@ -100,7 +114,10 @@ const proc = { }, exit(code) { if (!exited) { - exitCode = code === undefined ? 0 : Number(code); + if (code !== undefined) { + const numeric = code === null ? 0 : Number(code); + exitCode = Number.isFinite(numeric) ? numeric : 1; + } exited = true; scheduleDrainCheck(); } @@ -119,22 +136,8 @@ const proc = { if (Number.isFinite(numeric)) exitCode = numeric; }, abort() {}, - stdout: { - write(chunk, callback) { - if (exited) return false; - outputs.push(String(chunk)); - if (typeof callback === 'function') callback(); - return true; - }, - }, - stderr: { - write(chunk, callback) { - if (exited) return false; - errors.push(String(chunk)); - if (typeof callback === 'function') callback(); - return true; - }, - }, + stdout: makeStreamWriter(outputs), + stderr: makeStreamWriter(errors), }; function forwardEventHandlers(type, value) { @@ -258,8 +261,9 @@ fs.readFileSync = readSource; globalThis.console = captureConsole; globalThis.process = proc; -const customRequire = id => (id === 'process' || id === 'node:process' ? proc : require(id)); -Object.assign(customRequire, require); +const cwdRequire = createRequire(path.join(realProcess.cwd(), '__engraphis_request__.js')); +const customRequire = id => (id === 'process' || id === 'node:process' ? proc : cwdRequire(id)); +Object.assign(customRequire, cwdRequire); globalThis.require = customRequire; globalThis.setTimeout = (callback, delay, ...args) => { const id = realSetTimeout(() => { diff --git a/tests/test_node_runner_broker.py b/tests/test_node_runner_broker.py index 5ea9e8dc6..f6d1c831e 100644 --- a/tests/test_node_runner_broker.py +++ b/tests/test_node_runner_broker.py @@ -7,15 +7,18 @@ """ from __future__ import annotations +import gc import json +import os import shutil import threading import time +import warnings from pathlib import Path import pytest -from tests._node_runner import NodeExecutionError, _shared_worker, run_node +from tests._node_runner import NodeExecutionError, _shared_worker, _shutdown, run_node NODE = shutil.which("node") requires_node = pytest.mark.skipif(NODE is None, reason="node is not installed") @@ -200,7 +203,9 @@ def call() -> None: assert marker.exists(), "the broker never started the request" started = time.monotonic() - _shared_worker()._proc.kill() + proc = _shared_worker()._proc + assert proc is not None, "the broker process was not started" + proc.kill() thread.join(timeout=10.0) assert not thread.is_alive(), "run_node() kept waiting on a dead broker" assert time.monotonic() - started < 5.0 @@ -230,3 +235,144 @@ def test_setting_process_exit_code_and_requiring_process_honor_the_request_shim( assert exc_info.value.response["code"] == 3 assert exc_info.value.response["stdout"] == "before-exit-code\n" + +@requires_node +def test_bare_process_exit_preserves_exit_code_and_three_arg_write_calls_callback() -> None: + """`process.exit()` preserves preset `exitCode`, and 3-arg stream writes call `cb`.""" + try: + with pytest.raises(NodeExecutionError) as exc_info: + run_node( + "process.stdout.write('part1-', 'utf8', () => process.stdout.write('cb'));\n" + "process.exitCode = 5;\n" + "process.exit();\n", + timeout=10.0, + ) + assert exc_info.value.response is not None + assert exc_info.value.response["code"] == 5 + assert exc_info.value.response["stdout"] == "part1-cb" + finally: + _shutdown() + + +@requires_node +def test_worker_isolates_globals_env_and_recovers_from_thread_timeout_and_bad_message() -> None: + """Requests cannot leak globals/env, and worker timeouts leave the broker healthy.""" + try: + r1 = run_node( + "globalThis.leakedVar = 42;\n" + "Object.prototype.pollutedProp = 99;\n" + "Math.random = () => 0.123;\n" + "console.log(process.env.BROKER_TEST_ENV);\n", + env={"BROKER_TEST_ENV": "only-request-1"}, + timeout=10.0, + ) + assert r1["stdout"] == "only-request-1\n" + + r2 = run_node( + "console.log(JSON.stringify({" + "leaked: globalThis.leakedVar === undefined," + "protoOk: ({}).pollutedProp === undefined," + "randOk: Math.random() !== 0.123," + "envOk: process.env.BROKER_TEST_ENV === undefined" + "}));", + timeout=10.0, + ) + assert json.loads(r2["stdout"]) == { + "leaked": True, + "protoOk": True, + "randOk": True, + "envOk": True, + } + + with pytest.raises(NodeExecutionError) as timeout_exc: + run_node("while (true) {}", timeout=0.4) + assert "timed out" in str(timeout_exc.value) + assert _shared_worker().alive(), "worker thread timeout should keep broker alive" + + with pytest.raises(NodeExecutionError) as bad_msg: + run_node("require('worker_threads').parentPort.postMessage(null);", timeout=10.0) + assert "Malformed" in str(bad_msg.value) + + recovered = run_node("console.log('healthy');", timeout=10.0) + assert recovered["stdout"] == "healthy\n" + finally: + _shutdown() + + +@requires_node +def test_source_cache_invalidates_when_disk_asset_changes(tmp_path: Path) -> None: + """Updating an argument file between calls must not serve stale cached bytes.""" + asset = tmp_path / "asset.js" + asset.write_text("v1", encoding="utf-8") + try: + r1 = run_node( + "console.log(require('fs').readFileSync(process.argv[1], 'utf8'));", + args=[str(asset)], + timeout=10.0, + ) + assert r1["stdout"] == "v1\n" + + asset.write_text("version-two-updated", encoding="utf-8") + stat = asset.stat() + os.utime(asset, ns=(stat.st_atime_ns, stat.st_mtime_ns + 2_000_000_000)) + r2 = run_node( + "console.log(require('fs').readFileSync(process.argv[1], 'utf8'));", + args=[str(asset)], + timeout=10.0, + ) + assert r2["stdout"] == "version-two-updated\n" + finally: + _shutdown() + + +@requires_node +def test_unexpected_exit_preserves_stderr_and_stop_closes_pipes_cleanly( + tmp_path: Path, +) -> None: + """Abrupt broker crashes retain stderr diagnostics and _stop/_shutdown leak no pipes.""" + marker = tmp_path / "started" + outcome: dict = {} + try: + with warnings.catch_warnings(): + warnings.simplefilter("error", ResourceWarning) + worker = _shared_worker() + run_node("console.log('warm');", timeout=10.0) + worker._stop() + gc.collect() + run_node("console.log('respawned');", timeout=10.0) + _shutdown() + gc.collect() + + # Verify stderr collected prior to an unexpected mid-request death surfaces. + worker = _shared_worker() + script = ( + "require('fs').writeFileSync(%s, 'go');\n" + "setTimeout(() => {}, 30000);\n" % json.dumps(str(marker)) + ) + + def call() -> None: + try: + run_node(script, timeout=10.0) + except Exception as exc: + outcome["error"] = exc + + thread = threading.Thread(target=call, daemon=True) + thread.start() + deadline = time.monotonic() + 10.0 + while not marker.exists() and time.monotonic() < deadline: + time.sleep(0.01) + assert marker.exists(), "broker never started request" + with worker._stderr_lock: + worker._stderr.append("fatal-broker-diagnostic\n") + proc = worker._proc + assert proc is not None + proc.kill() + thread.join(timeout=10.0) + err = outcome.get("error") + assert isinstance(err, NodeExecutionError) + assert err.response is not None + assert "fatal-broker-diagnostic" in str(err) + assert "fatal-broker-diagnostic" in err.response["stderr"] + finally: + _shutdown() +