From 3b2a5982a22f5c1c7080cf550354c50180babcb1 Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Fri, 27 Mar 2026 17:19:43 +0000 Subject: [PATCH] fix(cli,chromium-runtime): clean up long-lived test runner resources after runs to prevent hanging processes --- changelog.md | 6 + ts/00_commitinfo_data.ts | 2 +- ts/index.ts | 1 + ts/tstest.classes.runtime.chromium.ts | 3 + ts/tstest.classes.tstest.ts | 344 +------------------------- 5 files changed, 19 insertions(+), 337 deletions(-) diff --git a/changelog.md b/changelog.md index 6ab1ef6..8c610ac 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## 2026-03-27 - 3.6.2 - fix(cli,chromium-runtime) +clean up long-lived test runner resources after runs to prevent hanging processes + +- call TsTest.cleanup() after non-watch CLI runs so smartexit handlers are deregistered +- terminate active WebSocket clients before closing the Chromium runtime server to release open connections cleanly + ## 2026-03-26 - 3.6.1 - fix(chromium runtime) encode Chromium test bundle names correctly for nested file paths diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 38f40a9..6ef5b06 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@git.zone/tstest', - version: '3.6.1', + version: '3.6.2', description: 'A powerful, modern test runner for TypeScript with multi-runtime support (Node.js, Deno, Bun, Chromium) and a batteries-included test framework.' } diff --git a/ts/index.ts b/ts/index.ts index 806f01c..fb31cde 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -193,6 +193,7 @@ export const runCli = async () => { await tsTestInstance.runWatch(watchIgnorePatterns); } else { await tsTestInstance.run(); + await tsTestInstance.cleanup(); } }; diff --git a/ts/tstest.classes.runtime.chromium.ts b/ts/tstest.classes.runtime.chromium.ts index 2216c8c..eacb0fb 100644 --- a/ts/tstest.classes.runtime.chromium.ts +++ b/ts/tstest.classes.runtime.chromium.ts @@ -277,6 +277,9 @@ export class ChromiumRuntimeAdapter extends RuntimeAdapter { } try { + for (const client of wss.clients) { + client.terminate(); + } wss.close(); } catch (error) { // WebSocket server might already be closed diff --git a/ts/tstest.classes.tstest.ts b/ts/tstest.classes.tstest.ts index 363a47f..b39e421 100644 --- a/ts/tstest.classes.tstest.ts +++ b/ts/tstest.classes.tstest.ts @@ -158,7 +158,14 @@ export class TsTest { tapCombinator.evaluate(); } - + + /** + * Clean up all long-lived resources so the Node.js event loop can drain naturally. + */ + public async cleanup() { + this.smartshellInstance.smartexit.deregister(); + } + public async runWatch(ignorePatterns: string[] = []) { const smartwatchInstance = new plugins.smartwatch.Smartwatch([this.testDir.cwd]); @@ -386,341 +393,6 @@ export class TsTest { } } - public async runInNode(fileNameArg: string, index: number, total: number): Promise { - this.logger.testFileStart(fileNameArg, 'node.js', index, total); - const tapParser = new TapParser(fileNameArg + ':node', this.logger); - - // tsrun options - let tsrunOptions = ''; - if (process.argv.includes('--web')) { - tsrunOptions += ' --web'; - } - - // Set filter tags as environment variable - if (this.filterTags.length > 0) { - process.env.TSTEST_FILTER_TAGS = this.filterTags.join(','); - } - - // Check for 00init.ts file in test directory - const testDir = plugins.path.dirname(fileNameArg); - const initFile = plugins.path.join(testDir, '00init.ts'); - let runCommand = `tsrun ${fileNameArg}${tsrunOptions}`; - - const initFileExists = await plugins.smartfsInstance.file(initFile).exists(); - - // If 00init.ts exists, run it first - if (initFileExists) { - // Create a temporary loader file that imports both 00init.ts and the test file - const absoluteInitFile = plugins.path.resolve(initFile); - const absoluteTestFile = plugins.path.resolve(fileNameArg); - const loaderContent = ` -import '${absoluteInitFile.replace(/\\/g, '/')}'; -import '${absoluteTestFile.replace(/\\/g, '/')}'; -`; - const loaderPath = plugins.path.join(testDir, `.loader_${plugins.path.basename(fileNameArg)}`); - await plugins.smartfsInstance.file(loaderPath).write(loaderContent); - runCommand = `tsrun ${loaderPath}${tsrunOptions}`; - } - - const execResultStreaming = await this.smartshellInstance.execStreamingSilent(runCommand); - - // If we created a loader file, clean it up after test execution - if (initFileExists) { - const loaderPath = plugins.path.join(testDir, `.loader_${plugins.path.basename(fileNameArg)}`); - const cleanup = () => { - try { - if (plugins.fs.existsSync(loaderPath)) { - plugins.fs.rmSync(loaderPath, { force: true }); - } - } catch (e) { - // Ignore cleanup errors - } - }; - - execResultStreaming.childProcess.on('exit', cleanup); - execResultStreaming.childProcess.on('error', cleanup); - } - - // Start warning timer if no timeout was specified - let warningTimer: NodeJS.Timeout | null = null; - if (this.timeoutSeconds === null) { - warningTimer = setTimeout(() => { - console.error(''); - console.error(cs('⚠️ WARNING: Test file is running for more than 1 minute', 'orange')); - console.error(cs(` File: ${fileNameArg}`, 'orange')); - console.error(cs(' Consider using --timeout option to set a timeout for test files.', 'orange')); - console.error(cs(' Example: tstest test --timeout=300 (for 5 minutes)', 'orange')); - console.error(''); - }, 60000); // 1 minute - } - - // Handle timeout if specified - if (this.timeoutSeconds !== null) { - const timeoutMs = this.timeoutSeconds * 1000; - let timeoutId: NodeJS.Timeout; - - const timeoutPromise = new Promise((_resolve, reject) => { - timeoutId = setTimeout(async () => { - // Use smartshell's terminate() to kill entire process tree - await execResultStreaming.terminate(); - reject(new Error(`Test file timed out after ${this.timeoutSeconds} seconds`)); - }, timeoutMs); - }); - - try { - await Promise.race([ - tapParser.handleTapProcess(execResultStreaming.childProcess), - timeoutPromise - ]); - // Clear timeout if test completed successfully - clearTimeout(timeoutId); - } catch (error) { - // Clear warning timer if it was set - if (warningTimer) { - clearTimeout(warningTimer); - } - // Handle timeout error - tapParser.handleTimeout(this.timeoutSeconds); - // Ensure entire process tree is killed if still running - try { - await execResultStreaming.kill(); // This kills the entire process tree with SIGKILL - } catch (killError) { - // Process tree might already be dead - } - await tapParser.evaluateFinalResult(); - } - } else { - await tapParser.handleTapProcess(execResultStreaming.childProcess); - } - - // Clear warning timer if it was set - if (warningTimer) { - clearTimeout(warningTimer); - } - - return tapParser; - } - - private async findFreePorts(): Promise<{ httpPort: number; wsPort: number }> { - const smartnetwork = new plugins.smartnetwork.SmartNetwork(); - - // Find random free HTTP port in range 30000-40000 to minimize collision chance - const httpPort = await smartnetwork.findFreePort(30000, 40000, { randomize: true }); - if (!httpPort) { - throw new Error('Could not find a free HTTP port in range 30000-40000'); - } - - // Find random free WebSocket port, excluding the HTTP port to ensure they're different - const wsPort = await smartnetwork.findFreePort(30000, 40000, { - randomize: true, - exclude: [httpPort] - }); - if (!wsPort) { - throw new Error('Could not find a free WebSocket port in range 30000-40000'); - } - - // Log selected ports for debugging - if (!this.logger.options.quiet) { - console.log(`Selected ports - HTTP: ${httpPort}, WebSocket: ${wsPort}`); - } - return { httpPort, wsPort }; - } - - public async runInChrome(fileNameArg: string, index: number, total: number): Promise { - this.logger.testFileStart(fileNameArg, 'chromium', index, total); - - // lets get all our paths sorted - const tsbundleCacheDirPath = plugins.path.join(paths.cwd, './.nogit/tstest_cache'); - const bundleFileName = fileNameArg.replaceAll('/', '__') + '.js'; - const bundleFilePath = plugins.path.join(tsbundleCacheDirPath, bundleFileName); - - // lets bundle the test - try { await plugins.smartfsInstance.directory(tsbundleCacheDirPath).recursive().delete(); } catch (e) { /* may not exist */ } - await plugins.smartfsInstance.directory(tsbundleCacheDirPath).recursive().create(); - await this.tsbundleInstance.build(process.cwd(), fileNameArg, bundleFilePath, { - bundler: 'esbuild', - }); - - // Find free ports for HTTP and WebSocket - const { httpPort, wsPort } = await this.findFreePorts(); - - // Use SmartServe with setHandler() to bypass global ControllerRegistry - const fileServer = new plugins.smartserve.FileServer({ root: tsbundleCacheDirPath }); - const server = new plugins.smartserve.SmartServe({ port: httpPort }); - server.setHandler(async (request: Request) => { - const url = new URL(request.url); - if (url.pathname === '/test') { - return new Response(` - - - - - - - `, { headers: { 'Content-Type': 'text/html' } }); - } - const staticResponse = await fileServer.serve(request); - if (staticResponse) return staticResponse; - return new Response('Not Found', { status: 404 }); - }); - await server.start(); - - // lets handle realtime comms - const tapParser = new TapParser(fileNameArg + ':chrome', this.logger); - const wss = new plugins.ws.WebSocketServer({ port: wsPort }); - wss.on('connection', (ws) => { - ws.on('message', (message) => { - const messageStr = message.toString(); - if (messageStr.startsWith('console:')) { - const [, level, ...messageParts] = messageStr.split(':'); - this.logger.browserConsole(messageParts.join(':'), level); - } else { - tapParser.handleTapLog(messageStr); - } - }); - }); - - // lets do the browser bit with timeout handling - await this.smartbrowserInstance.start(); - - const evaluatePromise = this.smartbrowserInstance.evaluateOnPage( - `http://localhost:${httpPort}/test?bundleName=${encodeURIComponent(bundleFileName)}`, - async () => { - // lets enable real time comms - const ws = new WebSocket(`ws://localhost:${globalThis.wsPort}`); - await new Promise((resolve) => (ws.onopen = resolve)); - - // Ensure this function is declared with 'async' - const logStore = []; - const originalLog = console.log; - const originalError = console.error; - - // Override console methods to capture the logs - console.log = (...args: any[]) => { - logStore.push(args.join(' ')); - ws.send(args.join(' ')); - originalLog(...args); - }; - console.error = (...args: any[]) => { - logStore.push(args.join(' ')); - ws.send(args.join(' ')); - originalError(...args); - }; - - const bundleName = new URLSearchParams(window.location.search).get('bundleName'); - originalLog(`::TSTEST IN CHROMIUM:: Relevant Script name is: ${bundleName}`); - - try { - // Dynamically import the test module - const testModule = await import(`/${bundleName}`); - if (testModule && testModule.default && testModule.default instanceof Promise) { - // Execute the exported test function - await testModule.default; - } else if (testModule && testModule.default && typeof testModule.default.then === 'function') { - console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); - console.log('Test module default export is just promiselike: Something might be messing with your Promise implementation.'); - console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); - await testModule.default; - } else if (globalThis.tapPromise && typeof globalThis.tapPromise.then === 'function') { - console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); - console.log('Using globalThis.tapPromise'); - console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); - await testModule.default; - } else { - console.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); - console.error('Test module does not export a default promise.'); - console.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); - console.log(`We got: ${JSON.stringify(testModule)}`); - - } - } catch (err) { - console.error(err); - } - - return logStore.join('\n'); - } - ); - - // Start warning timer if no timeout was specified - let warningTimer: NodeJS.Timeout | null = null; - if (this.timeoutSeconds === null) { - warningTimer = setTimeout(() => { - console.error(''); - console.error(cs('⚠️ WARNING: Test file is running for more than 1 minute', 'orange')); - console.error(cs(` File: ${fileNameArg}`, 'orange')); - console.error(cs(' Consider using --timeout option to set a timeout for test files.', 'orange')); - console.error(cs(' Example: tstest test --timeout=300 (for 5 minutes)', 'orange')); - console.error(''); - }, 60000); // 1 minute - } - - // Handle timeout if specified - if (this.timeoutSeconds !== null) { - const timeoutMs = this.timeoutSeconds * 1000; - let timeoutId: NodeJS.Timeout; - - const timeoutPromise = new Promise((_resolve, reject) => { - timeoutId = setTimeout(() => { - reject(new Error(`Test file timed out after ${this.timeoutSeconds} seconds`)); - }, timeoutMs); - }); - - try { - await Promise.race([ - evaluatePromise, - timeoutPromise - ]); - // Clear timeout if test completed successfully - clearTimeout(timeoutId); - } catch (error) { - // Clear warning timer if it was set - if (warningTimer) { - clearTimeout(warningTimer); - } - // Handle timeout error - tapParser.handleTimeout(this.timeoutSeconds); - } - } else { - await evaluatePromise; - } - - // Clear warning timer if it was set - if (warningTimer) { - clearTimeout(warningTimer); - } - - // Always clean up resources, even on timeout - try { - await this.smartbrowserInstance.stop(); - } catch (error) { - // Browser might already be stopped - } - - try { - await server.stop(); - } catch (error) { - // Server might already be stopped - } - - try { - wss.close(); - } catch (error) { - // WebSocket server might already be closed - } - - console.log( - `${cs('=> ', 'blue')} Stopped ${cs(fileNameArg, 'orange')} chromium instance and server.` - ); - // Always evaluate final result (handleTimeout just sets up the test state) - await tapParser.evaluateFinalResult(); - return tapParser; - } - - public async runInDeno() {} - private async movePreviousLogFiles() { const logDir = plugins.path.join('.nogit', 'testlogs'); const previousDir = plugins.path.join('.nogit', 'testlogs', 'previous');