fix(gitengine): thread context through history walking - #25
Merged
Merged
Conversation
14 tasks
hammadmajid
force-pushed
the
fix/13-gitengine-context
branch
from
September 5, 2026 14:14
f96118a to
6b26a6c
Compare
The entire history-walk phase was uncancellable, making it the primary Ctrl-C dead zone. `collectOrderedCommits` BFS-walked the whole commit DAG -- a packfile seek plus zlib inflate per commit -- before a single occurrence reached the caller, and `cmd/grg/main.go` checked `ctx.Err()` only inside the per-occurrence callback, which was never invoked until that phase completed. With `--all` every ref is seeded, so on a large repository SIGINT had literally no effect. `traverseExclude` was a second unbounded uncancellable walk, reached once per exclude of an `A..B` rev-range. Even during tree traversal the callback was the sole cancellation point while long stretches did real I/O and emitted nothing: subtree-OID pruning, the merge-commit `FindTreeEntry` probe per extra parent, and full-tree recursion in `traverseTreeHelper`. `HistoryWalker.Walk` now takes a leading `context.Context` and threads it through `collectOrderedCommits`, `traverseExclude` (which gains an error return), `walkCommitBlobs`, `diffTreesAndEmit`, and `emitBlobOccurrence`, plus `TraverseTree`/`traverseTreeHelper` and `FindTreeEntry` in tree.go. Guards are plain `ctx.Err()` loads rather than `select`, placed at loop heads where they gate the `ReadObject` that follows and never inside the innermost per-entry comparison, so they cost nothing next to the I/O they guard. The redundant `ctx.Err()` check inside the callback in `runContext` is gone; the surrounding `errors.Is(err, context.Canceled)` handling is unchanged. Signature call sites in existing tests were updated mechanically. New tests assert on work actually performed via a counting `ObjectReader`: cancellation during commit collection stops within a few reads of the trip point and emits nothing (the phase that was completely deaf), mid-walk cancellation stops after the emitting commit and reads far fewer objects than a full drain, and the exclude walk and tree traversal both abort instead of draining. Removing only the `collectOrderedCommits` guard makes the collection test fail on read count. Closes #13
hammadmajid
force-pushed
the
fix/13-gitengine-context
branch
from
September 5, 2026 14:16
6b26a6c to
1d9862e
Compare
hammadmajid
marked this pull request as ready for review
September 5, 2026 14:19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The entire history-walk phase was deaf to cancellation. On a large repository this is the dominant phase, so Ctrl-C did nothing at all.
Closes #13
Type of Change
Key Changes
HistoryWalkerhad noctxfield andWalktook no context.collectOrderedCommitsBFS-walked the whole commit DAG, doing a packfile seek plus zlib inflate per commit, before a single occurrence reached the caller — and the caller's only cancellation check lived inside the per-occurrence callback, which was never invoked until that phase completed. With--all, every ref is seeded.traverseExcludewas a second unbounded uncancellable walk, reached for every exclude in anA..Brev-range, sov1.0..HEADwalked the entire ancestry ofv1.0.Even during tree traversal the callback was the only checkpoint, and long stretches did real I/O while emitting nothing:
diffTreesAndEmitprunes identical subtrees,emitBlobOccurrence's merge check callsreadCommitandFindTreeEntry(which loopsReadObjectper path component) for every extra parent of every merge commit, andtraverseTreeHelperrecurses over full trees.Threaded
context.ContextthroughWalk,collectOrderedCommits,traverseExclude(which gains an error return),walkCommitBlobs,diffTreesAndEmit,emitBlobOccurrence,TraverseTree,traverseTreeHelper, andFindTreeEntry.Guards are plain
ctx.Err()reads, neverselect: these are non-blocking hot paths and an atomic load is free next to theReadObjectit guards. They sit where they gate I/O — head offor pq.Len() > 0, head oftraverseExclude's stack loop, head ofWalk's per-commit loop, entry ofdiffTreesAndEmitbefore the tree read pair, head of the extra-parent loop inemitBlobOccurrence, and the two tree loops — and deliberately not inside the two-pointer entry comparison, where the check would dominate the work.The call site in
runContextbecomeswalker.Walk(ctx, ...)and the now-redundant in-callback check is removed.Verification & Testing
go test -v -count=1 ./...go test -race -shuffle=on -count=1 ./...go vet ./...New tests assert work actually performed, via a counting
ObjectReader— not just the returned error:TestHistoryWalkerCancelDuringCommitCollection— 400-commit history, cancel on read 20; assertscontext.Canceled, zero occurrences emitted, and ≤ 25 of 400 reads. This is the phase that was completely deaf.TestHistoryWalkerCancelMidWalk— measures an uncancelled baseline first, then cancels from the callback while returningnil, so the walker itself must observe it; asserts ≤ 5 reads after cancel and total < baseline.TestTraverseExcludeCancellation— the exclude side of anA..Brange aborts instead of draining the ancestry.TestTraverseTreeCancellation— tree traversal stops at the entry that cancelled.Mutation: stripping all four guards fails all four tests. Stripping only the
collectOrderedCommitsguard failsTestHistoryWalkerCancelDuringCommitCollectionwith400 reads (cancelled at 20) out of 400 commits— proving that assertion defends that specific phase rather than just the returned error.Smoke: built
./cmd/grgand rangrg --all 'HistoryWalker'against a real repository; returns hits end to end.Note
Stacked on #24. Together with #24 this is what makes Ctrl-C actually work: this PR makes the first signal effective, #24 makes the second one an escape hatch.
Checklist
gofmtclean