fix: ensure resources are cleaned up in testing.Context#2506
Open
tonyandrewmeyer wants to merge 4 commits into
Open
fix: ensure resources are cleaned up in testing.Context#2506tonyandrewmeyer wants to merge 4 commits into
tonyandrewmeyer wants to merge 4 commits into
Conversation
… in scenario Make `ops.testing` test infrastructure clean up its temporary resources deterministically so that downstream test suites can run pytest with `-W error` without spurious ResourceWarnings: - Context: replace `tempfile.TemporaryDirectory` with `mkdtemp` plus a `weakref.finalize` cleanup, and add `close()` / context-manager support for eager cleanup. - Runtime._virtual_charm_root: wrap the yield in try/finally so the virtual charm root (or restored metadata files) is cleaned up even when the charm raises. - _CharmSpec: use `Path.read_text()` instead of leaving `Path.open()` file handles for the GC to close. - test_fs_pull: follow the rename from `_tmp` to `_tmp_path`.
`container.pull()` returns an open file handle; the tests called `.read()` on it without closing, so under `-W error` the GC-time close emitted a `ResourceWarning` (surfaced by pytest as `PytestUnraisableExceptionWarning`). Use `with container.pull(...) as baz:` to close deterministically.
This was referenced May 26, 2026
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.
A few fixes to ensure we do not leak resources (until gc time) in Scenario:
After these changes, the tests can be run with -Werror and not have resource warnings cause failures.