Skip to content

Prefetch merge-base blob in stable-shim linters to avoid mid-diff lazy fetch - #191228

Closed
ezyang wants to merge 1 commit into
gh/ezyang/3517/basefrom
gh/ezyang/3517/head
Closed

ezyang wants to merge 1 commit into
gh/ezyang/3517/basefrom
gh/ezyang/3517/head

Conversation

@ezyang

@ezyang ezyang commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

The STABLE_SHIM_VERSION linter intermittently fails in CI with
subprocess.TimeoutExpired on git diff <merge_base>..HEAD -- shim.h,
reported as an internal linter bug.

Root cause: CI checks out a blobless partial clone (--filter=blob:none, added
in #172544; see .github/actions/checkout-pytorch/action.yml). The 5s diff
timeout predates that (added with the linter in #166995) and was correct then --
the diff was purely local. merge_base_with_main() fetches the merge-base
commit ahead of time, but the blobless filter deliberately omits file contents
even for explicitly fetched commits, so the merge-base blob of the linted file
is never local. When git diff needs it, git triggers an on-demand lazy fetch
in the middle of the command -- an uncontrolled network round-trip that races
the 5s timeout under the network contention of lintrunner running adapters
concurrently.

This is the same partial-clone symptom #189419 already fixed for the sibling
git cat-file probe in merge_base_with_main(), which was simply never applied
to the git diff/git show paths. #189419 established the design: keep a short
timeout as a fast "is it local?" probe and, on absence, fall back to an
explicit, controlled git fetch. #189444 tried to instead bump the probe
timeout to wait the lazy fetch out; #189946 reverted that, on the grounds that a
timeout is the signal that the object is not present, not something to wait out.
So bumping the diff timeout (or wrapping the diff in the retrying fetch helper to
wait it out) would repeat the already-reverted mistake.

Fix: apply #189419's design to the missing blob. ensure_diff_blob_local()
resolves the blob OID offline (trees are present under blob:none) and, only if
it is missing locally, fetches that single object through the existing retrying
network helper; the diff/show then runs entirely from local objects with a
short, honest timeout. GIT_NO_LAZY_FETCH=1 guards the resolution and presence
checks so they cannot themselves trigger a fetch -- a deterministic improvement
over the cat-file probe's timeout race. In a full clone the blob is already
present and this is a no-op with no network access. The generated-shims linter's
prior 60s git show workaround is replaced by the same prefetch + 5s timeout.

Test Plan:
Verified locally in a full clone that the helper is a no-op and the git command
stays local and fast:

python tools/linter/adapters/stable_shim_version_linter.py \
    torch/csrc/inductor/aoti_torch/c/shim.h

End-to-end validation on a blobless partial clone still needs to run (the
sandbox has no network); it must confirm the single-blob
git fetch --no-write-fetch-head origin <oid> succeeds against GitHub's
promisor endpoint. If the explicit single-object fetch is rejected there, the
fallback is to trigger git's native lazy fetch through the same retry wrapper
(e.g. git cat-file -p <oid>).

Authored-with: Claude (AI assistant)

…d-diff lazy fetch

Rework the fix into a shared helper and apply it uniformly across both
stable-shim linter adapters.

Add `ensure_diff_blob_local(commit, filename)` to `_stable_shim_utils.py`.
It resolves the blob OID offline (trees are present under a blobless
partial clone) and, only when that object is missing locally, fetches
exactly that one blob through the existing retrying network helper.
`GIT_NO_LAZY_FETCH=1` guards the rev-parse and cat-file probes so the
local resolution and presence checks cannot themselves trigger a fetch.
Paths outside the repo, files that don't exist at the merge-base, and
full (non-partial) clones all short-circuit to a no-op with no network
access.

Wire the helper into both adapters ahead of the git command that reads
the merge-base contents:

- `stable_shim_version_linter.py`: prefetch before the
  `git diff <merge_base>..HEAD` in `get_added_lines()`.
- `generated_shims_version_linter.py`: prefetch before the `git show`
  in `_read_at_merge_base()`, and drop that command's prior 60s
  lazy-fetch workaround back to a short 5s timeout now that the blob is
  guaranteed local beforehand.

This keeps the git commands running entirely from local objects behind
an honest short timeout, rather than waiting out an uncontrolled
mid-command lazy fetch.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/191228

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 14 Pending

As of commit 6fb848a with merge base 9a036ca (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@albanD albanD left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

SGTM

@ezyang ezyang closed this Jul 27, 2026
@ezyang ezyang reopened this Jul 27, 2026
["git", "rev-parse", f"{commit}:{rel}"],
capture_output=True,
text=True,
timeout=5,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I do wonder if gitpython bindings would give us more info so we did not need to rely on these timeouts?

@Skylion007

Copy link
Copy Markdown
Collaborator

@claude would gitpython library or something similar give us more signal and prevent us from having to rely on timeouts?

@claude

claude Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

Claude finished @Skylion007's task in 4m 44s —— View job


Would GitPython (or similar) give more signal and remove the timeout reliance?

Short answer: it would remove one timeout, but not by virtue of being a library — and this PR already removes that same timeout more cheaply with GIT_NO_LAZY_FETCH=1. I don't think a new dependency buys anything here.

Details

1. GitPython's repo.git.* API is literally the same subprocess. Repo.git.diff(...) builds an argv and runs the git binary. Same lazy fetch, same network stall, same need for a timeout (GitPython spells it kill_after_timeout). Anything that goes through git.cmd.Git inherits every problem this PR is fixing, plus GitPython's own footguns (it's easy to leak git cat-file --batch child processes if you don't repo.close(), and its structured diff objects are a different parse than the unified-diff text stable_shim_version_linter.py already consumes to compute added line numbers).

2. The part that would give a clean signal is the pure-Python object DB — repo.odb/repo.tree()[path], or more so pygit2/dulwich. Those read loose + packed objects in-process and, critically, have no promisor/partial-clone support: a missing blob raises BadObject immediately rather than triggering a network fetch. That's a deterministic "not local" answer instead of a timeout probe.

But that is exactly what ensure_diff_blob_local already gets from GIT_NO_LAZY_FETCH=1 + git cat-file -e (_stable_shim_utils.py:507-514): git returns non-zero right away instead of dialing out. Same determinism, zero new deps, and it stays on the same git binary/config as the rest of the linter (so .git/objects/info/alternates, worktrees, core.* overrides all behave identically). A third-party ODB reader also can't do the repair step — you still need git fetch to actually get the blob, so you'd end up with both anyway.

3. Dependency cost is not zero. Every adapter under tools/linter/adapters/ is stdlib-only today; GitPython appears in the repo only inside .ci/lumen_cli/pyproject.toml. Pulling it into the lint path means an init_command install on every lint job and every contributor's lintrunner init, to save one env var.

So: the timeout reliance worth eliminating is the "timeout means missing" probe, not the timeouts on local commands. After this PR the 5s timeouts on git diff / git show are guarding commands that are guaranteed local — that's an honest backstop, not a signal channel, and no library changes that.

One concrete follow-up in the same spirit (no new dep): the last remaining timeout-as-signal is the pre-existing commit probe in merge_base_with_main() (_stable_shim_utils.py:540-553), which still catches TimeoutExpired and interprets it as "not present locally" — the exact pattern #189419/#189946 pushed back on. Adding the same GIT_NO_LAZY_FETCH=1 env there makes it deterministic and lets the except TimeoutExpired branch go away:

no_lazy = {**os.environ, "GIT_NO_LAZY_FETCH": "1"}
commit_missing = subprocess.run(
    ["git", "cat-file", "-e", f"{main_sha}^{{commit}}"],
    capture_output=True, timeout=5, env=no_lazy,
).returncode != 0

(GIT_NO_LAZY_FETCH needs git >= 2.36, which is well below what CI and any supported dev setup runs. Worth hoisting no_lazy to a module-level helper if both call sites use it.) Fix this →

Answering @Skylion007's question — no: GitPython's repo.git.* API is the same subprocess with the same lazy-fetch stall, and the pure-Python object readers (GitPython's odb, pygit2, dulwich) only give the deterministic "blob is missing" signal that GIT_NO_LAZY_FETCH=1 in this PR already provides for free; the one follow-up worth doing is applying that same env var to the leftover timeout-as-signal probe in merge_base_with_main().
| Branch: gh/ezyang/3517/head

@ezyang

ezyang commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -f "linter-only change (stable-shim adapter scripts); Lint is green, other CI not required"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…y fetch (pytorch#191228)

The STABLE_SHIM_VERSION linter intermittently fails in CI with
`subprocess.TimeoutExpired` on `git diff <merge_base>..HEAD -- shim.h`,
reported as an internal linter bug.

Root cause: CI checks out a blobless partial clone (`--filter=blob:none`, added
in pytorch#172544; see .github/actions/checkout-pytorch/action.yml). The 5s diff
timeout predates that (added with the linter in pytorch#166995) and was correct then --
the diff was purely local. `merge_base_with_main()` fetches the merge-base
*commit* ahead of time, but the blobless filter deliberately omits file contents
even for explicitly fetched commits, so the merge-base blob of the linted file
is never local. When `git diff` needs it, git triggers an on-demand lazy fetch
in the middle of the command -- an uncontrolled network round-trip that races
the 5s timeout under the network contention of lintrunner running adapters
concurrently.

This is the same partial-clone symptom pytorch#189419 already fixed for the sibling
`git cat-file` probe in `merge_base_with_main()`, which was simply never applied
to the `git diff`/`git show` paths. pytorch#189419 established the design: keep a short
timeout as a fast "is it local?" probe and, on absence, fall back to an
explicit, controlled `git fetch`. pytorch#189444 tried to instead bump the probe
timeout to wait the lazy fetch out; pytorch#189946 reverted that, on the grounds that a
timeout is the signal that the object is not present, not something to wait out.
So bumping the diff timeout (or wrapping the diff in the retrying fetch helper to
wait it out) would repeat the already-reverted mistake.

Fix: apply pytorch#189419's design to the missing blob. `ensure_diff_blob_local()`
resolves the blob OID offline (trees are present under blob:none) and, only if
it is missing locally, fetches that single object through the existing retrying
network helper; the diff/show then runs entirely from local objects with a
short, honest timeout. `GIT_NO_LAZY_FETCH=1` guards the resolution and presence
checks so they cannot themselves trigger a fetch -- a deterministic improvement
over the cat-file probe's timeout race. In a full clone the blob is already
present and this is a no-op with no network access. The generated-shims linter's
prior 60s `git show` workaround is replaced by the same prefetch + 5s timeout.

Test Plan:
Verified locally in a full clone that the helper is a no-op and the git command
stays local and fast:

```
python tools/linter/adapters/stable_shim_version_linter.py \
    torch/csrc/inductor/aoti_torch/c/shim.h
```

End-to-end validation on a blobless partial clone still needs to run (the
sandbox has no network); it must confirm the single-blob
`git fetch --no-write-fetch-head origin <oid>` succeeds against GitHub's
promisor endpoint. If the explicit single-object fetch is rejected there, the
fallback is to trigger git's native lazy fetch through the same retry wrapper
(e.g. `git cat-file -p <oid>`).

Authored-with: Claude (AI assistant)
Pull Request resolved: pytorch#191228
Approved by: https://github.com/albanD, https://github.com/Skylion007, https://github.com/janeyx99
janeyx99 added a commit that referenced this pull request Jul 29, 2026
We've tried a lot of partial fixes to this partial clone problem,  which then caused more holes that required patching. The latest was: #191228 which attempted to enforce certain git commands be local only. This was too strict, where the historical versions of files would error (see https://github.com/pytorch/pytorch/actions/runs/30380274127/job/90346227990).

Codex and I tried to solve this problem once and for all with a small redesign. We have separated out the git commands that should require network, and the ones that should not:

Must remain offline: refs + commits, which we should trust our checkout.
  - merge_base_with_main() `GIT_NO_LAZY_FETCH=1 git merge-base HEAD refs/remotes/origin/main`
  - Working-tree diff: `GIT_NO_LAZY_FETCH=1 git diff HEAD -- <file>`
  - First attempt of every historical object command.

May contact the network: actual file content.
  - Latter attempts of every historical object command, assuming the first local attempt failed:
```
git diff <merge-base>..HEAD -- <file>
git ls-tree -z <revision> -- <path>
git cat-file blob <oid>
```

Removed:
  - `git ls-remote`: trust the local main, do not refetch main.
  - Explicit `git fetch origin <object-oid>`: GitHub can reject this request if it's a blob vs a commit/branch. We replace this with cat-file.
  - Fetching or updating origin/main or any linter-managed ref updates: lint runs concurrently, don't step over each other's toes

I'm relatively confident about the must remain offline, because our checkout for lint does the following:
  - setup-linux defaults to checkout-mode: treeless.
  - checkout-pytorch defaults to fetch-depth: 0, meaning full commit history.
  - origin/main is fetched by checkout.

[ghstack-poisoned]
pytorchmergebot pushed a commit that referenced this pull request Jul 29, 2026
We've tried a lot of partial fixes to this partial clone problem,  which then caused more holes that required patching. The latest was: #191228 which attempted to enforce certain git commands be local only. This was too strict, where the historical versions of files would error (see https://github.com/pytorch/pytorch/actions/runs/30380274127/job/90346227990).

Codex and I tried to solve this problem once and for all with a small redesign. We have separated out the git commands that should require network, and the ones that should not:

Must remain offline: refs + commits, which we should trust our checkout.
  - merge_base_with_main() `GIT_NO_LAZY_FETCH=1 git merge-base HEAD refs/remotes/origin/main`
  - Working-tree diff: `GIT_NO_LAZY_FETCH=1 git diff HEAD -- <file>`
  - First attempt of every historical object command.

May contact the network: actual file content.
  - Latter attempts of every historical object command, assuming the first local attempt failed:
```
git diff <merge-base>..HEAD -- <file>
git ls-tree -z <revision> -- <path>
git cat-file blob <oid>
```

Removed:
  - `git ls-remote`: trust the local main, do not refetch main.
  - Explicit `git fetch origin <object-oid>`: GitHub can reject this request if it's a blob vs a commit/branch. We replace this with cat-file.
  - Fetching or updating origin/main or any linter-managed ref updates: lint runs concurrently, don't step over each other's toes

I'm relatively confident about the must remain offline, because our checkout for lint does the following:
  - setup-linux defaults to checkout-mode: treeless.
  - checkout-pytorch defaults to fetch-depth: 0, meaning full commit history.
  - origin/main is fetched by checkout.
Pull Request resolved: #191364
Approved by: https://github.com/albanD
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
…y fetch (pytorch#191228)

The STABLE_SHIM_VERSION linter intermittently fails in CI with
`subprocess.TimeoutExpired` on `git diff <merge_base>..HEAD -- shim.h`,
reported as an internal linter bug.

Root cause: CI checks out a blobless partial clone (`--filter=blob:none`, added
in pytorch#172544; see .github/actions/checkout-pytorch/action.yml). The 5s diff
timeout predates that (added with the linter in pytorch#166995) and was correct then --
the diff was purely local. `merge_base_with_main()` fetches the merge-base
*commit* ahead of time, but the blobless filter deliberately omits file contents
even for explicitly fetched commits, so the merge-base blob of the linted file
is never local. When `git diff` needs it, git triggers an on-demand lazy fetch
in the middle of the command -- an uncontrolled network round-trip that races
the 5s timeout under the network contention of lintrunner running adapters
concurrently.

This is the same partial-clone symptom pytorch#189419 already fixed for the sibling
`git cat-file` probe in `merge_base_with_main()`, which was simply never applied
to the `git diff`/`git show` paths. pytorch#189419 established the design: keep a short
timeout as a fast "is it local?" probe and, on absence, fall back to an
explicit, controlled `git fetch`. pytorch#189444 tried to instead bump the probe
timeout to wait the lazy fetch out; pytorch#189946 reverted that, on the grounds that a
timeout is the signal that the object is not present, not something to wait out.
So bumping the diff timeout (or wrapping the diff in the retrying fetch helper to
wait it out) would repeat the already-reverted mistake.

Fix: apply pytorch#189419's design to the missing blob. `ensure_diff_blob_local()`
resolves the blob OID offline (trees are present under blob:none) and, only if
it is missing locally, fetches that single object through the existing retrying
network helper; the diff/show then runs entirely from local objects with a
short, honest timeout. `GIT_NO_LAZY_FETCH=1` guards the resolution and presence
checks so they cannot themselves trigger a fetch -- a deterministic improvement
over the cat-file probe's timeout race. In a full clone the blob is already
present and this is a no-op with no network access. The generated-shims linter's
prior 60s `git show` workaround is replaced by the same prefetch + 5s timeout.

Test Plan:
Verified locally in a full clone that the helper is a no-op and the git command
stays local and fast:

```
python tools/linter/adapters/stable_shim_version_linter.py \
    torch/csrc/inductor/aoti_torch/c/shim.h
```

End-to-end validation on a blobless partial clone still needs to run (the
sandbox has no network); it must confirm the single-blob
`git fetch --no-write-fetch-head origin <oid>` succeeds against GitHub's
promisor endpoint. If the explicit single-object fetch is rejected there, the
fallback is to trigger git's native lazy fetch through the same retry wrapper
(e.g. `git cat-file -p <oid>`).

Authored-with: Claude (AI assistant)
Pull Request resolved: pytorch#191228
Approved by: https://github.com/albanD, https://github.com/Skylion007, https://github.com/janeyx99
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
We've tried a lot of partial fixes to this partial clone problem,  which then caused more holes that required patching. The latest was: pytorch#191228 which attempted to enforce certain git commands be local only. This was too strict, where the historical versions of files would error (see https://github.com/pytorch/pytorch/actions/runs/30380274127/job/90346227990).

Codex and I tried to solve this problem once and for all with a small redesign. We have separated out the git commands that should require network, and the ones that should not:

Must remain offline: refs + commits, which we should trust our checkout.
  - merge_base_with_main() `GIT_NO_LAZY_FETCH=1 git merge-base HEAD refs/remotes/origin/main`
  - Working-tree diff: `GIT_NO_LAZY_FETCH=1 git diff HEAD -- <file>`
  - First attempt of every historical object command.

May contact the network: actual file content.
  - Latter attempts of every historical object command, assuming the first local attempt failed:
```
git diff <merge-base>..HEAD -- <file>
git ls-tree -z <revision> -- <path>
git cat-file blob <oid>
```

Removed:
  - `git ls-remote`: trust the local main, do not refetch main.
  - Explicit `git fetch origin <object-oid>`: GitHub can reject this request if it's a blob vs a commit/branch. We replace this with cat-file.
  - Fetching or updating origin/main or any linter-managed ref updates: lint runs concurrently, don't step over each other's toes

I'm relatively confident about the must remain offline, because our checkout for lint does the following:
  - setup-linux defaults to checkout-mode: treeless.
  - checkout-pytorch defaults to fetch-depth: 0, meaning full commit history.
  - origin/main is fetched by checkout.
Pull Request resolved: pytorch#191364
Approved by: https://github.com/albanD
@github-actions
github-actions Bot deleted the gh/ezyang/3517/head branch August 27, 2026 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants