Skip to content

Commit 9900a90

Browse files
stestaggsobolevn
authored andcommitted
gh-152635: Raise MemoryError when the lock allocation fails in _interpchannels.create() (GH-152642)
Previously, an allocation failure when creating the lock for a channel in `_interpchannels` would trigger an assert. Caused by `handle_channel_error` being passed an error code of -1 which is only allowed if an exception has been set. (in this case, no exception was set) `channelsmod_create` now forwards the error code from `channel_create` which `handle_channel_error` already handled. (cherry picked from commit b383aa6) Co-authored-by: Steve Stagg <stestagg@gmail.com> Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent a4601d0 commit 9900a90

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash caused when running out of memory creating a
2+
:mod:`!_interpchannels` channel. Now a :exc:`MemoryError` is correctly raised.

Modules/_interpchannelsmodule.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ clear_module_state(module_state *state)
336336
#define ERR_CHANNEL_INTERP_CLOSED -4
337337
#define ERR_CHANNEL_EMPTY -5
338338
#define ERR_CHANNEL_NOT_EMPTY -6
339-
#define ERR_CHANNEL_MUTEX_INIT -7
339+
#define ERR_CHANNEL_MUTEX_INIT -7 // currently unused
340340
#define ERR_CHANNELS_MUTEX_INIT -8
341341
#define ERR_NO_NEXT_CHANNEL_ID -9
342342
#define ERR_CHANNEL_CLOSED_WAITING -10
@@ -409,10 +409,6 @@ handle_channel_error(int err, PyObject *mod, int64_t cid)
409409
"if not empty (try force=True)",
410410
cid);
411411
}
412-
else if (err == ERR_CHANNEL_MUTEX_INIT) {
413-
PyErr_SetString(state->ChannelError,
414-
"can't initialize mutex for new channel");
415-
}
416412
else if (err == ERR_CHANNELS_MUTEX_INIT) {
417413
PyErr_SetString(state->ChannelError,
418414
"can't initialize mutex for channel management");
@@ -1726,7 +1722,8 @@ channel_create(_channels *channels, int unboundop)
17261722
{
17271723
PyThread_type_lock mutex = PyThread_allocate_lock();
17281724
if (mutex == NULL) {
1729-
return ERR_CHANNEL_MUTEX_INIT;
1725+
PyErr_NoMemory();
1726+
return -1;
17301727
}
17311728
_channel_state *chan = _channel_new(mutex, unboundop);
17321729
if (chan == NULL) {
@@ -2906,7 +2903,7 @@ channelsmod_create(PyObject *self, PyObject *args, PyObject *kwds)
29062903

29072904
int64_t cid = channel_create(&_globals.channels, unboundop);
29082905
if (cid < 0) {
2909-
(void)handle_channel_error(-1, self, cid);
2906+
(void)handle_channel_error(cid, self, cid);
29102907
return NULL;
29112908
}
29122909
module_state *state = get_module_state(self);

0 commit comments

Comments
 (0)