Skip to content

test_asyncio.test_streams raises a ResourceWarning #116112

Description

@Eclips4

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

Activity

  1. sobolevn commented on Feb 29, 2024

    @sobolevn
    Member

    Bisects to a355f60 CC @CendioOssman

  2. gvanrossum commented on Feb 29, 2024

    @gvanrossum
    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
    
  3. CendioOssman commented on Feb 29, 2024

    @CendioOssman
    Contributor

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

  4. CendioOssman commented on Mar 5, 2024

    @CendioOssman
    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?

  5. sobolevn commented on Mar 5, 2024

    @sobolevn
    Member

    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) :)

  6. CendioOssman commented on Mar 5, 2024

    @CendioOssman
    Contributor

    That would be appreciated.

  7. added 2 commits that reference this issue on Mar 5, 2024
  8. sobolevn commented on Mar 5, 2024

    @sobolevn
    Member

    Should be fixed now! Thanks everyone!

  9. moved this from Todo to Done in asyncioon Mar 5, 2024
  10. added a commit that references this issue on Mar 25, 2024
  11. added a commit that references this issue on Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions