https://github.com/python/cpython/commit/171f7e6cc0cc3452917c8c7d2b304d2ad1b4332b
commit: 171f7e6cc0cc3452917c8c7d2b304d2ad1b4332b
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: picnixz <[email protected]>
date: 2026-10-21T01:28:16Z
summary:

[3.14] gh-143376: validate usages of `_interpchannels.ChannelID` in 
`_channelid_shared` (GH-146574) (#159996)

gh-143376: validate usages of `_interpchannels.ChannelID` in 
`_channelid_shared` (GH-146574)
(cherry picked from commit c6a084ac6022d834fd0ad3eb0129a111a28638fe)

Co-authored-by: Bénédikt Tran <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-03-28-14-34-18.gh-issue-143376.FvWTwi.rst
M Lib/test/test_interpreters/test_channels.py
M Modules/_interpchannelsmodule.c

diff --git a/Lib/test/test_interpreters/test_channels.py 
b/Lib/test/test_interpreters/test_channels.py
index 1adba40b740895f..cd53a4f61bedbda 100644
--- a/Lib/test/test_interpreters/test_channels.py
+++ b/Lib/test/test_interpreters/test_channels.py
@@ -8,7 +8,7 @@
 # Raise SkipTest if subinterpreters not supported.
 _channels = import_helper.import_module('_interpchannels')
 from concurrent import interpreters
-from test.support import channels
+from test.support import channels, force_not_colorized
 from .utils import _run_output, TestBase
 
 
@@ -34,6 +34,39 @@ def test_highlevel_reloaded(self):
             importlib.reload(channels)
             """));
 
+    @force_not_colorized
+    def test_channel_id_types(self):
+        # See https://github.com/python/cpython/issues/143376.
+        code = """\
+        import _interpchannels
+        import re
+        from concurrent import interpreters
+
+        class End:
+            __slots__ = ()
+            def __new__(cls, chan=None):
+                return object.__new__(cls)
+            @property
+            def _id(self):
+                return object()
+
+        class Send(End): pass
+        class Recv(End): pass
+
+        _interpchannels._register_end_types(Send, Recv)
+        chan = _interpchannels._channel_id(_interpchannels.create(), 
_resolve=True)
+        interp = interpreters.create()
+        try:
+            interp.prepare_main(chan=Send(chan))
+        finally:
+            interp.close()
+        """
+
+        out, err = self.assert_python_failure("-c", code)
+        self.assertEqual(out, '')
+        errmsg = "TypeError: '_id' attribute must be an 
_interpchannels.ChannelID object, not object"
+        self.assertIn(errmsg, err)
+
 
 class TestChannels(TestBase):
 
diff --git 
a/Misc/NEWS.d/next/Library/2026-03-28-14-34-18.gh-issue-143376.FvWTwi.rst 
b/Misc/NEWS.d/next/Library/2026-03-28-14-34-18.gh-issue-143376.FvWTwi.rst
new file mode 100644
index 000000000000000..981e0ede58fde87
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-03-28-14-34-18.gh-issue-143376.FvWTwi.rst
@@ -0,0 +1,2 @@
+:mod:`concurrent.interpreters`: fix a crash when internal cross-interpreter
+channel ends have incorrect channel IDs. Patch by Bénédikt Tran.
diff --git a/Modules/_interpchannelsmodule.c b/Modules/_interpchannelsmodule.c
index c38495058692768..351509d57de16e9 100644
--- a/Modules/_interpchannelsmodule.c
+++ b/Modules/_interpchannelsmodule.c
@@ -2634,6 +2634,17 @@ _channelid_from_xid(_PyXIData_t *data)
 static int
 _channelid_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data)
 {
+    module_state *st = _get_current_module_state();
+    if (st == NULL) {
+        return -1;
+    }
+    if (!PyObject_IsInstance(obj, (PyObject *)st->ChannelIDType)) {
+        PyErr_Format(PyExc_TypeError,
+                     "'_id' attribute must be an %N object, not %T",
+                     st->ChannelIDType, obj);
+        return -1;
+    }
+
     if (_PyXIData_InitWithSize(
             data, tstate->interp, sizeof(struct _channelid_xid), obj,
             _channelid_from_xid

_______________________________________________
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]

Reply via email to