fix: warn when async client cleanup is skipped - #363
Open
abhinavkr26104 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
1 issue found across 3 files
Confidence score: 5/5
- In
tests/test_async_client_cleanup.py(test_unclosed_async_http_client_warns_without_running_loop), placingasyncio.run(client.aclose())insidepytest.warns(...)means a failed warning assertion can skip cleanup, leaving an unclosed client that may cause noisy or flaky downstream tests — move cleanup to afinallyblock (or fixture finalizer) so it always runs.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="tests/test_async_client_cleanup.py">
<violation number="1" location="tests/test_async_client_cleanup.py:17">
P3: In `test_unclosed_async_http_client_warns_without_running_loop`, the `asyncio.run(client.aclose())` cleanup runs only if the `pytest.warns(...)` block passes. If the expect-warning assertion fails (e.g. `__del__` stops warning), the block raises and the client is never closed, leaking an unclosed HTTP client into subsequent tests. Wrap the cleanup in a `try/finally` so the client is always closed deterministically.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| with pytest.warns(ResourceWarning, match="Unclosed async HTTP client"): | ||
| client.__del__() | ||
|
|
||
| asyncio.run(client.aclose()) |
There was a problem hiding this comment.
P3: In test_unclosed_async_http_client_warns_without_running_loop, the asyncio.run(client.aclose()) cleanup runs only if the pytest.warns(...) block passes. If the expect-warning assertion fails (e.g. __del__ stops warning), the block raises and the client is never closed, leaking an unclosed HTTP client into subsequent tests. Wrap the cleanup in a try/finally so the client is always closed deterministically.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/test_async_client_cleanup.py, line 17:
<comment>In `test_unclosed_async_http_client_warns_without_running_loop`, the `asyncio.run(client.aclose())` cleanup runs only if the `pytest.warns(...)` block passes. If the expect-warning assertion fails (e.g. `__del__` stops warning), the block raises and the client is never closed, leaking an unclosed HTTP client into subsequent tests. Wrap the cleanup in a `try/finally` so the client is always closed deterministically.</comment>
<file context>
@@ -0,0 +1,28 @@
+ with pytest.warns(ResourceWarning, match="Unclosed async HTTP client"):
+ client.__del__()
+
+ asyncio.run(client.aclose())
+
+
</file context>
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.
Summary
ResourceWarningwhen an unclosed async HTTP client is collected without a running event loopasync with/await client.close()resource managementTesting
uv run --frozen pytest tests/test_async_client_cleanup.py -q(2 passed)uv run --frozen ruff check .uv run --frozen pyright -p .uv run --frozen mypy --platform linux .Fixes #356
Summary by cubic
Warns when async async-client cleanup is skipped to prevent silent connection leaks. Previously
AsyncHttpxClientWrapper.__del__silently did nothing if no event loop was running; now it emits aResourceWarningin that case and still schedulesaclose()when a loop exists. README examples now useasync withto show explicit lifecycle management, with tests covering both paths.async with AsyncStagehand(...)orawait client.close()to shut down async clients deterministically and avoidResourceWarning.Written for commit 87a7b03. Summary will update on new commits.