Skip to content

test_asyncio.test_streams raises a ResourceWarning #116112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Eclips4 opened this issue Feb 29, 2024 · 7 comments
Closed

test_asyncio.test_streams raises a ResourceWarning #116112

Eclips4 opened this issue Feb 29, 2024 · 7 comments
Labels
tests Tests in the Lib/test dir topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@Eclips4
Copy link
Member

Eclips4 commented Feb 29, 2024

Bug report

Bug description:

./python.exe -m test test_asyncio.test_streams

Using random seed: 4151462817
0:00:00 load avg: 28.75 Run 1 test sequentially
0:00:00 load avg: 28.75 [1/1] test_asyncio.test_streams
/Users/admin/Projects/cpython/Lib/asyncio/streams.py:410: ResourceWarning: unclosed <StreamWriter transport=<_SelectorSocketTransport closing fd=12 read=idle write=<idle, bufsize=0>> reader=<StreamReader transport=<_SelectorSocketTransport closing fd=12 read=idle write=<idle, bufsize=0>>>>
  warnings.warn(f"unclosed {self!r}", ResourceWarning)

== Tests result: SUCCESS ==

1 test OK.

Total duration: 1.5 sec
Total tests: run=69
Total test files: run=1/1
Result: SUCCESS

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

@Eclips4 Eclips4 added type-bug An unexpected behavior, bug, or error tests Tests in the Lib/test dir topic-asyncio labels Feb 29, 2024
@github-project-automation github-project-automation bot moved this to Todo in asyncio Feb 29, 2024
@sobolevn
Copy link
Member

Bisects to a355f60 CC @CendioOssman

@gvanrossum
Copy link
Member

To be precise the error occurs in test_unhandled_cancel:

(.venv) ~/cpython$ ./python.exe -m test test_asyncio.test_streams -v -m test_unhandled_cancel
[...]
Using random seed: 781240745
0:00:00 load avg: 1.56 Run 1 test sequentially
0:00:00 load avg: 1.56 [1/1] test_asyncio.test_streams
test_unhandled_cancel (test.test_asyncio.test_streams.StreamTests.test_unhandled_cancel) ... /Users/guido/cpython/Lib/asyncio/streams.py:410: ResourceWarning: unclosed <StreamWriter transport=<_SelectorSocketTransport closing fd=11 read=idle write=<idle, bufsize=0>> reader=<StreamReader transport=<_SelectorSocketTransport closing fd=11 read=idle write=<idle, bufsize=0>>>>
  warnings.warn(f"unclosed {self!r}", ResourceWarning)
ok

@CendioOssman
Copy link
Contributor

Thanks for letting me know. I'll try to have a look as soon as I'm able.

@CendioOssman
Copy link
Contributor

In this case, the ResourceWarning is absolutely correct. The test does not correctly clean up the streams.

I am not sure why the same thing doesn't happen for test_unhandled_exception(). Maybe the StreamWriter gets tangled up in the exception stack and is kept alive?

The fix is this:

diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py
index bf123ebf9b..2cf48538d5 100644
--- a/Lib/test/test_asyncio/test_streams.py
+++ b/Lib/test/test_asyncio/test_streams.py
@@ -1188,6 +1188,7 @@ async def handle_echo(reader, writer):
 
     def test_unhandled_cancel(self):
         async def handle_echo(reader, writer):
+            writer.close()
             asyncio.current_task().cancel()
         messages = self._basetest_unhandled_exceptions(handle_echo)
         self.assertEqual(messages, [])

Can someone easily commit this without a PR song and dance?

@sobolevn
Copy link
Member

sobolevn commented Mar 5, 2024

Can someone easily commit this without a PR song and dance?

No, this is not possible. All changes require a PR, CI run and proper review.
If you don't have time, I can help you (with crediting your work properly) :)

@CendioOssman
Copy link
Contributor

That would be appreciated.

@sobolevn
Copy link
Member

sobolevn commented Mar 5, 2024

Should be fixed now! Thanks everyone!

@sobolevn sobolevn closed this as completed Mar 5, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in asyncio Mar 5, 2024
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

4 participants