Skip to content

fix(hooks): arm the rebuild timeout without SIGALRM on Windows - #2161

Closed
Rishet11 wants to merge 2 commits into
Graphify-Labs:v8from
Rishet11:fix/2148-windows-rebuild-timeout
Closed

Rishet11 wants to merge 2 commits into
Graphify-Labs:v8from
Rishet11:fix/2148-windows-rebuild-timeout

Conversation

@Rishet11

Copy link
Copy Markdown
Contributor

Problem

GRAPHIFY_REBUILD_TIMEOUT is a silent no-op on Windows, so a hung rebuild survives indefinitely — the exact tail case the #791 timeout was added to catch.

Both embedded rebuild bodies in graphify/hooks.py (_REBUILD_BODY_COMMIT, _REBUILD_BODY_CHECKOUT) armed the watchdog like this:

if _timeout > 0 and hasattr(signal, 'SIGALRM'):
    signal.signal(signal.SIGALRM, ...)
    signal.alarm(_timeout)

Root cause: signal.SIGALRM does not exist on Windows and there is no else branch, so the whole guard is skipped and no watchdog is ever armed. The env var is read, then ignored.

@epocamx measured this on a real repo with a ~20-min auto-commit daemon: 916 rebuild starts, 909 clean finishes, 0 timeout lines ever logged, 7 starts with no matching finish, and at one snapshot 16 detached rebuild processes still alive 10+ hours past their logged completion, regardless of the GRAPHIFY_REBUILD_TIMEOUT value.

Fix

Nest the existing check and add a threading.Timer fallback for the non-SIGALRM case, in both bodies (the two blocks were byte-identical, and remain so):

    _timeout = int(os.environ.get('GRAPHIFY_REBUILD_TIMEOUT', '600'))
    if _timeout > 0:
        if hasattr(signal, 'SIGALRM'):
            signal.signal(signal.SIGALRM, lambda *_: (_ for _ in ()).throw(TimeoutError(f'graphify rebuild exceeded {_timeout}s')))
            signal.alarm(_timeout)
        else:
            def _bail():
                print(f'[graphify hook] graphify rebuild exceeded {_timeout}s', flush=True)
                os._exit(1)
            _watchdog = threading.Timer(_timeout, _bail)
            _watchdog.daemon = True
            _watchdog.start()

threading added to each body's existing import os, signal, sys line.

Three notes on the choices, since they are deliberate:

  • os._exit(1) rather than an exception. The process being killed is already stuck, so a clean shutdown may itself be blocked. It is also safe here specifically: _rebuild_lock in graphify/watch.py "falls back to a no-op yield(True) on platforms without fcntl (Windows)" (its own docstring), so on the only platform that reaches this branch there is no lock to leave stale. graph.json writes go through write_json_atomic, so an abrupt exit cannot truncate a good graph.
  • The explicit print. os._exit skips the surrounding except TimeoutError handler that logs on the POSIX path, so without it the kill would be silent. Message text and destination match the SIGALRM path exactly; flush=True because os._exit skips the normal stdio flush.
  • daemon = True, and the timer is never cancelled. This is a one-shot hook script, so the daemon thread dies with the interpreter on a normal finish and cannot fire after the rebuild it guards has already exited.

Both bodies are carried inside a shell double-quoted -c "..." argument, so the added lines contain no ", $, backtick or backslash, and no ''' — test_rebuild_bodies_are_shell_quote_safe covers this and passes.

Testing

Added test_rebuild_bodies_arm_a_timeout_without_sigalrm in tests/test_hooks.py, parametrized over both bodies so patching only one still fails.

It asserts structurally via ast, not by substring presence: it finds the if whose test mentions 'SIGALRM', requires a non-empty orelse, and requires threading.Timer and os._exit to appear inside that orelse. I verified it actually fails on wrong implementations:

wrong (Timer at unconditional top level)  -> REJECT: no else-branch
wrong (else-branch, but no os._exit)      -> REJECT: no os._exit in else
real post-commit body                     -> ACCEPT
real post-checkout body                   -> ACCEPT

tests/test_hooks.py: 12 failed, 48 passed, versus 12 failed, 46 passed before the change — the same 12 pre-existing failures, plus my 2 new cases. Those 12 are local to my machine (a broken editable install: uv run graphify --help fails with ModuleNotFoundError: No module named 'graphify' both with and without this change), not introduced here.

Full suite, same environment, both commits:

2fa6cd3 (base) : 103 failed, 3448 passed, 33 skipped
cfdef07 (this) : 102 failed, 3451 passed, 33 skipped

Collected count rises by exactly 2. No new failures; one pre-existing failure flipped to pass, which looks flaky and is unrelated.

Scope

Hook rebuild bodies only. graphify watch has its own timeout path and is untouched — happy to extend this there in a follow-up if you'd like it covered too.

Fixes #2148

The GRAPHIFY_REBUILD_TIMEOUT watchdog in both embedded rebuild bodies was
guarded by hasattr(signal, 'SIGALRM') with no else branch, so on Windows it
never armed and a hung rebuild survived indefinitely -- the exact tail case
the Graphify-Labs#791 timeout was added to catch.

Fall back to a daemon threading.Timer that prints the same message and calls
os._exit(1). The hard exit is deliberate: the process is already stuck, so a
clean shutdown may itself be blocked, and _rebuild_lock degrades to a no-op
yield on platforms without fcntl, so there is no lock to leave stale.
Copilot AI review requested due to automatic review settings July 24, 2026 20:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes GRAPHIFY_REBUILD_TIMEOUT being a no-op on Windows by adding a portable watchdog fallback to the embedded post-commit and post-checkout rebuild bodies, ensuring hung rebuild subprocesses are terminated even when signal.SIGALRM is unavailable.

Changes:

  • Added a threading.Timer-based watchdog fallback (using os._exit(1)) when SIGALRM is not present, in both embedded rebuild bodies.
  • Updated the embedded scripts’ imports to include threading.
  • Added an AST-structural test that enforces the fallback lives specifically in the else branch of the SIGALRM check and includes both threading.Timer and os._exit.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
graphify/hooks.py Arms the rebuild timeout on Windows by adding a threading.Timer watchdog fallback in both embedded rebuild bodies.
tests/test_hooks.py Adds an AST-based regression test ensuring the non-SIGALRM timeout fallback exists and is structured correctly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread graphify/hooks.py Resolved
The post-checkout body logs with [graphify] throughout, but the new
non-SIGALRM fallback used [graphify hook], so the same timeout read
differently depending on the platform. The post-commit body does use
[graphify hook] everywhere, so only the checkout copy was wrong.

Assert each body uses a single log prefix so this cannot drift again.
@safishamsi

Copy link
Copy Markdown
Member

Thanks @Rishet11. Shipped in v0.9.26 (cherry-picked to v8 as ca3113a, 0da6929 to preserve your authorship). Closed-unmerged here, but it is in the release: https://github.com/Graphify-Labs/graphify/releases/tag/v0.9.26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Post-commit hook rebuild timeout is a no-op on Windows (SIGALRM guard) — #791 mitigation never applies there

3 participants