FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-150213: Fix OOM error handling in _interpchannels by lpyu001 · Pull Request #150214 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
63 changes: 63 additions & 0 deletions Lib/test/test__interpchannels.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1838,5 +1838,68 @@ def test_force_close(self):
fix.clean_up()


class InterpChannelsOOMTest(TestBase):
def test_create_no_memory(self):
_testcapi = import_helper.import_module('_testcapi')

raised_memoryerror = False
for start in range(1, 20):
with self.subTest(start=start):
cid = None
_testcapi.set_nomemory(start, start + 1)
try:
try:
cid = _channels.create(REPLACE)
except MemoryError:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

you can use with self.assertRaises(MemoryError):

raised_memoryerror = True
except SystemError as exc:
self.fail(
f"missing MemoryError at start={start}: {exc}")
finally:
_testcapi.remove_mem_hooks()
if cid is not None:
_channels.destroy(cid)

self.assertTrue(
raised_memoryerror,
"_testcapi.set_nomemory did not trigger a MemoryError from "
"_channels.create() within start=1..20; widen range")

def test_close_nonempty_no_memory(self):
_testcapi = import_helper.import_module('_testcapi')

raised_memoryerror = False
for start in range(1, 30):
with self.subTest(start=start):
cid = _channels.create(REPLACE)
try:
_channels.send(cid, b'spam', blocking=False)

_testcapi.set_nomemory(start, start + 1)
try:
_channels.close(cid, send=True)
except MemoryError:
raised_memoryerror = True
except SystemError as exc:
self.fail(
f"missing MemoryError at start={start}: {exc}")
finally:
_testcapi.remove_mem_hooks()
finally:
try:
_channels.close(cid, force=True)
except Exception:
pass
try:
_channels.destroy(cid)
except Exception:
pass

self.assertTrue(
raised_memoryerror,
"_testcapi.set_nomemory did not trigger a MemoryError from "
"_channel_set_closing within start=1..30; widen range")


if __name__ == '__main__':
unittest.main()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix missing :exc:`MemoryError` in several out-of-memory error paths in
:mod:`!_interpchannels`. Previously these paths could raise
:exc:`SystemError` or trigger an assertion failure in debug builds.
8 changes: 6 additions & 2 deletions Modules/_interpchannelsmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,8 @@ static _channelends *
_channelends_new(void)
{
_channelends *ends = GLOBAL_MALLOC(_channelends);
if (ends== NULL) {
if (ends == NULL) {
PyErr_NoMemory();
return NULL;
}
ends->numsendopen = 0;
Expand Down Expand Up @@ -1115,6 +1116,7 @@ _channel_new(PyThread_type_lock mutex, struct _channeldefaults defaults)
assert(check_unbound(defaults.unboundop));
_channel_state *chan = GLOBAL_MALLOC(_channel_state);
if (chan == NULL) {
PyErr_NoMemory();
return NULL;
}
chan->mutex = mutex;
Expand Down Expand Up @@ -1313,6 +1315,7 @@ _channelref_new(int64_t cid, _channel_state *chan)
{
_channelref *ref = GLOBAL_MALLOC(_channelref);
if (ref == NULL) {
PyErr_NoMemory();
return NULL;
}
ref->cid = cid;
Expand Down Expand Up @@ -1698,6 +1701,7 @@ _channel_set_closing(_channelref *ref, PyThread_type_lock mutex) {
}
chan->closing = GLOBAL_MALLOC(struct _channel_closing);
if (chan->closing == NULL) {
PyErr_NoMemory();
goto done;
}
chan->closing->ref = ref;
Expand Down Expand Up @@ -2948,7 +2952,7 @@ channelsmod_create(PyObject *self, PyObject *args, PyObject *kwds)
if (handle_channel_error(err, self, cid)) {
assert(cidobj == NULL);
err = channel_destroy(&_globals.channels, cid);
if (handle_channel_error(err, self, cid)) {
if (err != 0 && handle_channel_error(err, self, cid)) {
// XXX issue a warning?
}
return NULL;
Expand Down
Loading

Back | FazBrowse Home | New Git URL