https://github.com/python/cpython/commit/3f50432e31c8e0d2e3ea8cbc2e472f7ee80e327a
commit: 3f50432e31c8e0d2e3ea8cbc2e472f7ee80e327a
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-02-18T14:54:48+02:00
summary:
gh-140652: Fix a crash in _interpchannels.list_all() after closing a channel
(ПР-143743)
files:
A Misc/NEWS.d/next/Library/2026-01-12-19-39-57.gh-issue-140652.HvM9Bl.rst
M Lib/test/test__interpchannels.py
M Lib/test/test_interpreters/test_channels.py
M Modules/_interpchannelsmodule.c
diff --git a/Lib/test/test__interpchannels.py b/Lib/test/test__interpchannels.py
index d7cf77368ef9f2..2b0aba42896c06 100644
--- a/Lib/test/test__interpchannels.py
+++ b/Lib/test/test__interpchannels.py
@@ -382,6 +382,38 @@ def test_sequential_ids(self):
self.assertEqual(id3, int(id2) + 1)
self.assertEqual(set(after) - set(before), {id1, id2, id3})
+ def test_channel_list_all_closed(self):
+ id1 = _channels.create()
+ id2 = _channels.create()
+ id3 = _channels.create()
+ before = _channels.list_all()
+ expected = [info for info in before if info[0] != id2]
+ _channels.close(id2, force=True)
+ after = _channels.list_all()
+ self.assertEqual(set(after), set(expected))
+ self.assertEqual(len(after), len(before) - 1)
+
+ def test_channel_list_all_destroyed(self):
+ id1 = _channels.create()
+ id2 = _channels.create()
+ id3 = _channels.create()
+ before = _channels.list_all()
+ expected = [info for info in before if info[0] != id2]
+ _channels.destroy(id2)
+ after = _channels.list_all()
+ self.assertEqual(set(after), set(expected))
+ self.assertEqual(len(after), len(before) - 1)
+
+ def test_channel_list_all_released(self):
+ id1 = _channels.create()
+ id2 = _channels.create()
+ id3 = _channels.create()
+ before = _channels.list_all()
+ _channels.release(id2, send=True, recv=True)
+ after = _channels.list_all()
+ self.assertEqual(set(after), set(before))
+ self.assertEqual(len(after), len(before))
+
def test_ids_global(self):
id1 = _interpreters.create()
out = _run_output(id1, dedent("""
diff --git a/Lib/test/test_interpreters/test_channels.py
b/Lib/test/test_interpreters/test_channels.py
index 52827357078b85..5437792b5a7014 100644
--- a/Lib/test/test_interpreters/test_channels.py
+++ b/Lib/test/test_interpreters/test_channels.py
@@ -47,6 +47,12 @@ def test_list_all(self):
after = set(channels.list_all())
self.assertEqual(after, created)
+ def test_list_all_closed(self):
+ created = [channels.create() for _ in range(3)]
+ rch, sch = created.pop(1)
+ rch.close()
+ self.assertEqual(set(channels.list_all()), set(created))
+
def test_shareable(self):
interp = interpreters.create()
rch, sch = channels.create()
diff --git
a/Misc/NEWS.d/next/Library/2026-01-12-19-39-57.gh-issue-140652.HvM9Bl.rst
b/Misc/NEWS.d/next/Library/2026-01-12-19-39-57.gh-issue-140652.HvM9Bl.rst
new file mode 100644
index 00000000000000..bed126f02f8714
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-01-12-19-39-57.gh-issue-140652.HvM9Bl.rst
@@ -0,0 +1 @@
+Fix a crash in :func:`!_interpchannels.list_all` after closing a channel.
diff --git a/Modules/_interpchannelsmodule.c b/Modules/_interpchannelsmodule.c
index ef9cf01ecbec5e..2933332ad465d4 100644
--- a/Modules/_interpchannelsmodule.c
+++ b/Modules/_interpchannelsmodule.c
@@ -1644,14 +1644,16 @@ _channels_list_all(_channels *channels, int64_t *count)
if (ids == NULL) {
goto done;
}
- _channelref *ref = channels->head;
- for (int64_t i=0; ref != NULL; ref = ref->next, i++) {
- ids[i] = (struct channel_id_and_info){
- .id = ref->cid,
- .defaults = ref->chan->defaults,
- };
+ int64_t i = 0;
+ for (_channelref *ref = channels->head; ref != NULL; ref = ref->next) {
+ if (ref->chan != NULL) {
+ ids[i++] = (struct channel_id_and_info){
+ .id = ref->cid,
+ .defaults = ref->chan->defaults,
+ };
+ }
}
- *count = channels->numopen;
+ *count = i;
cids = ids;
done:
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]