diff --git a/doc/source/changes.rst b/doc/source/changes.rst index b0ba5e2a3..f9e8a13b2 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -225,6 +225,13 @@ API changes ``os.path.expandvars`` if wanted; initial ``~`` expansion remains. ``HIDE_WINDOWS_KNOWN_ERRORS`` and ``HIDE_WINDOWS_FREEZE_ERRORS`` are removed. ``rmtree`` propagates filesystem errors rather than raising ``SkipTest``. +* ``Repo.close()``, and therefore ``with Repo(...)`` and dropping a ``Repo``, no + longer forces two full garbage collections on Windows. It still stops the + repository's persistent Git processes. ``IndexFile.add()`` no longer leaves a + reference cycle that kept the index and repository alive until the next + collection. Close files and repositories you opened yourself before removing a + repository on Windows; call ``gc.collect()`` only if your own objects are + reachable solely through reference cycles. 3.2.1 ===== diff --git a/git/index/base.py b/git/index/base.py index 93300b71c..4f522b356 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -464,7 +464,12 @@ def _iter_expand_paths(self: "IndexFile", paths: Sequence[PathLike]) -> Iterator """ def raise_exc(e: Exception) -> NoReturn: - raise e + try: + raise e + finally: + # Break the exception -> traceback -> frame -> exception cycle, which + # would otherwise keep this index and its repository alive. + del e r = str(self.repo.working_tree_dir) rs = r + os.sep diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 840f32d50..615b9b777 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -3,7 +3,6 @@ __all__ = ["Submodule", "UpdateProgress"] -import gc import logging import ntpath import os @@ -1553,8 +1552,7 @@ def remove( if not dry_run: self._clear_cache() wtd = mod.working_tree_dir - del mod # Release file-handles (Windows). - gc.collect() + mod.close() rmtree(str(wtd)) # END delete tree if possible # END handle force diff --git a/git/repo/base.py b/git/repo/base.py index a7e7f068a..c4ebe431c 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -7,7 +7,6 @@ __all__ = ["Repo"] -import gc from io import BytesIO import logging import os @@ -529,15 +528,7 @@ def close(self) -> None: """ if self.git: self.git.clear_cache() - # Tempfiles objects on Windows are holding references to open files until - # they are collected by the garbage collector, thus preventing deletion. - # TODO: Find these references and ensure they are closed and deleted - # synchronously rather than forcing a gc collection. - if sys.platform == "win32": - gc.collect() gitdb.util.mman.collect() - if sys.platform == "win32": - gc.collect() def __eq__(self, rhs: object) -> bool: if isinstance(rhs, Repo): diff --git a/test/test_index.py b/test/test_index.py index ab7ae46ea..2b9487cbc 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -4,10 +4,12 @@ # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ import contextlib +import gc import logging import os import os.path as osp import sys +import weakref from itertools import product from pathlib import Path from stat import S_ISLNK, ST_MODE @@ -52,6 +54,25 @@ def test_staged_legacy_filename_and_blob_bytes(): } +def test_adding_a_file_does_not_keep_the_repository_alive(): + # Repo.close() does not force a garbage collection, so staging must not leave a + # reference cycle that delays releasing a dropped repository's Git processes. + with fixture_repo("index.sh", "file", "testfile.txt", "content of testfile", write=True) as owner: + gc_was_enabled = gc.isenabled() + gc.disable() + try: + with mock.patch.object(gc, "collect") as collect: + repo = Repo(owner.working_dir) + repo.index.add(["testfile.txt"]) + dropped = weakref.ref(repo) + del repo + assert dropped() is None + collect.assert_not_called() + finally: + if gc_was_enabled: + gc.enable() + + @pytest.mark.skipif(os.name == "nt", reason="Windows symlink targets are Unicode paths") def test_staging_preserves_non_utf8_symlink_targets(): if not symlinks_supported():