Skip to content

Enable immediate garbage collection of connections. - #1752

Merged
aaugustin merged 1 commit into
mainfrom
break-reference-cycle
Aug 25, 2026
Merged

Enable immediate garbage collection of connections.#1752
aaugustin merged 1 commit into
mainfrom
break-reference-cycle

Conversation

@aaugustin

Copy link
Copy Markdown
Member

While the legacy implementation is in maintenance mode at this point,
the change looked simple and safe enough to update it as well.

Fix #1749.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Enables closed connections to be reclaimed immediately without cyclic GC.

Changes:

  • Replaces logger backreferences with weak proxies.
  • Clears cancelled keepalive task references.
  • Adds regression tests and changelog documentation.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/websockets/asyncio/connection.py Breaks logger and keepalive cycles.
src/websockets/sync/connection.py Weakens the logger backreference.
src/websockets/trio/connection.py Weakens the logger backreference.
src/websockets/legacy/protocol.py Breaks logger and keepalive cycles.
tests/asyncio/test_connection.py Adds collection coverage and updates keepalive tests.
tests/sync/test_connection.py Adds collection coverage.
tests/trio/test_connection.py Adds collection coverage.
tests/legacy/test_protocol.py Preserves task references for assertions.
docs/project/changelog.rst Documents immediate collection.
Suppressed comments (1)

tests/asyncio/test_connection.py:1354

  • Calling gc.collect() manually still runs cyclic collection even while automatic GC is disabled, so this test passes with the original reference cycle and doesn't verify immediate reference-counted cleanup. Assert the weak reference immediately after deletion instead.
        gc.collect()
        self.assertIsNone(connection_ref(), "still alive after garbage collection")

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/sync/test_connection.py Outdated
Comment thread tests/trio/test_connection.py Outdated
Comment thread tests/asyncio/test_connection.py
Comment thread src/websockets/legacy/protocol.py
@aaugustin
aaugustin force-pushed the break-reference-cycle branch 3 times, most recently from 24df5d6 to 09acb90 Compare August 25, 2026 06:52
While the legacy implementation is in maintenance mode at this point,
the change looked simple and safe enough to update it as well.

Fix #1749.
@aaugustin
aaugustin force-pushed the break-reference-cycle branch from 09acb90 to 50683fe Compare August 25, 2026 06:52
@aaugustin
aaugustin merged commit 5a5748f into main Aug 25, 2026
8 checks passed
@aaugustin
aaugustin deleted the break-reference-cycle branch August 25, 2026 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reference cycles keep closed connections alive until a full GC pass

2 participants