FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: close in-memory mapped data for a report-only Coverage by Sanjays2402 · Pull Request #2252 · coveragepy/coveragepy · GitHub

fix: close in-memory mapped data for a report-only Coverage - #2252

Open
Sanjays2402 wants to merge 2 commits into
coveragepy:mainfrom
Sanjays2402:fix/report-only-atexit-close
Open

fix: close in-memory mapped data for a report-only Coverage#2252
Sanjays2402 wants to merge 2 commits into
coveragepy:mainfrom
Sanjays2402:fix/report-only-atexit-close

Conversation

Copy link
Copy Markdown

Closes #2251

With [paths] configured, _prepare_data_for_reporting() creates a CoverageData(no_disk=True) and appends it to _data_to_close, but for no_disk data SqliteDb.close() is a no-op unless force=True — and the only caller passing force=True is _atexit, which was registered solely from _init_for_start(). A Coverage that only reports (the coverage report CLI, or pytest-cov's never-started combining_cov) therefore never drained the list, leaving the connection open at shutdown: ResourceWarning: unclosed database, which fails a -Werror job.

This registers the handler from _prepare_data_for_reporting() too, guarded by a new _atexit_registered flag so start-and-report can't register it twice. ReportOnlyCleanupTest in tests/test_process.py runs the issue's reproducer and fails without the control.py change; tests/test_api.py and tests/test_data.py stay green.

This change was prepared with AI assistance; the regression test was run locally and fails without the fix.

With [paths] configured, _prepare_data_for_reporting() creates a
CoverageData(no_disk=True) and appends it to _data_to_close.  For no_disk
data, SqliteDb.close() is a no-op unless force=True, and the only caller
that passes force=True is _atexit.

_atexit was only registered from _init_for_start(), which is reachable
only via Coverage.start().  A Coverage object that only reports -- the
`coverage report` CLI, or pytest-cov's never-started combining_cov --
therefore populated _data_to_close and never drained it, so the shared
cache connection was still open at interpreter shutdown:

    ResourceWarning: unclosed database

which fails a -Werror job.

Register the handler from _prepare_data_for_reporting() as well, guarded
by a new _atexit_registered flag so start() and reporting cannot register
it twice.

Adds ReportOnlyCleanupTest in tests/test_process.py, which runs
`python -Werror -m coverage report` with [paths] set and asserts the
warning is absent.  It fails without the control.py change.

Closes coveragepy#2251
Comment thread coverage/control.py Outdated
mapped_data.update(self._data, map_path=self._make_aliases().map)
self._data = mapped_data
self._data_to_close.append(mapped_data)
if not self._atexit_registered:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

These three lines of code are the same as 655-657. Let's at least make a helper method to do the check-and-register.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Addressed in 5d05722: extracted the once-only check-and-register into _register_atexit() and use it from both _init_for_start() and report-only data preparation. The targeted process tests pass: 7 passed.

This file contains hidden or 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ResourceWarning: unclosed database still reproduces with [paths] config — a Coverage that only reports never registers its atexit cleanup

2 participants


Back | FazBrowse Home | New Git URL