-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TarOutput: Close collected files after reading them to free buffers
With tar output, TarOutput.addRealFile() did not close file objects. So in tar mode, the file objects of all captures regular files were not closed. As the fixed line use `TarFile.addfile(tarinfo, open("fn"))` the opened file object could have never been passed anywhere else so it is safe to replace this pattern using a context manager. Summary: - The changes to xen-bugtool are 4 safe lines to use `with open` - All other changes are improvements for the unit tests to cover it. Description: This was found by PYTHONDEVMODE=yes to trace unclosed resources. I set this up in .pre-commit-config.yaml, so while developing tests, I saw the warning while testing with pre-commit run -v. Fixes in this PR: - Use context managers to fix the locations which are easy to fix. - Extend the unit tests (those in master now) to cover both locations. - While updating those tests, improve them to check the file list of the ZIP and tar archives generated in this particular test case. - Configure GitHub CI to create source annotations for PYTHONWARNINGS which are issued by such cases and are shown in code review of PRs. Notes: In the course of a year, my development machine accumulated 24000 files in /var/xapi/blobs (spread evenly over all subdirectories), so Python apparently does some garbage collection, but it's certainly not good to never close so many open file-like objects in Python. Closing them in time has the potential to reduce the memory usage of the xen-bugtool / xenserver-status-report process. There are more locations where Python 3.10 reports unclosed file objects, but it seems we may have to use enable tracemalloc as recommended by the issued PYTHONWARNINGS to find their source. Signed-off-by: Bernhard Kaindl <[email protected]>
- Loading branch information
1 parent
1aae840
commit 5fc5d42
Showing
8 changed files
with
80 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"problemMatcher": [ | ||
{ | ||
"owner": "python-libs-PYTHONWARNINGS", | ||
"severity": "warning", | ||
"pattern": [ | ||
{ | ||
"regexp": "^.*/python-libs/(.+):([0-9]*):(.*):(.*)$", | ||
"file": 1, | ||
"line": 2, | ||
"code": 3, | ||
"message": 4 | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,9 +89,17 @@ jobs: | |
path: ~/.cache/pre-commit | ||
key: pre-commit|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }} | ||
|
||
# https://docs.python.org/3/library/devmode.html#resourcewarning-example | ||
# If pytest runs with PYTHONDEVMODE=yes, it enables resource checks like | ||
# unclosed file warnings. Configure GitHub to show all such warnings as | ||
# annotations at the source location they occur in the PR code review: | ||
|
||
- run: echo "::add-matcher::.github/workflows/PYTHONWARNINGS-problemMatcher.json" | ||
|
||
- uses: pre-commit/[email protected] | ||
name: Run pre-commit checks | ||
env: | ||
PYTHONDEVMODE: yes # Enable Python3 checks. See the comment above. | ||
# Skip the no-commit-to-branch check inside of GitHub CI (for CI on merge to master) | ||
SKIP: no-commit-to-branch | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root:x:0: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root:x:0:0:root:/root:/bin/bash |
2 changes: 2 additions & 0 deletions
2
tests/integration/dom0-template/etc/xensource/bugtool/mock/stuff.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5fc5d42
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/marketplace/actions/pytest-coverage-comment