feat(native-host): kill previous instance on startup and in installers
CI / Build & Test (push) Failing after 3m39s

- Add kill-previous-instance.js with PID lock file under BASE_DIR
- On startup, send SIGTERM to any existing native-host process before binding ports
- Register removePidFile on shutdown and exit so the lock is cleared
- Run "stop any running native host" at the start of install.sh and install.ps1
- Add kill-previous-instance.js to CI syntax check
This commit is contained in:
Raven Scott
2026-03-14 23:22:43 -04:00
parent 2693e39040
commit 3bf9851390
5 changed files with 84 additions and 6 deletions
+1
View File
@@ -75,6 +75,7 @@ jobs:
node --check native-host/managers/rdp-manager.js
node --check native-host/host/messenger.js
node --check native-host/host/paths.js
node --check native-host/host/kill-previous-instance.js
node --check native-host/host/logger.js
node --check native-host/host/startup.js
node --check native-host/host/message-router.js
@@ -0,0 +1,62 @@
/**
* Kill any previously running native-host instance (same installation) before
* we bind to ports. Uses a PID file under BASE_DIR so the lock is per-install.
*/
const path = require('bare-path');
const fs = require('bare-fs');
const { BASE_DIR } = require('./paths.js');
const PID_FILE = path.join(BASE_DIR, '.holesail-browser-host.pid');
function isProcessAlive(pid) {
if (typeof pid !== 'number' || pid <= 0 || pid === process.pid) return false;
try {
process.kill(pid, 0);
return true;
} catch (_) {
return false;
}
}
/**
* Read existing PID from lock file; if it is another running process, send
* SIGTERM. Then write our PID to the lock file. Does not wait for the old
* process to exit.
*/
function killPreviousInstanceSync() {
let existingPid = null;
try {
const raw = fs.readFileSync(PID_FILE, 'utf8');
const n = parseInt(raw.trim(), 10);
if (Number.isFinite(n)) existingPid = n;
} catch (_) {
// No file or unreadable: treat as no previous instance
}
if (existingPid != null && existingPid !== process.pid && isProcessAlive(existingPid)) {
try {
process.kill(existingPid, 'SIGTERM');
} catch (_) {}
}
writePidFile();
}
function writePidFile() {
try {
fs.writeFileSync(PID_FILE, String(process.pid), 'utf8');
} catch (_) {}
}
function removePidFile() {
try {
fs.unlinkSync(PID_FILE);
} catch (_) {}
}
module.exports = {
killPreviousInstanceSync,
writePidFile,
removePidFile
};
+10 -1
View File
@@ -14,6 +14,9 @@
import 'bare-process/global';
import './set-tmpdir.mjs';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const { killPreviousInstanceSync, removePidFile } = require('./host/kill-previous-instance.js');
import _messenger from './host/messenger.js';
import _host from './host/message-router.js';
@@ -31,6 +34,8 @@ if (process.argv.includes('--extract-addons')) {
const { createMessenger } = _messenger;
const { handleMessage, cleanup } = _host;
killPreviousInstanceSync();
function logErr(msg) {
try {
process.stderr.write(`[holesail-browser-host] ${msg}\n`);
@@ -69,12 +74,16 @@ const messenger = createMessenger({
});
function shutdown() {
removePidFile();
cleanup();
messenger.destroy();
process.exit(0);
}
process.on('exit', () => cleanup());
process.on('exit', () => {
removePidFile();
cleanup();
});
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
+5 -2
View File
@@ -20,6 +20,10 @@ Write-Host "==========================" -ForegroundColor Cyan
Write-Host "Install : $InstallDir"
Write-Host ""
# ── Stop any running native host first ─────────────────────────────────────────
Write-Host "Stopping any running native host..."
Get-Process | Where-Object { $_.Path -like "*holesail-browser-host*" } | Stop-Process -Force -ErrorAction SilentlyContinue
# ── Preserve user data from any previous installation ─────────────────────────
$StashDir = Join-Path $env:TEMP "holesail-browser-stash-$([System.Guid]::NewGuid().ToString('N'))"
$StashStorage = Join-Path $StashDir "holesail-browser-storage"
@@ -42,9 +46,8 @@ if (Test-Path $InstallDir) {
$HadPrevious = $true
}
# ── Stop any running instance ──────────────────────────────────────────────────
# ── Remove previous installation ──────────────────────────────────────────────
Write-Host "Removing any previous installation..."
Get-Process | Where-Object { $_.Path -like "*holesail-browser-host*" } | Stop-Process -Force -ErrorAction SilentlyContinue
if (Test-Path $InstallDir) { Remove-Item $InstallDir -Recurse -Force }
# Remove old registry entries
+6 -3
View File
@@ -34,6 +34,11 @@ echo "Platform : ${PLATFORM}-${ARCH}"
echo "Install : ${INSTALL_DIR}"
echo ""
# ── Stop any running native host first ────────────────────────────────────────
echo "Stopping any running native host..."
pkill -f "holesail-browser/native-host/index.mjs" 2>/dev/null || true
pkill -f "holesail-browser-host" 2>/dev/null || true
# ── Preserve user data from any previous installation ─────────────────────────
STASH_DIR="$(mktemp -d)"
STASH_STORAGE="${STASH_DIR}/holesail-browser-storage"
@@ -51,10 +56,8 @@ if [[ -d "$INSTALL_DIR" ]]; then
HAD_PREVIOUS=true
fi
# ── Stop any running instance ──────────────────────────────────────────────────
# ── Remove previous installation ──────────────────────────────────────────────
echo "Removing any previous installation..."
pkill -f "holesail-browser/native-host/index.mjs" 2>/dev/null || true
pkill -f "holesail-browser-host" 2>/dev/null || true
rm -rf "$INSTALL_DIR"
# Remove old native messaging manifests