gm-plugkit 2.0.1516 → 2.0.1518
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/supervisor.js +48 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gm-plugkit",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1518",
|
|
4
4
|
"description": "Bootstrap and daemon-spawn tool for gm plugkit binary. Downloads the correct platform binary, verifies SHA256, and starts the spool watcher daemon. Includes plugkit-wasm-wrapper for WASM-based spool watching.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
package/supervisor.js
CHANGED
|
@@ -23,6 +23,7 @@ fs.mkdirSync(spoolDir, { recursive: true });
|
|
|
23
23
|
const STATUS_PATH = path.join(spoolDir, '.status.json');
|
|
24
24
|
const SHUTDOWN_REASON_PATH = path.join(spoolDir, '.shutdown-reason.json');
|
|
25
25
|
const SUPERVISOR_PATH = path.join(spoolDir, '.supervisor.json');
|
|
26
|
+
const SUPERVISOR_PID_PATH = path.join(spoolDir, '.supervisor.pid');
|
|
26
27
|
const LOG_PATH = path.join(spoolDir, '.watcher.log');
|
|
27
28
|
const GM_LOG_ROOT = process.env.GM_LOG_DIR || path.join(os.homedir(), '.claude', 'gm-log');
|
|
28
29
|
|
|
@@ -66,6 +67,47 @@ function pidAlive(pid) {
|
|
|
66
67
|
try { process.kill(pid, 0); return true; } catch (_) { return false; }
|
|
67
68
|
}
|
|
68
69
|
|
|
70
|
+
// Single-instance guard. findSupervisorPid (skill-bootstrap.js) reads .supervisor.pid to early-return
|
|
71
|
+
// when a supervisor is already running; without it every bootstrap spawns a duplicate supervisor,
|
|
72
|
+
// and duplicates spawn duplicate watchers that lock-fight in an endless spawn-reject churn. Write the
|
|
73
|
+
// pid file on startup and refuse to start if a live peer already holds it.
|
|
74
|
+
function acquireSingleInstance() {
|
|
75
|
+
// Atomic via O_EXCL ('wx'): exclusive-create fails if the file exists, so when N supervisors
|
|
76
|
+
// race to start in the same instant exactly one wins. A plain existsSync->write is TOCTOU and
|
|
77
|
+
// lets a concurrent burst all pass, which is the duplicate-supervisor churn this guards against.
|
|
78
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
79
|
+
try {
|
|
80
|
+
const fd = fs.openSync(SUPERVISOR_PID_PATH, 'wx');
|
|
81
|
+
try { fs.writeSync(fd, String(process.pid)); } finally { fs.closeSync(fd); }
|
|
82
|
+
return true;
|
|
83
|
+
} catch (e) {
|
|
84
|
+
if (e && e.code === 'EEXIST') {
|
|
85
|
+
let other = NaN;
|
|
86
|
+
try { other = parseInt(fs.readFileSync(SUPERVISOR_PID_PATH, 'utf-8').trim(), 10); } catch (_) {}
|
|
87
|
+
if (Number.isFinite(other) && other !== process.pid && pidAlive(other)) {
|
|
88
|
+
logEvent('supervisor.refused-duplicate', { existing_pid: other, severity: 'warn' });
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
// Holder is dead/stale: remove and retry the exclusive create once.
|
|
92
|
+
try { fs.unlinkSync(SUPERVISOR_PID_PATH); } catch (_) {}
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
logEvent('supervisor.pid-write-failed', { error: e && e.message, severity: 'warn' });
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function releaseSingleInstance() {
|
|
103
|
+
try {
|
|
104
|
+
if (fs.existsSync(SUPERVISOR_PID_PATH)) {
|
|
105
|
+
const raw = fs.readFileSync(SUPERVISOR_PID_PATH, 'utf-8').trim();
|
|
106
|
+
if (parseInt(raw, 10) === process.pid) fs.unlinkSync(SUPERVISOR_PID_PATH);
|
|
107
|
+
}
|
|
108
|
+
} catch (_) {}
|
|
109
|
+
}
|
|
110
|
+
|
|
69
111
|
function readStatus() {
|
|
70
112
|
try { return JSON.parse(fs.readFileSync(STATUS_PATH, 'utf-8')); } catch (_) { return null; }
|
|
71
113
|
}
|
|
@@ -273,9 +315,15 @@ process.on('SIGTERM', () => {
|
|
|
273
315
|
if (currentChildPid && pidAlive(currentChildPid)) {
|
|
274
316
|
try { process.kill(currentChildPid, 'SIGTERM'); } catch (_) {}
|
|
275
317
|
}
|
|
318
|
+
releaseSingleInstance();
|
|
276
319
|
process.exit(0);
|
|
277
320
|
});
|
|
321
|
+
process.on('exit', () => { releaseSingleInstance(); });
|
|
278
322
|
|
|
323
|
+
if (!acquireSingleInstance()) {
|
|
324
|
+
process.stderr.write('[plugkit-supervisor] another supervisor is alive; exiting\n');
|
|
325
|
+
process.exit(0);
|
|
326
|
+
}
|
|
279
327
|
writeSupervisorStatus('starting', {});
|
|
280
328
|
logEvent('supervisor.starting', { spool_dir: spoolDir });
|
|
281
329
|
try { fs.unlinkSync(path.join(spoolDir, '.pre-supervised-watcher.json')); } catch (_) {}
|