Skip to content

Commit 2d2570b

Browse files
jenshnielsenCopilot
andcommitted
fix(index): don't keep the repository alive after IndexFile.add()
`IndexFile._iter_expand_paths()` passes `os.walk()` an `onerror` callback that re-raises the error it receives. `os.walk()` fails for every file path, so each `index.add()` of a file raised through `raise_exc()` while its frame local `e` still referenced the exception. That forms the cycle exception -> `__traceback__` -> `raise_exc` frame -> exception, and the traceback also retains the `_iter_expand_paths` generator frame, whose `self` references the `IndexFile` and its `Repo`. A `Repo` used to stage a file was therefore only released by the cyclic garbage collector instead of by reference counting. Until then its persistent `git cat-file` processes stayed alive and held repository files open, which on Windows prevents deleting the repository. `Repo.close()` hid this by forcing `gc.collect()`. Delete the local in a `finally` block before the exception propagates, which breaks the cycle without changing which errors are raised or handled. The new test stages a file with the garbage collector disabled and checks that dropping the `Repo` frees it. It fails before this change and passes after it with both the CLI and Gix backends on Windows (Python 3.14). Agent-Model: unknown Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 57a7865 commit 2d2570b

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎git/index/base.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,12 @@ def _iter_expand_paths(self: "IndexFile", paths: Sequence[PathLike]) -> Iterator
464464
"""
465465

466466
def raise_exc(e: Exception) -> NoReturn:
467-
raise e
467+
try:
468+
raise e
469+
finally:
470+
# Break the exception -> traceback -> frame -> exception cycle, which
471+
# would otherwise keep this index and its repository alive.
472+
del e
468473

469474
r = str(self.repo.working_tree_dir)
470475
rs = r + os.sep

‎test/test_index.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

66
import contextlib
7+
import gc
78
import logging
89
import os
910
import os.path as osp
1011
import sys
12+
import weakref
1113
from itertools import product
1214
from pathlib import Path
1315
from stat import S_ISLNK, ST_MODE
@@ -52,6 +54,21 @@ def test_staged_legacy_filename_and_blob_bytes():
5254
}
5355

5456

57+
def test_adding_a_file_does_not_keep_the_repository_alive():
58+
# Repo.close() does not force a garbage collection, so staging must not leave a
59+
# reference cycle that delays releasing a dropped repository's Git processes.
60+
with fixture_repo("index.sh", "file", "testfile.txt", "content of testfile", write=True) as owner:
61+
gc.disable()
62+
try:
63+
repo = Repo(owner.working_dir)
64+
repo.index.add(["testfile.txt"])
65+
dropped = weakref.ref(repo)
66+
del repo
67+
assert dropped() is None
68+
finally:
69+
gc.enable()
70+
71+
5572
@pytest.mark.skipif(os.name == "nt", reason="Windows symlink targets are Unicode paths")
5673
def test_staging_preserves_non_utf8_symlink_targets():
5774
if not symlinks_supported():

0 commit comments

Comments
 (0)