fix: close in-memory mapped data for a report-only Coverage - #2252
Open
Sanjays2402 wants to merge 2 commits into
Open
fix: close in-memory mapped data for a report-only Coverage#2252Sanjays2402 wants to merge 2 commits into
Sanjays2402 wants to merge 2 commits into
Conversation
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
nedbat
reviewed
Aug 6, 2026
| 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: |
Member
There was a problem hiding this comment.
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.
Author
There was a problem hiding this comment.
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2251
With
[paths]configured,_prepare_data_for_reporting()creates aCoverageData(no_disk=True)and appends it to_data_to_close, but forno_diskdataSqliteDb.close()is a no-op unlessforce=True— and the only caller passingforce=Trueis_atexit, which was registered solely from_init_for_start(). ACoveragethat only reports (thecoverage reportCLI, or pytest-cov's never-startedcombining_cov) therefore never drained the list, leaving the connection open at shutdown:ResourceWarning: unclosed database, which fails a-Werrorjob.This registers the handler from
_prepare_data_for_reporting()too, guarded by a new_atexit_registeredflag so start-and-report can't register it twice.ReportOnlyCleanupTestintests/test_process.pyruns the issue's reproducer and fails without thecontrol.pychange;tests/test_api.pyandtests/test_data.pystay green.This change was prepared with AI assistance; the regression test was run locally and fails without the fix.