summaryrefslogtreecommitdiff
path: root/skills/brainstorming/scripts
diff options
context:
space:
mode:
authorJesse Vincent <jesse@fsck.com>2026-03-24 14:39:20 -0700
committerJesse Vincent <jesse@fsck.com>2026-03-24 14:39:20 -0700
commitaf025aa35bcd403b15e2b44281304c1e92da0f5d (patch)
tree2ebec944274c1ce98bc231813c471d59aee27e98 /skills/brainstorming/scripts
parent738a18d6ff47be822cfd2e81ace56b3287814dd4 (diff)
Fix owner-PID lifecycle monitoring for cross-platform reliabilityfix/owner-pid-lifecycle
Two bugs caused the brainstorm server to self-terminate within 60s: 1. ownerAlive() treated EPERM (permission denied) as "process dead". When the owner PID belongs to a different user (Tailscale SSH, system daemons), process.kill(pid, 0) throws EPERM — but the process IS alive. Fixed: return e.code === 'EPERM'. 2. On WSL, the grandparent PID resolves to a short-lived subprocess that exits before the first 60s lifecycle check. The PID is genuinely dead (ESRCH), so the EPERM fix alone doesn't help. Fixed: validate the owner PID at server startup — if it's already dead, it was a bad resolution, so disable monitoring and rely on the 30-minute idle timeout. This also removes the Windows/MSYS2-specific OWNER_PID="" carve-out from start-server.sh, since the server now handles invalid PIDs generically at startup regardless of platform. Tested on Linux (magic-kingdom) via Tailscale SSH: - Root-owned owner PID (EPERM): server survives ✓ - Dead owner PID at startup (WSL sim): monitoring disabled, survives ✓ - Valid owner that dies: server shuts down within 60s ✓ Fixes #879
Diffstat (limited to 'skills/brainstorming/scripts')
-rw-r--r--skills/brainstorming/scripts/server.cjs19
-rwxr-xr-xskills/brainstorming/scripts/start-server.sh6
2 files changed, 16 insertions, 9 deletions
diff --git a/skills/brainstorming/scripts/server.cjs b/skills/brainstorming/scripts/server.cjs
index 14b8197..562c17f 100644
--- a/skills/brainstorming/scripts/server.cjs
+++ b/skills/brainstorming/scripts/server.cjs
@@ -79,7 +79,7 @@ const URL_HOST = process.env.BRAINSTORM_URL_HOST || (HOST === '127.0.0.1' ? 'loc
const SESSION_DIR = process.env.BRAINSTORM_DIR || '/tmp/brainstorm';
const CONTENT_DIR = path.join(SESSION_DIR, 'content');
const STATE_DIR = path.join(SESSION_DIR, 'state');
-const OWNER_PID = process.env.BRAINSTORM_OWNER_PID ? Number(process.env.BRAINSTORM_OWNER_PID) : null;
+let ownerPid = process.env.BRAINSTORM_OWNER_PID ? Number(process.env.BRAINSTORM_OWNER_PID) : null;
const MIME_TYPES = {
'.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript',
@@ -312,8 +312,8 @@ function startServer() {
}
function ownerAlive() {
- if (!OWNER_PID) return true;
- try { process.kill(OWNER_PID, 0); return true; } catch (e) { return e.code === 'EPERM'; }
+ if (!ownerPid) return true;
+ try { process.kill(ownerPid, 0); return true; } catch (e) { return e.code === 'EPERM'; }
}
// Check every 60s: exit if owner process died or idle for 30 minutes
@@ -323,6 +323,19 @@ function startServer() {
}, 60 * 1000);
lifecycleCheck.unref();
+ // Validate owner PID at startup. If it's already dead, the PID resolution
+ // was wrong (common on WSL, Tailscale SSH, and cross-user scenarios).
+ // Disable monitoring and rely on the idle timeout instead.
+ if (ownerPid) {
+ try { process.kill(ownerPid, 0); }
+ catch (e) {
+ if (e.code !== 'EPERM') {
+ console.log(JSON.stringify({ type: 'owner-pid-invalid', pid: ownerPid, reason: 'dead at startup' }));
+ ownerPid = null;
+ }
+ }
+ }
+
server.listen(PORT, HOST, () => {
const info = JSON.stringify({
type: 'server-started', port: Number(PORT), host: HOST,
diff --git a/skills/brainstorming/scripts/start-server.sh b/skills/brainstorming/scripts/start-server.sh
index c3dd7f9..9ef6dcb 100755
--- a/skills/brainstorming/scripts/start-server.sh
+++ b/skills/brainstorming/scripts/start-server.sh
@@ -107,12 +107,6 @@ if [[ -z "$OWNER_PID" || "$OWNER_PID" == "1" ]]; then
OWNER_PID="$PPID"
fi
-# On Windows/MSYS2, the MSYS2 PID namespace is invisible to Node.js.
-# Skip owner-PID monitoring — the 30-minute idle timeout prevents orphans.
-case "${OSTYPE:-}" in
- msys*|cygwin*|mingw*) OWNER_PID="" ;;
-esac
-
# Foreground mode for environments that reap detached/background processes.
if [[ "$FOREGROUND" == "true" ]]; then
echo "$$" > "$PID_FILE"