Skip to content
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

Stop resource leakage when reading conf files #2451

Merged
merged 1 commit into from
Nov 4, 2022

Conversation

lunkwill42
Copy link
Member

This should fix the annoying ResourceWarnings coming through during the test suite:

/source/python/nav/config.py:339: ResourceWarning: unclosed file <_io.TextIOWrapper name='/source/.tox/integration-py37-django32/etc/nav.conf' mode='r' encoding='utf-8'>
  NAV_CONFIG = read_flat_config('nav.conf')
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/source/python/nav/django/settings.py:36: ResourceWarning: unclosed file <_io.TextIOWrapper name='/source/.tox/integration-py37-django32/etc/webfront/webfront.conf' mode='r' encoding='utf-8'>
  _webfront_config = getconfig('webfront/webfront.conf')
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/source/python/nav/db/__init__.py:104: ResourceWarning: unclosed file <_io.TextIOWrapper name='/source/.tox/integration-py37-django32/etc/db.conf' mode='r' encoding='utf-8'>
  conf = config.read_flat_config('db.conf')
ResourceWarning: Enable tracemalloc to get the object allocation traceback

This PR just ensures we wrap the config-reading code in a context manager so the config files always end up being closed.

This should fix the annoying ResourceWarnings coming through during the
test suite:

/source/python/nav/config.py:339: ResourceWarning: unclosed file <_io.TextIOWrapper name='/source/.tox/integration-py37-django32/etc/nav.conf' mode='r' encoding='utf-8'>
  NAV_CONFIG = read_flat_config('nav.conf')
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/source/python/nav/django/settings.py:36: ResourceWarning: unclosed file <_io.TextIOWrapper name='/source/.tox/integration-py37-django32/etc/webfront/webfront.conf' mode='r' encoding='utf-8'>
  _webfront_config = getconfig('webfront/webfront.conf')
ResourceWarning: Enable tracemalloc to get the object allocation traceback
/source/python/nav/db/__init__.py:104: ResourceWarning: unclosed file <_io.TextIOWrapper name='/source/.tox/integration-py37-django32/etc/db.conf' mode='r' encoding='utf-8'>
  conf = config.read_flat_config('db.conf')
ResourceWarning: Enable tracemalloc to get the object allocation traceback
@lunkwill42 lunkwill42 self-assigned this Jul 21, 2022
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@codecov
Copy link

codecov bot commented Jul 21, 2022

Codecov Report

Merging #2451 (743de9d) into master (26c69f1) will increase coverage by 0.00%.
The diff coverage is 90.47%.

@@           Coverage Diff           @@
##           master    #2451   +/-   ##
=======================================
  Coverage   52.72%   52.73%           
=======================================
  Files         552      552           
  Lines       40186    40188    +2     
=======================================
+ Hits        21190    21192    +2     
  Misses      18996    18996           
Impacted Files Coverage Δ
python/nav/config.py 64.96% <90.47%> (+0.45%) ⬆️

@github-actions
Copy link

Test results

     12 files       12 suites   12m 3s ⏱️
3 088 tests 2 992 ✔️   96 💤 0
8 739 runs  8 451 ✔️ 288 💤 0

Results for commit 743de9d.

@johannaengland
Copy link
Contributor

For the functional tests this error message still appears:

/source/tests/functional/conftest.py:30: ResourceWarning: unclosed file <_io.BufferedWriter name='reports/gunicorn.log'>
  start_gunicorn()
ResourceWarning: Enable tracemalloc to get the object allocation traceback

@lunkwill42
Copy link
Member Author

For the functional tests this error message still appears:

/source/tests/functional/conftest.py:30: ResourceWarning: unclosed file <_io.BufferedWriter name='reports/gunicorn.log'>
  start_gunicorn()
ResourceWarning: Enable tracemalloc to get the object allocation traceback

Possibly, but this is not a config file leakage, it's part of the test suite setup, and does not affect production code (i.e. separate PR, at best)

Copy link
Contributor

@johannaengland johannaengland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as intended 👍

@lunkwill42 lunkwill42 added this to the 5.5.0 milestone Nov 4, 2022
@lunkwill42 lunkwill42 merged commit 9ce426e into Uninett:master Nov 4, 2022
@lunkwill42 lunkwill42 deleted the bugfix/resourcewarnings branch November 4, 2022 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants