memtrace 0.3.89 → 0.3.90

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 +6 -6
  2. package/uninstall.js +79 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "memtrace",
3
- "version": "0.3.89",
3
+ "version": "0.3.90",
4
4
  "description": "Code intelligence graph — MCP server + AI agent skills + visualization UI",
5
5
  "keywords": [
6
6
  "mcp",
@@ -39,11 +39,11 @@
39
39
  "fs-extra": "^11.0.0"
40
40
  },
41
41
  "optionalDependencies": {
42
- "@memtrace/darwin-arm64": "0.3.89",
43
- "@memtrace/linux-x64": "0.3.89",
44
- "@memtrace/win32-x64": "0.3.89",
45
- "@memtrace/linux-x64-noavx2": "0.3.89",
46
- "@memtrace/win32-x64-noavx2": "0.3.89"
42
+ "@memtrace/darwin-arm64": "0.3.90",
43
+ "@memtrace/linux-x64": "0.3.90",
44
+ "@memtrace/win32-x64": "0.3.90",
45
+ "@memtrace/linux-x64-noavx2": "0.3.90",
46
+ "@memtrace/win32-x64-noavx2": "0.3.90"
47
47
  },
48
48
  "engines": {
49
49
  "node": ">=18"
package/uninstall.js CHANGED
@@ -294,7 +294,85 @@ function legacyCleanup(options = {}) {
294
294
  removeClaudeIntegration(home);
295
295
  }
296
296
 
297
+ function killRunningMemtraceProcesses(env = process.env) {
298
+ // npm's atomic global install on macOS / Linux works by renaming the
299
+ // existing `node_modules/memtrace` directory aside (to a `.memtrace-XXXX`
300
+ // tempname) before extracting the new tarball. If any memtrace process
301
+ // is still running — a long-lived daemon from `memtrace start`, an MCP
302
+ // server an agent left attached, the file watcher — the binary on disk
303
+ // is mmap'd into that process and the rename fails with ENOTEMPTY,
304
+ // leaving both the old dir AND a partially-renamed temp dir on disk.
305
+ // The user then has to manually rm both and retry. We can prevent that
306
+ // by killing memtrace processes BEFORE npm tries the rename — which is
307
+ // exactly what the preuninstall hook (this script) runs.
308
+ //
309
+ // Self-upgrade case: `memtrace install` sets MEMTRACE_INSTALL_PARENT=1
310
+ // before spawning npm, so the running memtrace binary is OUR PARENT.
311
+ // Killing it would abort the npm install we're a child of. Skip in
312
+ // that path — the parent will exit cleanly after its npm child returns
313
+ // and the rename will succeed on its way out.
314
+ if (env.MEMTRACE_INSTALL_PARENT === "1") {
315
+ return;
316
+ }
317
+
318
+ const tryKill = (cmd, args) => {
319
+ try {
320
+ spawnSync(cmd, args, { stdio: "ignore" });
321
+ } catch {
322
+ // taskkill / pkill missing from PATH is rare but possible inside
323
+ // pared-down containers or locked-down enterprise images. We don't
324
+ // want to abort the user's install because a process-killer isn't
325
+ // installed — fall through and let npm's rename try its luck.
326
+ }
327
+ };
328
+
329
+ if (process.platform === "win32") {
330
+ // /F = force terminate
331
+ // /T = also kill child processes (file watchers, embed-worker children
332
+ // spawned by `memtrace start`, MCP server's notify-rs threads)
333
+ // /IM = match by image name; covers every launcher because the npm
334
+ // shim execs the same `memtrace.exe` binary regardless of
335
+ // `start` / `mcp` / `daemon` argv.
336
+ tryKill("taskkill", ["/F", "/T", "/IM", "memtrace.exe"]);
337
+ // Defensive: some Windows installs end up with a `node.exe` running
338
+ // `memtrace.js` (the npm shim) that hasn't yet exec'd the platform
339
+ // binary. Kill any node process whose command line references our
340
+ // shim path via PowerShell — wrapped so a missing PS install (rare
341
+ // on modern Windows but possible on Server Core) doesn't abort.
342
+ tryKill("powershell", [
343
+ "-NoProfile",
344
+ "-NonInteractive",
345
+ "-Command",
346
+ "Get-CimInstance Win32_Process -Filter \"Name='node.exe'\" | " +
347
+ "Where-Object { $_.CommandLine -like '*memtrace*\\bin\\memtrace.js*' } | " +
348
+ "ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }",
349
+ ]);
350
+ } else {
351
+ // Three patterns cover every launcher shape the binary exposes.
352
+ // `pkill -f` matches against the full command line, so `memtrace mcp`
353
+ // catches both the npm-shim invocation AND the platform binary it
354
+ // exec'd into. Missing-process exit (1) is ignored via stdio: ignore.
355
+ tryKill("pkill", ["-9", "-f", "memtrace mcp"]);
356
+ tryKill("pkill", ["-9", "-f", "memtrace start"]);
357
+ tryKill("pkill", ["-9", "-f", "memtrace daemon"]);
358
+ }
359
+
360
+ // Settle window: SIGKILL / taskkill is synchronous but the OS needs a
361
+ // tick to tear down mmap'd page tables and release directory entries.
362
+ // - macOS / Linux: 200ms is enough on every machine tested; the vnode
363
+ // reference usually drops within ~50ms after TASK_INACTIVE.
364
+ // - Windows: longer hold because Windows Defender, Search Indexer, and
365
+ // any OneDrive sync agent each take a file-handle reference when the
366
+ // binary exits. 750ms is empirically enough to clear those — well
367
+ // under any human-noticeable install delay.
368
+ const settleMs = process.platform === "win32" ? 750 : 200;
369
+ const until = Date.now() + settleMs;
370
+ // eslint-disable-next-line no-empty
371
+ while (Date.now() < until) {}
372
+ }
373
+
297
374
  function run() {
375
+ killRunningMemtraceProcesses();
298
376
  console.log("memtrace: cleaning up...");
299
377
 
300
378
  // Try delegating to the compiled installer (handles ALL agents via registry)
@@ -346,6 +424,7 @@ module.exports = {
346
424
  removeMemtraceHomeDir,
347
425
  isUpgradeLifecycle,
348
426
  shouldPurgeMemtraceHomeDir,
427
+ killRunningMemtraceProcesses,
349
428
  removeClaudeIntegration,
350
429
  legacyCleanup,
351
430
  };