Repository navigation
Commit d7fd408
perf(repo): stop forcing garbage collections in
`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>Repo.close()
1 parent 2d2570b commit d7fd408
3 files changed
Lines changed: 8 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
228 | 235 | | |
229 | 236 | | |
230 | 237 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| |||
1553 | 1552 | | |
1554 | 1553 | | |
1555 | 1554 | | |
1556 | | - | |
1557 | | - | |
| 1555 | + | |
1558 | 1556 | | |
1559 | 1557 | | |
1560 | 1558 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
| |||
529 | 528 | | |
530 | 529 | | |
531 | 530 | | |
532 | | - | |
533 | | - | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | 531 | | |
539 | | - | |
540 | | - | |
541 | 532 | | |
542 | 533 | | |
543 | 534 | | |
| |||
0 commit comments