magector 2.2.1 → 2.2.3

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 +5 -5
  2. package/src/mcp-server.js +27 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magector",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
4
4
  "description": "Semantic code search for Magento 2 — index, search, MCP server",
5
5
  "type": "module",
6
6
  "main": "src/mcp-server.js",
@@ -39,10 +39,10 @@
39
39
  "ruvector": "^0.1.96"
40
40
  },
41
41
  "optionalDependencies": {
42
- "@magector/cli-darwin-arm64": "2.2.1",
43
- "@magector/cli-linux-x64": "2.2.1",
44
- "@magector/cli-linux-arm64": "2.2.1",
45
- "@magector/cli-win32-x64": "2.2.1"
42
+ "@magector/cli-darwin-arm64": "2.2.3",
43
+ "@magector/cli-linux-x64": "2.2.3",
44
+ "@magector/cli-linux-arm64": "2.2.3",
45
+ "@magector/cli-win32-x64": "2.2.3"
46
46
  },
47
47
  "keywords": [
48
48
  "magento",
package/src/mcp-server.js CHANGED
@@ -166,12 +166,19 @@ function tryAcquirePrimaryLock() {
166
166
  return false; // another instance is alive and primary
167
167
  }
168
168
  } catch { /* holder is dead, take over */ }
169
- // Stale lock — reclaim
169
+ // Stale lock — reclaim. Use random jitter to avoid thundering herd
170
+ // when multiple instances detect stale lock simultaneously.
171
+ const jitterMs = Math.floor(Math.random() * 200) + 50;
172
+ const start = Date.now();
173
+ while (Date.now() - start < jitterMs) { /* busy-wait for sub-second jitter */ }
170
174
  try { unlinkSync(PRIMARY_LOCK_PATH); } catch {}
171
175
  try {
172
176
  const fd = openSync(PRIMARY_LOCK_PATH, fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL);
173
177
  writeFileSync(fd, String(process.pid));
174
178
  closeSync(fd);
179
+ // Double-check: re-read to confirm we actually own it (no TOCTOU race)
180
+ const check = readFileSync(PRIMARY_LOCK_PATH, 'utf-8').trim();
181
+ if (check !== String(process.pid)) return false;
175
182
  return true;
176
183
  } catch {
177
184
  return false; // another instance beat us
@@ -566,6 +573,14 @@ let serveReadyPromise = null;
566
573
  let serveReadyResolve = null;
567
574
 
568
575
  function startServeProcess() {
576
+ // Guard: if a live serve process already exists (e.g. started by another
577
+ // instance that won the lock race), reuse it instead of spawning a duplicate.
578
+ const existingPid = getExistingServePid();
579
+ if (existingPid) {
580
+ logToFile('INFO', `Reusing existing serve process (PID ${existingPid}) — skipping spawn`);
581
+ return;
582
+ }
583
+
569
584
  serveReadyPromise = new Promise((resolve) => { serveReadyResolve = resolve; });
570
585
  try {
571
586
  const args = [
@@ -3525,6 +3540,17 @@ process.on('SIGTERM', () => { cleanup('SIGTERM'); process.exit(0); });
3525
3540
  process.on('SIGINT', () => { cleanup('SIGINT'); process.exit(0); });
3526
3541
  process.on('SIGHUP', () => { cleanup('SIGHUP'); process.exit(0); });
3527
3542
 
3543
+ // Exit when parent (Claude Code) closes stdin — prevents orphaned processes
3544
+ process.stdin.on('end', () => {
3545
+ logToFile('INFO', 'stdin closed (parent disconnected) — shutting down');
3546
+ cleanup('stdin end');
3547
+ process.exit(0);
3548
+ });
3549
+ process.stdin.on('error', () => {
3550
+ cleanup('stdin error');
3551
+ process.exit(0);
3552
+ });
3553
+
3528
3554
  main().catch((err) => {
3529
3555
  logToFile('FATAL', `Startup failed: ${err.message}\n${err.stack}`);
3530
3556
  console.error(err);