File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -180,7 +180,12 @@ def _step(self, exc=None):
180
180
else :
181
181
result = coro .throw (exc )
182
182
except StopIteration as exc :
183
- self .set_result (exc .value )
183
+ if self ._must_cancel :
184
+ # Task is cancelled right before coro stops.
185
+ self ._must_cancel = False
186
+ self .set_exception (futures .CancelledError ())
187
+ else :
188
+ self .set_result (exc .value )
184
189
except futures .CancelledError :
185
190
super ().cancel () # I.e., Future.cancel(self).
186
191
except Exception as exc :
Original file line number Diff line number Diff line change @@ -1985,6 +1985,16 @@ task_step_impl(TaskObj *task, PyObject *exc)
1985
1985
if (_PyGen_FetchStopIterationValue (& o ) == 0 ) {
1986
1986
/* The error is StopIteration and that means that
1987
1987
the underlying coroutine has resolved */
1988
+ if (task -> task_must_cancel ) {
1989
+ // Task is cancelled right before coro stops.
1990
+ Py_DECREF (o );
1991
+ task -> task_must_cancel = 0 ;
1992
+ et = asyncio_CancelledError ;
1993
+ Py_INCREF (et );
1994
+ ev = NULL ;
1995
+ tb = NULL ;
1996
+ goto set_exception ;
1997
+ }
1988
1998
PyObject * res = future_set_result ((FutureObj * )task , o );
1989
1999
Py_DECREF (o );
1990
2000
if (res == NULL ) {
@@ -2002,6 +2012,8 @@ task_step_impl(TaskObj *task, PyObject *exc)
2002
2012
2003
2013
/* Some other exception; pop it and call Task.set_exception() */
2004
2014
PyErr_Fetch (& et , & ev , & tb );
2015
+
2016
+ set_exception :
2005
2017
assert (et );
2006
2018
if (!ev || !PyObject_TypeCheck (ev , (PyTypeObject * ) et )) {
2007
2019
PyErr_NormalizeException (& et , & ev , & tb );
You can’t perform that action at this time.
0 commit comments