Skip to content

Commit 9baa30c

Browse files
committed
gh-161841: Fix a crash when comparing ChannelID under wrong sys.modules
1 parent 2d862b1 commit 9baa30c

3 files changed

Lines changed: 41 additions & 59 deletions

File tree

‎Lib/test/test__interpchannels.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import threading
66
import time
77
import unittest
8+
import types
89

910
from test.support import import_helper
1011

@@ -352,6 +353,17 @@ def test_equality(self):
352353
self.assertFalse(cid1 != cid2)
353354
self.assertTrue(cid1 != cid3)
354355

356+
def test_richcompare_empty_module_state(self):
357+
# gh-161841
358+
with import_helper.CleanImport("_interpchannels"):
359+
import _interpchannels as ch
360+
sys.modules["_interpchannels"] = types.ModuleType("_interpchannels")
361+
362+
cid = ch.create()
363+
self.assertTrue(cid == cid)
364+
self.assertTrue(cid == int(cid))
365+
self.assertFalse(cid == int(cid) + 1)
366+
355367
def test_shareable(self):
356368
chan = _channels.create(REPLACE)
357369

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes a possible crash when comparing channel ids from
2+
:mod:`concurrent.interpreters` under an unusual ``sys.modules`` setup.

‎Modules/_interpchannelsmodule.c‎

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,6 @@ _get_current_module(void)
161161
return mod;
162162
}
163163

164-
static PyObject *
165-
get_module_from_owned_type(PyTypeObject *cls)
166-
{
167-
assert(cls != NULL);
168-
return _get_current_module();
169-
// XXX Use the more efficient API now that we use heap types:
170-
//return PyType_GetModule(cls);
171-
}
172-
173-
static struct PyModuleDef moduledef;
174-
175-
static PyObject *
176-
get_module_from_type(PyTypeObject *cls)
177-
{
178-
assert(cls != NULL);
179-
return _get_current_module();
180-
// XXX Use the more efficient API now that we use heap types:
181-
//return PyType_GetModuleByDef(cls, &moduledef);
182-
}
183-
184164
static PyObject *
185165
add_new_exception(PyObject *mod, const char *name, PyObject *base)
186166
{
@@ -265,6 +245,23 @@ get_module_state(PyObject *mod)
265245
return state;
266246
}
267247

248+
static struct PyModuleDef moduledef;
249+
250+
static inline PyObject *
251+
get_module_from_type(PyTypeObject *tp)
252+
{
253+
assert(tp != NULL);
254+
PyObject *mod = PyType_GetModuleByDef(tp, &moduledef);
255+
assert(mod != NULL);
256+
return mod;
257+
}
258+
259+
static inline module_state *
260+
get_module_state_from_type(PyTypeObject *tp)
261+
{
262+
return get_module_state(get_module_from_type(tp));
263+
}
264+
268265
static module_state *
269266
_get_current_module_state(void)
270267
{
@@ -363,9 +360,7 @@ static int
363360
exceptions_init(PyObject *mod)
364361
{
365362
module_state *state = get_module_state(mod);
366-
if (state == NULL) {
367-
return -1;
368-
}
363+
assert(state != NULL);
369364

370365
#define ADD(NAME, BASE) \
371366
do { \
@@ -2245,9 +2240,7 @@ static PyObject *
22452240
new_channel_info(PyObject *mod, struct channel_info *info)
22462241
{
22472242
module_state *state = get_module_state(mod);
2248-
if (state == NULL) {
2249-
return NULL;
2250-
}
2243+
assert(state != NULL);
22512244

22522245
assert(state->ChannelInfoType != NULL);
22532246
PyObject *self = PyStructSequence_New(state->ChannelInfoType);
@@ -2505,14 +2498,8 @@ channelid_richcompare(PyObject *self, PyObject *other, int op)
25052498
Py_RETURN_NOTIMPLEMENTED;
25062499
}
25072500

2508-
PyObject *mod = get_module_from_type(Py_TYPE(self));
2509-
if (mod == NULL) {
2510-
return NULL;
2511-
}
2512-
module_state *state = get_module_state(mod);
2513-
if (state == NULL) {
2514-
goto done;
2515-
}
2501+
module_state *state = get_module_state_from_type(Py_TYPE(self));
2502+
assert(state != NULL);
25162503

25172504
if (!PyObject_TypeCheck(self, state->ChannelIDType)) {
25182505
res = Py_NewRef(Py_NotImplemented);
@@ -2556,7 +2543,6 @@ channelid_richcompare(PyObject *self, PyObject *other, int op)
25562543
}
25572544

25582545
done:
2559-
Py_DECREF(mod);
25602546
return res;
25612547
}
25622548

@@ -2596,9 +2582,7 @@ _channelid_from_xid(_PyXIData_t *data)
25962582
}
25972583
assert(mod != Py_None);
25982584
module_state *state = get_module_state(mod);
2599-
if (state == NULL) {
2600-
goto done;
2601-
}
2585+
assert(state != NULL);
26022586

26032587
// Note that we do not preserve the "resolve" flag.
26042588
int err = newchannelid(state->ChannelIDType, xid->cid, xid->end,
@@ -2823,9 +2807,7 @@ static int
28232807
set_channelend_types(PyObject *mod, PyTypeObject *send, PyTypeObject *recv)
28242808
{
28252809
module_state *state = get_module_state(mod);
2826-
if (state == NULL) {
2827-
return -1;
2828-
}
2810+
assert(state != NULL);
28292811

28302812
// Clear the old values if the .py module was reloaded.
28312813
if (state->send_channel_type != NULL) {
@@ -2951,9 +2933,7 @@ channelsmod_create(PyObject *self, PyObject *args, PyObject *kwds)
29512933
return NULL;
29522934
}
29532935
module_state *state = get_module_state(self);
2954-
if (state == NULL) {
2955-
return NULL;
2956-
}
2936+
assert(state != NULL);
29572937
channelid *cidobj = NULL;
29582938
int err = newchannelid(state->ChannelIDType, cid, 0,
29592939
&_globals.channels, 0, 0,
@@ -3018,11 +2998,7 @@ channelsmod_list_all(PyObject *self, PyObject *Py_UNUSED(ignored))
30182998
goto finally;
30192999
}
30203000
module_state *state = get_module_state(self);
3021-
if (state == NULL) {
3022-
Py_DECREF(ids);
3023-
ids = NULL;
3024-
goto finally;
3025-
}
3001+
assert(state != NULL);
30263002
struct channel_id_and_info *cur = cids;
30273003
for (int64_t i=0; i < count; cur++, i++) {
30283004
PyObject *cidobj = NULL;
@@ -3485,16 +3461,8 @@ static PyObject *
34853461
channelsmod__channel_id(PyObject *self, PyObject *args, PyObject *kwds)
34863462
{
34873463
module_state *state = get_module_state(self);
3488-
if (state == NULL) {
3489-
return NULL;
3490-
}
3491-
PyTypeObject *cls = state->ChannelIDType;
3492-
3493-
PyObject *mod = get_module_from_owned_type(cls);
3494-
assert(mod == self);
3495-
Py_DECREF(mod);
3496-
3497-
return _channelid_new(self, cls, args, kwds);
3464+
assert(state != NULL);
3465+
return _channelid_new(self, state->ChannelIDType, args, kwds);
34983466
}
34993467

35003468
static PyObject *

0 commit comments

Comments
 (0)