Compare commits

...

1 Commits

Author SHA1 Message Date
Nikhil Sonti
b03bad80e1 test(server): reap orphaned test browsers before spawn
Pre-kill BrowserOS processes whose --user-data-dir path contains the
browseros-test- prefix before each spawnBrowser, and in the test:cleanup
hook. This prevents a crashed prior test run from leaving a headless
BrowserOS attached to a stale port, without touching the developer's
regular BrowserOS.app instance (its user-data-dir is
~/Library/Application Support/BrowserOS, which does not match).
2026-04-15 14:15:26 -07:00
2 changed files with 23 additions and 1 deletions

View File

@@ -6,9 +6,11 @@
* Use setup.ts:ensureBrowserOS() for the full test environment.
*/
import type { ChildProcess } from 'node:child_process'
import { spawn } from 'node:child_process'
import { spawn, spawnSync } from 'node:child_process'
import { rmSync } from 'node:fs'
const TEST_USER_DATA_PREFIX = 'browseros-test-'
export interface BrowserConfig {
cdpPort: number
serverPort: number
@@ -58,6 +60,16 @@ export function getBrowserState(): BrowserState | null {
return browserState
}
function killOrphanedTestBrowsers(): void {
// Matches only BrowserOS processes launched with a test user-data-dir
// (e.g., /var/folders/.../browseros-test-XXXX). Never matches a dev
// BrowserOS run from ~/Library/Application Support/BrowserOS.
const result = spawnSync('pkill', ['-9', '-f', TEST_USER_DATA_PREFIX])
if (result.status === 0) {
console.log('Killed orphaned test browsers from a previous run')
}
}
export async function spawnBrowser(
config: BrowserConfig,
): Promise<BrowserState> {
@@ -73,6 +85,8 @@ export async function spawnBrowser(
await killBrowser()
}
killOrphanedTestBrowsers()
console.log(`Starting BrowserOS on CDP port ${config.cdpPort}...`)
const browserProcess = spawn(
config.binaryPath,

View File

@@ -20,6 +20,14 @@ for port in $CDP_PORT $SERVER_PORT $EXTENSION_PORT; do
fi
done
# Kill orphaned test browser processes (matches only BrowserOS launched with
# a test user-data-dir — never the user's dev BrowserOS)
orphan_pids=$(pgrep -f 'browseros-test-' 2>/dev/null || true)
if [ -n "$orphan_pids" ]; then
echo " Killing orphaned test browser processes: $(echo "$orphan_pids" | tr '\n' ' ')"
echo "$orphan_pids" | xargs kill -9 2>/dev/null || true
fi
# Clean up orphaned temp directories (created by browser.ts)
# Uses $TMPDIR which matches Node's os.tmpdir()
TEMP_DIR="${TMPDIR:-/tmp}"