Skip to content

util: close lockfile after opening successfully #1639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
util: close lockfile after opening successfully
Otherwise, this will leak file handles and can be a problem in Windows.
Also, `closefd=true` is the default here, so need to pass it explicitly.

Regression from #1619.

I noticed after [our tests started raising `ResourceWarning`][1].

```python
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.17/x64/lib/python3.8/site-packages/git/util.py", line 938, in _obtain_lock_or_raise
    open(lock_file, mode='w', closefd=True)
ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/pytest-of-runner/pytest-0/popen-gw0/external0/project.git/.git/config.lock' mode='w' encoding='UTF-8'>
```

[1]: https://github.com/iterative/dvc/actions/runs/6055520480/job/16434544764#step:6:869
  • Loading branch information
skshetry committed Sep 2, 2023
commit 3e829eb516a60212bae81a6549361be4748e22d7
3 changes: 2 additions & 1 deletion git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,8 @@ def _obtain_lock_or_raise(self) -> None:
)

try:
open(lock_file, mode='w', closefd=True)
with open(lock_file, mode='w'):
pass
except OSError as e:
raise IOError(str(e)) from e

Expand Down