memorix icon indicating copy to clipboard operation
memorix copied to clipboard

memorix hook hangs indefinitely and leaks Node processes (macOS 100% CPU / memory pressure)

Open RaviTharuma opened this issue 3 months ago • 4 comments

Description

memorix hook is documented as a short-lived agent lifecycle command: read stdin JSON, write one stdout JSON object, and exit in seconds. In production on macOS (Memorix 1.5.0) it often never exits.

Each hung instance is a Node process started with --max-old-space-size=4096. Copies accumulate (15–19 hooks plus extra orphaned serve processes), exhaust RAM, and drive the macOS memory compressor / kernel_task until the machine is at 0.0% idle.

This is a process-lifecycle leak in the official CLI hook entrypoint, not a host-wrapper bug and not an MCP-tool hang.

Related but distinct: #196 (memorix_store hanging until an MCP client timeout). Same unbounded-work family, different entry point and impact. Do not close #196.


Environment

Item Value
OS darwin 25.6.0 (macOS), 16 GB RAM
When 2026-08-16 ~00:18–00:25 UTC+2
Memorix 1.5.0 (mcpName: io.github.AVIDS2/memorix)
Install <home>
Node <home>
Host Cursor agent hooks

Exact argv of a hung process:

<home> \
  --max-old-space-size=4096 \
  <home> \
  hook

How it is invoked (Cursor agent hook → wrapper → CLI):

bash <home>
# which historically did:
printf '%s' "$input" | memorix hook
# and blocked forever when the CLI never exited

Local LaunchAgents (present; not the leak):

  • io.tharuma.memorix-control-plane.plist — StartInterval 120, HTTP port 3211
  • io.tharuma.memorix-sync.plist

These are local operator daemons. They were left running. The bug is memorix hook not exiting.


Live snapshot (before kill)

Captured on the host while hooks were hung (2026-08-16 ~00:18–00:25 UTC+2):

Metric Value
Load averages 39.76 / 36.41 / 42.29
CPU 45.81% user, 54.18% sys, 0.0% idle
kernel_task ~119.9% (classic memory-pressure / compressor / swap)
PhysMem 15G used, ~125M unused, ~6962M compressor
Swap swapins ~71.9M, swapouts ~73.5M
Compressor pages pages occupied by compressor ~448k; pages stored in compressor ~2.19M
Process counts ~25 memorix-related processes; ~44 Node processes
Hung hooks ~15–19 memorix hook instances, many with PPID 1, etimes 42m–2h45m
Extra orphans orphaned memorix serve with PPID 1 (a live Cursor MCP serve must be left alone)
Representative RSS hooks 86–249 MB each
VSZ huge because every CLI invocation respawns with a 4 GB V8 heap cap

Expected vs actual

Expected

  1. Short-lived lifecycle hook: read stdin, emit one JSON object, exit in seconds.
  2. At most one leftover handle after stdout is written.
  3. A wedged store / embed / codegraph / LLM call cannot keep the process alive for hours.
  4. serve / MCP may use a large heap; hook must not.

Actual

  1. Event-loop / I/O hang. Processes live 1–3 hours.
  2. They accumulate and orphan onto PPID 1 after the parent wrapper is gone.
  3. RAM exhaustion → compressor / swap → kernel_task pins CPU.
  4. Host wrapper printf '%s' "$input" | memorix hook blocks forever, so the agent hook itself wedges.

Steps to reproduce

I did not bisect a single payload that always hangs. The failure mode is accumulation: one hung hook is a leak; many hung hooks are a denial of service against the host.

  1. Install memorix 1.5.0 (npm package memorix, CLI memorix hook).
  2. Enable agent / Cursor hooks that invoke memorix hook on lifecycle events (prompt submit, tool use, file edit, session start/stop, etc.).
  3. Use the agent normally for 30–180 minutes so hooks fire repeatedly.
  4. Observe ps / Activity Monitor:
    • many node --max-old-space-size=4096 …/memorix/dist/cli/index.js hook
    • etimes of tens of minutes to hours
    • several with PPID 1 after the parent wrapper is gone
  5. On a 16 GB Mac, 10–15 concurrent hooks is enough to fill RAM. macOS compressor + kernel_task then pin CPU.

Minimal hang probe (after a build of a fixed tree this should not hang):

# Producer never closes stdin and never finishes work:
python3 -c 'import time; time.sleep(600)' | memorix hook
# 1.5.0: process still alive after minutes
# expected: JSON {"continue":true} and exit in seconds
ps -axo pid,ppid,etime,rss,vsz,command | grep 'memorix/dist/cli/index.js hook' | grep -v grep

