Skip to content

Abandoned StreamWriter isn't reliably detected #114914

Closed
@CendioOssman

Description

@CendioOssman

Bug report

Bug description:

A StreamWriter should be close():d when you are done with it. There is code in the destructor for StreamWriter to detect when this is overlooked and trigger a ResourceWarning. However, the current code often maintains a strong reference to the writer, preventing it from being garbage collected and hence no warning.

Test case:

#!/usr/bin/python3 import asyncio import gc import socket async def handle_echo(reader, writer): addr = writer.get_extra_info('peername') print(f"Connection from {addr!r}") # Forgetting to close the writer #writer.close() #await writer.wait_closed() async def main(): server = await asyncio.start_server( handle_echo, '127.0.0.1', 8888) addrs = ', '.join(str(sock.getsockname()) for sock in server.sockets) print(f'Serving on {addrs}') client = socket.create_connection(('127.0.0.1', 8888)) client.send(b'a' * 64 * 1024) async with server: for i in range(25): await asyncio.sleep(0.1) gc.collect() print('Exiting') print('Done serving') client.close() asyncio.run(main())

Test case locks up waiting for the client connection, when instead I would expect a ResourceWarning and everything exiting nicely.

CPython versions tested on:

3.12, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions