gm-plugkit 2.0.1517 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/supervisor.js +22 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gm-plugkit",
3
- "version": "2.0.1517",
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
@@ -72,20 +72,31 @@ function pidAlive(pid) {
72
72
  // and duplicates spawn duplicate watchers that lock-fight in an endless spawn-reject churn. Write the
73
73
  // pid file on startup and refuse to start if a live peer already holds it.
74
74
  function acquireSingleInstance() {
75
- try {
76
- if (fs.existsSync(SUPERVISOR_PID_PATH)) {
77
- const other = parseInt(fs.readFileSync(SUPERVISOR_PID_PATH, 'utf-8').trim(), 10);
78
- if (Number.isFinite(other) && other !== process.pid && pidAlive(other)) {
79
- logEvent('supervisor.refused-duplicate', { existing_pid: other, severity: 'warn' });
80
- return false;
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;
81
94
  }
95
+ logEvent('supervisor.pid-write-failed', { error: e && e.message, severity: 'warn' });
96
+ return true;
82
97
  }
83
- fs.writeFileSync(SUPERVISOR_PID_PATH, String(process.pid));
84
- return true;
85
- } catch (e) {
86
- logEvent('supervisor.pid-write-failed', { error: e.message, severity: 'warn' });
87
- return true;
88
98
  }
99
+ return true;
89
100
  }
90
101
 
91
102
  function releaseSingleInstance() {