Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Lib/test/test_asyncio/test_eager_task_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ async def run():
self.run_coro(run())
captured_current_task = None

def test_exception_state_not_inherited(self):
# gh-155575: the eager first step must not see the creator's exception
async def bare():
raise

async def explicit():
raise ValueError

async def run():
try:
raise ConnectionError
except ConnectionError:
t1 = self.loop.create_task(bare())
t2 = self.loop.create_task(explicit())
with self.assertRaises(RuntimeError):
await t1
with self.assertRaises(ValueError) as cm:
await t2
self.assertIsNone(cm.exception.__context__)

self.run_coro(run())

def test_all_tasks_with_eager_completion(self):
captured_all_tasks = None

Expand Down Expand Up @@ -297,6 +319,10 @@ def tearDown(self):
asyncio.all_tasks = asyncio.tasks.all_tasks = self._all_tasks
return super().tearDown()

# gh-155575: Python Task implementation cannot clear the exception state
@unittest.expectedFailure
def test_exception_state_not_inherited(self):
super().test_exception_state_not_inherited()


@unittest.skipUnless(hasattr(tasks, '_CTask'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :mod:`asyncio` eager tasks inheriting the creator's exception state.
5 changes: 5 additions & 0 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3477,9 +3477,14 @@ task_eager_start(_PyThreadStateImpl *ts, asyncio_state *state, TaskObj *task)
int retval = 0;

PyObject *stepres;
// gh-155575: this step runs on the creators stack, hide its exception state
_PyErr_StackItem exc_state = { .exc_value = NULL, .previous_item = NULL };
_PyErr_StackItem *prev_exc_info = ts->base.exc_info;
ts->base.exc_info = &exc_state;
Py_BEGIN_CRITICAL_SECTION(task);
stepres = task_step_impl(state, task, NULL);
Py_END_CRITICAL_SECTION();
ts->base.exc_info = prev_exc_info;
if (stepres == NULL) {
PyObject *exc = PyErr_GetRaisedException();
_PyErr_ChainExceptions1(exc);
Expand Down
Loading