Skip to content

Commit 32b17a5

Browse files
author
Kristján Valur Jónsson
committed
keep current task state in sync between python tasks and c tasks
1 parent 525dcfe commit 32b17a5

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Lib/asyncio/tasks.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,15 @@ def __step(self, exc=None):
273273
self._must_cancel = False
274274
self._fut_waiter = None
275275

276-
_py_enter_task(self._loop, self)
276+
prev_task = _py_swap_current_task(self._loop, self)
277277
try:
278278
self.__step_run_and_handle_result(exc)
279279
finally:
280-
_py_leave_task(self._loop, self)
281-
self = None # Needed to break cycles when an exception occurs.
280+
try:
281+
curtask = _py_swap_current_task(self._loop, prev_task)
282+
assert curtask is self
283+
finally:
284+
self = None # Needed to break cycles when an exception occurs.
282285

283286
def __step_run_and_handle_result(self, exc):
284287
coro = self._coro
@@ -1099,6 +1102,9 @@ def _swap_current_task(loop, task):
10991102
del _current_tasks[loop]
11001103
else:
11011104
_current_tasks[loop] = task
1105+
if _c_swap_current_task is not _py_swap_current_task:
1106+
# keep the C task state in sync
1107+
_c_swap_current_task(loop, task)
11021108
return prev_task
11031109

11041110

0 commit comments

Comments
 (0)