nav icon indicating copy to clipboard operation
nav copied to clipboard

Stop resource leakage when reading conf files

Open lunkwill42 opened this issue 2 years ago • 4 comments

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.

lunkwill42 avatar Jul 21 '22 08:07 lunkwill42

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

sonarqubecloud[bot] avatar Jul 21 '22 08:07 sonarqubecloud[bot]

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%) :arrow_up:

codecov[bot] avatar Jul 21 '22 09:07 codecov[bot]

Test results

     12 files       12 suites   12m 3s :stopwatch: 3 088 tests 2 992 :heavy_check_mark:   96 :zzz: 0 :x: 8 739 runs  8 451 :heavy_check_mark: 288 :zzz: 0 :x:

Results for commit 743de9d0.

github-actions[bot] avatar Jul 21 '22 09:07 github-actions[bot]

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

johannaengland avatar Jul 29 '22 07:07 johannaengland

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)

lunkwill42 avatar Nov 03 '22 13:11 lunkwill42