Skip to content

Post-commit hook spawns unbounded detached Python rebuilds — multi-repo concurrent commits exhaust memory #791

Closed

Description

@EmanuelFaria

Summary

graphify hook install registers a git post-commit hook (hooks.py:46-93) that
spawns a detached Python process to rebuild the entire repo's graph. The hook
has no concurrency cap, no memory/CPU limit, no timeout, and does not use the
GRAPHIFY_CHANGED data it computes. When multiple repos with the hook commit
in the same time window — or when a single watcher fires _rebuild_code across
many projects — the system fills with concurrent multi-GB rebuilds.

This caused a full UI freeze on a Mac mini (32 GB RAM) this morning: ~10
concurrent rebuilds, peak 4.1 GB / 99% CPU each, free RAM dropped to 60 MB,
load average hit 31, mouse clicks froze for 30+ minutes.

Reproduction

  1. graphify hook install in three or more repos
  2. Trigger commits in all of them within ~10 seconds (e.g. via a multi-repo
    sync tool, autocommit script, or pre-commit chain)
  3. Observe: N independent python -c "from graphify.watch import _rebuild_code; _rebuild_code(Path('.'))"
    processes, each holding the full code graph in memory, no shared state,
    no lock, no nice level

Severity scales with repo size and number of repos. On macOS, runaway
fseventsd traffic from rebuild output compounds the problem.

Code References (v0.7.10)

Issue 1 — No concurrency control on detached rebuild
hooks.py:71-91 — nohup graphify_python -c "..." > $log 2>&1 & then disown.
No lock file, no semaphore, no nice/ionice, no max-concurrent-rebuilds env.

Issue 2 — GRAPHIFY_CHANGED computed but discarded
hooks.py:75-87 — the inline script reads the env var, parses it into
changed: list[Path], then exits if empty. But the actual rebuild on line 87
calls _rebuild_code(Path('.'), force=_force) and ignores changed entirely.
_rebuild_code re-globs every code file every time. For a 446-file repo this
is ~5 s of CPU + heavy fseventsd traffic per commit, even when one file changed.

Issue 3 — No timeout on the background process
If a rebuild leaks memory or hangs (network volumes, Spotlight contention,
parser bug on a specific file), it stays alive indefinitely. macOS has no
default kill-after-N-minutes for detached nohup children.

Suggested Fix

  1. Concurrency lock — flock on ~/.cache/graphify/rebuild.lock (or
    per-repo lock in <repo>/graphify-out/.rebuild.lock). Skip if held; an
    in-progress rebuild covers later commits anyway.

  2. Honour GRAPHIFY_CHANGED — extend _rebuild_code signature:

    def _rebuild_code(
        watch_path: Path,
        *,
        changed_paths: list[Path] | None = None,
        follow_symlinks: bool = False,
        force: bool = False,
    ) -> bool: ...

    When changed_paths is provided, re-extract only those files and merge
    into the existing graph instead of rebuilding from scratch.

  3. Resource limits — nice -n 10, GRAPHIFY_REBUILD_TIMEOUT (default
    10 min), optional GRAPHIFY_REBUILD_MEMORY_LIMIT_MB enforced via
    resource.setrlimit(RLIMIT_AS, ...) in the inline script.

Happy to send a PR for any/all of these if there's interest.

Environment

  • macOS 15.4 (Sequoia), Apple Silicon Mac mini, 32 GB RAM
  • Python 3.14.4 (Homebrew)
  • graphify 0.7.10 (PyPI)
  • Hook installed across ~17 directories via graphify_watcher_worker.py

Activity

  1. safishamsi commented on May 9, 2026

    @safishamsi
    Member

    Fixed in v7. Three root causes addressed:

    1. Per-repo lock — _rebuild_code now acquires a fcntl.flock non-blocking lock in graphify-out/.rebuild.lock. If a rebuild is already running for that repo, the new invocation skips instead of spawning a second 4 GB process. The lock releases automatically on SIGKILL (kernel closes the fd) so no stale-lock cleanup is needed. Falls back to a no-op on Windows (no fcntl).

    2. Honour GRAPHIFY_CHANGED — changed_paths is now wired all the way from the hook script through _rebuild_code to the AST extractor. Only modified files are re-extracted; unchanged files come from the cache. Deleted files have their nodes evicted from the preserved graph by source_file match.

    3. Resource ceiling — GRAPHIFY_REBUILD_TIMEOUT (default 600s) watchdog via SIGALRM; GRAPHIFY_REBUILD_MEMORY_LIMIT_MB cap using RLIMIT_DATA on Darwin (not RLIMIT_AS which is broken under libmalloc) and RLIMIT_AS on Linux; nice -n 10 in the background script.

    Interactive graphify update passes block_on_lock=True so it waits instead of silently exiting when a hook rebuild is running.

    Your diagnosis was accurate — the original nohup ... & disown patch from #650 fixed the synchronous-block problem but left no concurrency story. This closes that gap.

  2. safishamsi commented on May 9, 2026

    @safishamsi
    Member

    Fixed in v0.7.12 — hooks now use a per-repo fcntl.flock non-blocking lock so concurrent commits skip rather than stack unbounded rebuilds. Added nice -n 10, RLIMIT_DATA/RLIMIT_AS memory cap, and SIGALRM watchdog timeout. graphify update uses block_on_lock=True to wait rather than silently skip.

  3. added a commit that references this issue on Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions