Skip to content

Commit d7fd408

Browse files
jenshnielsenCopilot
andcommitted
perf(repo): stop forcing garbage collections in Repo.close()
`Repo.close()`, used by `__exit__` and `__del__`, called `gc.collect()` twice on Windows to release handles held by unreachable objects. Each call walks the whole Python heap. With 3 million live objects, closing a repository cost about 1.4 seconds in an application. A dropped `Repo` is freed by reference counting, so `__del__` paid this cost outside any automatic collection. The handles came from objects that GitPython left in reference cycles. Running the Windows test suite without any `gc.collect()` calls, and recording every failed temporary directory removal, found these sources: - The `IndexFile.add()` exception cycle fixed in the previous commit kept the repository and its `git cat-file` processes alive. It caused the observed library leaks and the file lock that `test_root_module` is marked xfail for. - `GitCmdObjectDB` reads objects only through Git, so no smmap windows or memory maps remain; `gitdb.util.mman.collect()` is kept because it is cheap. - The remaining leaks were test artifacts: `MagicMock` call records and logged exceptions retained by pytest. The test harness keeps its own collections. `close()` now only stops the persistent Git processes and releases unused gitdb memory maps. `Submodule.remove()` closes the module repository before deleting its checkout, as its other deletion paths already do, instead of collecting. The changelog documents that callers must close or release their own objects that are only reachable through reference cycles. With 3 million live objects on Windows and Python 3.14, explicit `close()` and dropping a `Repo` take about 2 ms (CLI backend) or under 0.1 ms (Gix backend) instead of about 225 ms. Without any collection in `Repo.close()` or the test harness, the full Windows suite passed with the CLI backend (2127 passed). With this change, the affected index, documentation and submodule tests pass, except for an order-dependent `test_git_submodule_compatibility` failure that also occurs on `main`. Agent-Model: unknown Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2d2570b commit d7fd408

3 files changed

Lines changed: 8 additions & 12 deletions

File tree

‎doc/source/changes.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ API changes
225225
``os.path.expandvars`` if wanted; initial ``~`` expansion remains.
226226
``HIDE_WINDOWS_KNOWN_ERRORS`` and ``HIDE_WINDOWS_FREEZE_ERRORS`` are removed.
227227
``rmtree`` propagates filesystem errors rather than raising ``SkipTest``.
228+
* ``Repo.close()``, and therefore ``with Repo(...)`` and dropping a ``Repo``, no
229+
longer forces two full garbage collections on Windows. It still stops the
230+
repository's persistent Git processes. ``IndexFile.add()`` no longer leaves a
231+
reference cycle that kept the index and repository alive until the next
232+
collection. Close files and repositories you opened yourself before removing a
233+
repository on Windows; call ``gc.collect()`` only if your own objects are
234+
reachable solely through reference cycles.
228235

229236
3.2.1
230237
=====

‎git/objects/submodule/base.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
__all__ = ["Submodule", "UpdateProgress"]
55

6-
import gc
76
import logging
87
import ntpath
98
import os
@@ -1553,8 +1552,7 @@ def remove(
15531552
if not dry_run:
15541553
self._clear_cache()
15551554
wtd = mod.working_tree_dir
1556-
del mod # Release file-handles (Windows).
1557-
gc.collect()
1555+
mod.close()
15581556
rmtree(str(wtd))
15591557
# END delete tree if possible
15601558
# END handle force

‎git/repo/base.py‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
__all__ = ["Repo"]
99

10-
import gc
1110
from io import BytesIO
1211
import logging
1312
import os
@@ -529,15 +528,7 @@ def close(self) -> None:
529528
"""
530529
if self.git:
531530
self.git.clear_cache()
532-
# Tempfiles objects on Windows are holding references to open files until
533-
# they are collected by the garbage collector, thus preventing deletion.
534-
# TODO: Find these references and ensure they are closed and deleted
535-
# synchronously rather than forcing a gc collection.
536-
if sys.platform == "win32":
537-
gc.collect()
538531
gitdb.util.mman.collect()
539-
if sys.platform == "win32":
540-
gc.collect()
541532

542533
def __eq__(self, rhs: object) -> bool:
543534
if isinstance(rhs, Repo):

0 commit comments

Comments
 (0)