Skip to content

Abandoned StreamWriter isn't reliably detected #114914

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/python3importasyncioimportgcimportsocketasyncdefhandle_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()asyncdefmain():
server=awaitasyncio.start_server(
handle_echo, '127.0.0.1', 8888)
addrs=', '.join(str(sock.getsockname()) forsockinserver.sockets)
print(f'Serving on {addrs}')
client=socket.create_connection(('127.0.0.1', 8888))
client.send(b'a'*64*1024)
asyncwithserver:
foriinrange(25):
awaitasyncio.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