Code notes (1.5.0, read-only of the installed package)

src/cli/commands/hook.ts only await runHook(...).

src/hooks/handler.ts runHook():

  • Has a 3s stdin timeout (good; comments mention Gemini CLI not closing stdin).
  • Then unbounded work: handleHookEvent → storeObservation → queueCodegraphRefreshForMutation.
  • No process-level timeout, no process.exit, no stdin.pause() / destroy() after the stdin timer.
  • After stdout is written, Node can stay alive on open handles (stdin still open, SQLite, fetch, timers, queued codegraph refresh).

dist/cli/index.js always respawns:

node --max-old-space-size=4096 … hook

That heap size is reasonable for serve / codegraph, not for a per-event hook that can fire many times per minute.

Related: #196 (memorix_store hang until client timeout), #200 (withTimeout does not abort in-flight fetches). Those can explain why hook work never finishes; they do not explain why the CLI process is allowed to live for hours or why 15 copies can sit at 4 GB each.


Local mitigation (already applied on this host — not the upstream fix)

Do not treat the following as the product fix. It only stops this laptop from dying again.

  1. Killed hung memorix hook processes + leftover orphaned memorix serve (PPID 1).
  2. Kept the live Cursor MCP serve and serve-http on :3211.
  3. After cleanup: idle 13–32%, free RAM ~2 GB, compressor ~1.7 GB, kernel_task ~30%.
  4. Host wrapper now has a 20s timeout so a wedged hook cannot leak another 4 GB process.

Current wrapper (~/.agents/hooks/quiet-memorix-hook.sh):

#!/usr/bin/env bash
# Run memorix lifecycle hooks without flooding the chat transcript.
# Safe no-op when memorix is missing or returns empty output.
set -u
. "${HOME}/.agents/hooks/path-bootstrap.sh"

input="$(cat || true)"
if ! command -v memorix >/dev/null 2>&1; then
  printf '%s\n' '{"continue":true,"suppressOutput":true}'
  exit 0
fi

# Host-side backstop only. The CLI itself must still exit.
result="$(printf '%s' "$input" | python3 -c '
import subprocess, sys
try:
    completed = subprocess.run(
        ["memorix", "hook"],
        input=sys.stdin.buffer.read(),
        stdout=subprocess.PIPE,
        stderr=subprocess.DEVNULL,
        timeout=20,
    )
    sys.stdout.buffer.write(completed.stdout)
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
    pass
' || true)"

A host-side timeout is a seatbelt. Please ship a real fix in the CLI.


Asks for maintainers

Please fix this in the official memorix hook entrypoint (not in host wrappers):

  1. Hard timeout on memorix hook (15–30s, env-overridable, e.g. MEMORIX_HOOK_TIMEOUT_MS). Always write {"continue":true} (or equivalent) and exit.
  2. Idle / EOF exit when the stdin pipe closes or goes idle — no forever event loop. Destroy / pause stdin after the read.
  3. Smaller default heap for hook only (256–512 MB). Leave serve / MCP at 4096. Skip the unconditional --max-old-space-size=4096 respawn for this command.
  4. Single-instance / orphan cleanup that reaps leftover hook processes (PPID 1, etime above budget) without killing a live MCP serve.
  5. Treat hook store / embed / codegraph failures as best-effort: log and exit, never block the host agent.

Happy to attach another ps / top snapshot if useful. Please do not recommend killing a live Cursor MCP serve as the fix; the durable fix is making memorix hook exit.

RaviTharuma avatar Aug 15 '26 22:08 RaviTharuma

Field report addendum (2026-08-16, darwin 25.6.0)

This comment is the reproduce-oriented engineering report for maintainers. The issue body has the same facts in one place.

1. Exact hung argv

<home> \
  --max-old-space-size=4096 \
  <home> \
  hook

Package: Memorix 1.5.0 at <home>. Window: ~00:18–00:25 UTC+2.

2. Host invocation that blocked forever

Cursor agent hooks call:

bash <home>

The wrapper historically did printf '%s' "$input" | memorix hook and waited on the CLI. When memorix hook never exited, the agent hook itself never returned.

3. Live numbers before kill

  • Load: 39.76 / 36.41 / 42.29
  • CPU: 45.81% user, 54.18% sys, 0.0% idle
  • kernel_task ~119.9%
  • PhysMem: 15G used, ~125M unused, ~6962M compressor
  • Swapins ~71.9M, swapouts ~73.5M
  • Pages occupied by compressor ~448k; pages stored in compressor ~2.19M
  • ~25 memorix-related processes, ~44 Node processes
  • ~15–19 hung hooks, many PPID 1, etimes 42m–2h45m
  • Extra orphaned memorix serve PPID 1 (live Cursor MCP serve was not killed)
  • Hook RSS 86–249 MB each; VSZ huge because of the 4 GB heap cap

4. After local cleanup (mitigation only)

Killed hung hooks + leftover orphaned serves. Kept live MCP + serve-http :3211.

  • Idle 13–32%
  • Free RAM ~2 GB
  • Compressor ~1.7 GB
  • kernel_task ~30%

Wrapper now has a 20s subprocess.run(..., timeout=20) so a wedged hook cannot leak another 4 GB process. That is a host seatbelt, not the upstream fix.

5. LaunchAgents (local; not the bug)

  • io.tharuma.memorix-control-plane.plist — StartInterval 120, port 3211
  • io.tharuma.memorix-sync.plist

6. Expected vs actual

  • Expected: read stdin, emit JSON, exit in seconds.
  • Actual: event-loop / I/O hang; processes live 1–3 hours; accumulate; RAM exhaustion → compressor/swap → kernel_task pins CPU.

7. Related

Closest issue is #196 (MCP memorix_store hang). Same unbounded-work class, not CLI hook accumulation. Do not close #196.

8. Asks (CLI, not host timeout)

  1. Hard timeout on memorix hook (15–30s, env-overridable, e.g. MEMORIX_HOOK_TIMEOUT_MS).
  2. Idle/EOF exit when stdin pipe closes — no forever event loop.
  3. Smaller default heap for hook only (256–512 MB). Leave serve alone.
  4. Single-instance / orphan cleanup that does not kill a live MCP serve.

Fix PR: I will attach / have attached a CLI-side patch that implements 1–3 and documents 4 as residual (concurrent hooks from different events are legitimate; the deadline is what stops accumulation).

RaviTharuma avatar Aug 15 '26 22:08 RaviTharuma

Fleet follow-up (same night, 2026-08-16)

This is not a single-Mac flake. After the original report we audited every reachable host that actually runs Memorix. Every one of them is still on 1.5.0 (unbounded memorix hook, --max-old-space-size=4096). The same leak is sitting on the whole fleet until #206 lands.

Second live incident — (worse than the original)

  • 32 GB RAM, Darwin 25.6.0, Memorix 1.5.0
  • 3 hung hook trees (31–36 min, PPID 1) plus 4 orphaned 4 GB serve processes (PPID 1, up to 2.5 h)
  • Load 146–186, ~25 GB of 26 GB swap
  • No host-side hook timeout at the time
  • Hung hooks + PPID-1 leftover serve killed; live Cursor MCP serve and serve-http :3211 left alone
  • After kill: load still ~137, swap ~23 GB (compressor/swap lag — machine was not rebooted; 12 sessions + Cursor still up)

Other Memorix hosts (same binary, not yet on fire)

Host Memorix Hung hooks at audit Notes
macbook13 (original, 16 GB) 1.5.0 0 (already cleaned) Load falling after first kill
(32 GB) 1.5.0 yes — active incident See above
dev-001 (Ubuntu, 24 GB) 1.5.0 0 Healthy serve-http only; wrapper had no timeout
(Ubuntu, 8 GB) 1.5.0 0 systemd control plane; OpenClaw/OpenCode plugins already use 10s spawnSync

Hosts without Memorix (Pi / PVE / HA / Talos / phones) are irrelevant to this bug.

Host-side fuse (not a substitute for #206)

We added a 20s timeout around memorix hook on macbook13, , and dev-001 so a wedged hook cannot leak another 4 GB Node. That is a wrapper, not a CLI fix. memorix hook 1.5.0 can still open a 4 GB heap; #206 is still the real fix (deadline + stdin EOF/idle exit + 512 MB hook heap).

Please merge #206. We now have two independent Macs the same night, one of them at load ~150 / 25 GB swap.

RaviTharuma avatar Aug 15 '26 22:08 RaviTharuma

1.6.1 已发,#206 的原始提交已经保留并合进主线。现在 hook 的 stdin 有空闲上限、进程有硬时限,写入走延后路径,hook 自己只使用 512 MiB 堆;macOS、Windows、Linux 都跑过完整测试。谢谢这次很具体的排查。

AVIDS2 avatar Aug 17 '26 04:08 AVIDS2

1.6.1 bounded the hook lifecycle (#206). A separate happy-path problem remains on 1.6.0: hook init still loadAll()s ~40k observations before writing one capture, so OpenCode per-tool events get skipped and formation=shadow gets used as a workaround.

New write-up: #217. Do not treat host-side hook timeouts as the product fix.

RaviTharuma avatar Aug 17 '26 13:08 RaviTharuma