Repository: python/cpython
Pull request: gh-100348: asyncio._SelectorSocketTransport: free cyclic reference on close()
Closes #100348

<!--
Thanks for your contribution!
Please read this comment in its entirety. It's quite important.

# Pull Request title

It should be in the following format:

```
gh-NNNNN: Summary of the changes made
```

Where: gh-NNNNN refers to the GitHub issue number.

Most PRs will require an issue number. Trivial changes, like fixing a typo, do not need an issue.

# Backport Pull Request title

If this is a backport PR (PR made against branches other than `main`),
please ensure that the PR title is in the following format:

```
[X.Y] <title from the original PR> (GH-NNNN)
```

Where: [X.Y] is the branch name, e.g. [3.6].

GH-NNNN refers to the PR number from `main`.

-->


<!-- gh-issue-number: gh-100348 -->
* Issue: gh-100348
<!-- /gh-issue-number -->


Problem statement:
asyncio._SelectorSocketTransport: help garbage collector
# Feature or enhancement

Debugging an asyncio application I've found that many _SelectorSocketTransport objects end up listed in gc.garbage instead of being released as soon as possible.

# Pitch

We could release resources as soon as possible, and avoid this task to be done by gc.


<!-- gh-linked-prs -->
### Linked PRs
* gh-100349
<!-- /gh-linked-prs -->

Hints from issue discussion:

Commit message: 📜🤖 Added by blurb_it.

Patch to review:
diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
index 3d30006198f671..74f289f0e6f811 100644
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -1133,6 +1133,10 @@ def _make_empty_waiter(self):
     def _reset_empty_waiter(self):
         self._empty_waiter = None
 
+    def close(self):
+        self._read_ready_cb = None
+        super().close()
+
 
 class _SelectorDatagramTransport(_SelectorTransport, transports.DatagramTransport):
 
diff --git a/Misc/NEWS.d/next/Library/2022-12-19-19-30-06.gh-issue-100348.o7IAHh.rst b/Misc/NEWS.d/next/Library/2022-12-19-19-30-06.gh-issue-100348.o7IAHh.rst
new file mode 100644
index 00000000000000..38e73903ad9412
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-12-19-19-30-06.gh-issue-100348.o7IAHh.rst
@@ -0,0 +1 @@
+Release _SelectorSocketTransport._read_ready_cb on close() to free up cyclic reference.


Code review comments:
diff_hunk: @@ -0,0 +1 @@
+Release _SelectorSocketTransport._read_ready_cb on close() to free up cyclic reference.
path: Misc/NEWS.d/next/Library/2022-12-19-19-30-06.gh-issue-100348.o7IAHh.rst
text: @user1:
```suggestion
Fix ref cycle in :class:`!asyncio._SelectorSocketTransport` by removing ``_read_ready_cb`` in ``close``.

```