2020-03-12 01:46:20 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/smallint.h"
|
|
|
|
#include "py/pairheap.h"
|
|
|
|
#include "py/mphal.h"
|
2022-02-16 09:00:45 -05:00
|
|
|
|
2022-02-22 03:49:23 -05:00
|
|
|
#ifndef __unix__
|
|
|
|
#include "shared-bindings/supervisor/__init__.h"
|
|
|
|
#endif
|
2020-03-12 01:46:20 -04:00
|
|
|
|
|
|
|
#if MICROPY_PY_UASYNCIO
|
|
|
|
|
2021-06-08 08:45:56 -04:00
|
|
|
// Used when task cannot be guaranteed to be non-NULL.
|
|
|
|
#define TASK_PAIRHEAP(task) ((task) ? &(task)->pairheap : NULL)
|
|
|
|
|
extmod/uasyncio: Fix race with cancelled task waiting on finished task.
This commit fixes a problem with a race between cancellation of task A and
completion of task B, when A waits on B. If task B completes just before
task A is cancelled then the cancellation of A does not work. Instead,
the CancelledError meant to cancel A gets passed through to B (that's
expected behaviour) but B handles it as a "Task exception wasn't retrieved"
scenario, printing out such a message (this is because finished tasks point
their "coro" attribute to themselves to indicate they are done, and
implement the throw() method, but that method inadvertently catches the
CancelledError). The correct behaviour is for B to bounce that
CancelledError back out.
This bug is mainly seen when wait_for() is used, and in that context the
symptoms are:
- occurs when using wait_for(T, S), if the task T being waited on finishes
at exactly the same time as the wait-for timeout S expires
- task T will have run to completion
- the "Task exception wasn't retrieved message" is printed with
"<class 'CancelledError'>" as the error (ie no traceback)
- the wait_for(T, S) call never returns (it's never put back on the
uasyncio run queue) and all tasks waiting on this are blocked forever
from running
- uasyncio otherwise continues to function and other tasks continue to be
scheduled as normal
The fix here reworks the "waiting" attribute of Task to be called "state"
and uses it to indicate whether a task is: running and not awaited on,
running and awaited on, finished and not awaited on, or finished and
awaited on. This means the task does not need to point "coro" to itself to
indicate finished, and also allows removal of the throw() method.
A benefit of this is that "Task exception wasn't retrieved" messages can go
back to being able to print the name of the coroutine function.
Fixes issue #7386.
Signed-off-by: Damien George <damien@micropython.org>
2021-06-14 08:32:51 -04:00
|
|
|
#define TASK_STATE_RUNNING_NOT_WAITED_ON (mp_const_true)
|
|
|
|
#define TASK_STATE_DONE_NOT_WAITED_ON (mp_const_none)
|
|
|
|
#define TASK_STATE_DONE_WAS_WAITED_ON (mp_const_false)
|
|
|
|
|
|
|
|
#define TASK_IS_DONE(task) ( \
|
|
|
|
(task)->state == TASK_STATE_DONE_NOT_WAITED_ON \
|
|
|
|
|| (task)->state == TASK_STATE_DONE_WAS_WAITED_ON)
|
|
|
|
|
2020-03-12 01:46:20 -04:00
|
|
|
typedef struct _mp_obj_task_t {
|
|
|
|
mp_pairheap_t pairheap;
|
|
|
|
mp_obj_t coro;
|
|
|
|
mp_obj_t data;
|
extmod/uasyncio: Fix race with cancelled task waiting on finished task.
This commit fixes a problem with a race between cancellation of task A and
completion of task B, when A waits on B. If task B completes just before
task A is cancelled then the cancellation of A does not work. Instead,
the CancelledError meant to cancel A gets passed through to B (that's
expected behaviour) but B handles it as a "Task exception wasn't retrieved"
scenario, printing out such a message (this is because finished tasks point
their "coro" attribute to themselves to indicate they are done, and
implement the throw() method, but that method inadvertently catches the
CancelledError). The correct behaviour is for B to bounce that
CancelledError back out.
This bug is mainly seen when wait_for() is used, and in that context the
symptoms are:
- occurs when using wait_for(T, S), if the task T being waited on finishes
at exactly the same time as the wait-for timeout S expires
- task T will have run to completion
- the "Task exception wasn't retrieved message" is printed with
"<class 'CancelledError'>" as the error (ie no traceback)
- the wait_for(T, S) call never returns (it's never put back on the
uasyncio run queue) and all tasks waiting on this are blocked forever
from running
- uasyncio otherwise continues to function and other tasks continue to be
scheduled as normal
The fix here reworks the "waiting" attribute of Task to be called "state"
and uses it to indicate whether a task is: running and not awaited on,
running and awaited on, finished and not awaited on, or finished and
awaited on. This means the task does not need to point "coro" to itself to
indicate finished, and also allows removal of the throw() method.
A benefit of this is that "Task exception wasn't retrieved" messages can go
back to being able to print the name of the coroutine function.
Fixes issue #7386.
Signed-off-by: Damien George <damien@micropython.org>
2021-06-14 08:32:51 -04:00
|
|
|
mp_obj_t state;
|
2020-03-12 01:46:20 -04:00
|
|
|
mp_obj_t ph_key;
|
|
|
|
} mp_obj_task_t;
|
|
|
|
|
|
|
|
typedef struct _mp_obj_task_queue_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_task_t *heap;
|
|
|
|
} mp_obj_task_queue_t;
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t task_queue_type;
|
|
|
|
STATIC const mp_obj_type_t task_type;
|
|
|
|
|
|
|
|
STATIC mp_obj_t task_queue_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// Ticks for task ordering in pairing heap
|
|
|
|
|
2022-02-22 03:49:23 -05:00
|
|
|
#ifdef __unix__
|
|
|
|
STATIC mp_obj_t ticks(void) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_int_t ticks_diff(mp_obj_t t1_in, mp_obj_t t0_in) {
|
|
|
|
mp_uint_t t0 = MP_OBJ_SMALL_INT_VALUE(t0_in);
|
|
|
|
mp_uint_t t1 = MP_OBJ_SMALL_INT_VALUE(t1_in);
|
|
|
|
mp_int_t diff = ((t1 - t0 + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1))
|
|
|
|
- MICROPY_PY_UTIME_TICKS_PERIOD / 2;
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
#else
|
2022-02-16 05:22:18 -05:00
|
|
|
#define _TICKS_PERIOD (1lu << 29)
|
|
|
|
#define _TICKS_MAX (_TICKS_PERIOD - 1)
|
|
|
|
#define _TICKS_HALFPERIOD (_TICKS_PERIOD >> 1)
|
|
|
|
|
|
|
|
#define ticks() supervisor_ticks_ms()
|
2020-03-12 01:46:20 -04:00
|
|
|
|
|
|
|
STATIC mp_int_t ticks_diff(mp_obj_t t1_in, mp_obj_t t0_in) {
|
|
|
|
mp_uint_t t0 = MP_OBJ_SMALL_INT_VALUE(t0_in);
|
|
|
|
mp_uint_t t1 = MP_OBJ_SMALL_INT_VALUE(t1_in);
|
2022-02-16 05:22:18 -05:00
|
|
|
mp_int_t diff = ((t1 - t0 + _TICKS_HALFPERIOD) & _TICKS_MAX) - _TICKS_HALFPERIOD;
|
2020-03-12 01:46:20 -04:00
|
|
|
return diff;
|
|
|
|
}
|
2022-02-22 03:49:23 -05:00
|
|
|
#endif
|
2020-03-12 01:46:20 -04:00
|
|
|
|
|
|
|
STATIC int task_lt(mp_pairheap_t *n1, mp_pairheap_t *n2) {
|
|
|
|
mp_obj_task_t *t1 = (mp_obj_task_t *)n1;
|
|
|
|
mp_obj_task_t *t2 = (mp_obj_task_t *)n2;
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(ticks_diff(t1->ph_key, t2->ph_key)) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// TaskQueue class
|
|
|
|
|
|
|
|
STATIC mp_obj_t task_queue_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
|
|
(void)args;
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
|
|
|
mp_obj_task_queue_t *self = m_new_obj(mp_obj_task_queue_t);
|
|
|
|
self->base.type = type;
|
|
|
|
self->heap = (mp_obj_task_t *)mp_pairheap_new(task_lt);
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t task_queue_peek(mp_obj_t self_in) {
|
|
|
|
mp_obj_task_queue_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
if (self->heap == NULL) {
|
|
|
|
return mp_const_none;
|
|
|
|
} else {
|
|
|
|
return MP_OBJ_FROM_PTR(self->heap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_queue_peek_obj, task_queue_peek);
|
|
|
|
|
|
|
|
STATIC mp_obj_t task_queue_push_sorted(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_task_queue_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
mp_obj_task_t *task = MP_OBJ_TO_PTR(args[1]);
|
|
|
|
task->data = mp_const_none;
|
|
|
|
if (n_args == 2) {
|
|
|
|
task->ph_key = ticks();
|
|
|
|
} else {
|
|
|
|
assert(mp_obj_is_small_int(args[2]));
|
|
|
|
task->ph_key = args[2];
|
|
|
|
}
|
2021-06-08 08:45:56 -04:00
|
|
|
self->heap = (mp_obj_task_t *)mp_pairheap_push(task_lt, TASK_PAIRHEAP(self->heap), TASK_PAIRHEAP(task));
|
2020-03-12 01:46:20 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(task_queue_push_sorted_obj, 2, 3, task_queue_push_sorted);
|
|
|
|
|
|
|
|
STATIC mp_obj_t task_queue_pop_head(mp_obj_t self_in) {
|
|
|
|
mp_obj_task_queue_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_obj_task_t *head = (mp_obj_task_t *)mp_pairheap_peek(task_lt, &self->heap->pairheap);
|
|
|
|
if (head == NULL) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("empty heap"));
|
2020-03-12 01:46:20 -04:00
|
|
|
}
|
|
|
|
self->heap = (mp_obj_task_t *)mp_pairheap_pop(task_lt, &self->heap->pairheap);
|
|
|
|
return MP_OBJ_FROM_PTR(head);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_queue_pop_head_obj, task_queue_pop_head);
|
|
|
|
|
|
|
|
STATIC mp_obj_t task_queue_remove(mp_obj_t self_in, mp_obj_t task_in) {
|
|
|
|
mp_obj_task_queue_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_obj_task_t *task = MP_OBJ_TO_PTR(task_in);
|
|
|
|
self->heap = (mp_obj_task_t *)mp_pairheap_delete(task_lt, &self->heap->pairheap, &task->pairheap);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(task_queue_remove_obj, task_queue_remove);
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t task_queue_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_peek), MP_ROM_PTR(&task_queue_peek_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_push_sorted), MP_ROM_PTR(&task_queue_push_sorted_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_push_head), MP_ROM_PTR(&task_queue_push_sorted_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pop_head), MP_ROM_PTR(&task_queue_pop_head_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&task_queue_remove_obj) },
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(task_queue_locals_dict, task_queue_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t task_queue_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_TaskQueue,
|
|
|
|
.make_new = task_queue_make_new,
|
|
|
|
.locals_dict = (mp_obj_dict_t *)&task_queue_locals_dict,
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// Task class
|
|
|
|
|
|
|
|
// This is the core uasyncio context with cur_task, _task_queue and CancelledError.
|
|
|
|
STATIC mp_obj_t uasyncio_context = MP_OBJ_NULL;
|
|
|
|
|
|
|
|
STATIC mp_obj_t task_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 2, false);
|
|
|
|
mp_obj_task_t *self = m_new_obj(mp_obj_task_t);
|
|
|
|
self->pairheap.base.type = type;
|
|
|
|
mp_pairheap_init_node(task_lt, &self->pairheap);
|
|
|
|
self->coro = args[0];
|
|
|
|
self->data = mp_const_none;
|
extmod/uasyncio: Fix race with cancelled task waiting on finished task.
This commit fixes a problem with a race between cancellation of task A and
completion of task B, when A waits on B. If task B completes just before
task A is cancelled then the cancellation of A does not work. Instead,
the CancelledError meant to cancel A gets passed through to B (that's
expected behaviour) but B handles it as a "Task exception wasn't retrieved"
scenario, printing out such a message (this is because finished tasks point
their "coro" attribute to themselves to indicate they are done, and
implement the throw() method, but that method inadvertently catches the
CancelledError). The correct behaviour is for B to bounce that
CancelledError back out.
This bug is mainly seen when wait_for() is used, and in that context the
symptoms are:
- occurs when using wait_for(T, S), if the task T being waited on finishes
at exactly the same time as the wait-for timeout S expires
- task T will have run to completion
- the "Task exception wasn't retrieved message" is printed with
"<class 'CancelledError'>" as the error (ie no traceback)
- the wait_for(T, S) call never returns (it's never put back on the
uasyncio run queue) and all tasks waiting on this are blocked forever
from running
- uasyncio otherwise continues to function and other tasks continue to be
scheduled as normal
The fix here reworks the "waiting" attribute of Task to be called "state"
and uses it to indicate whether a task is: running and not awaited on,
running and awaited on, finished and not awaited on, or finished and
awaited on. This means the task does not need to point "coro" to itself to
indicate finished, and also allows removal of the throw() method.
A benefit of this is that "Task exception wasn't retrieved" messages can go
back to being able to print the name of the coroutine function.
Fixes issue #7386.
Signed-off-by: Damien George <damien@micropython.org>
2021-06-14 08:32:51 -04:00
|
|
|
self->state = TASK_STATE_RUNNING_NOT_WAITED_ON;
|
2020-03-12 01:46:20 -04:00
|
|
|
self->ph_key = MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
if (n_args == 2) {
|
|
|
|
uasyncio_context = args[1];
|
|
|
|
}
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2020-11-30 01:38:30 -05:00
|
|
|
STATIC mp_obj_t task_done(mp_obj_t self_in) {
|
|
|
|
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mp_obj_new_bool(TASK_IS_DONE(self));
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_done_obj, task_done);
|
|
|
|
|
2020-03-12 01:46:20 -04:00
|
|
|
STATIC mp_obj_t task_cancel(mp_obj_t self_in) {
|
|
|
|
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
// Check if task is already finished.
|
2020-11-30 01:38:19 -05:00
|
|
|
if (TASK_IS_DONE(self)) {
|
2020-03-12 01:46:20 -04:00
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
// Can't cancel self (not supported yet).
|
|
|
|
mp_obj_t cur_task = mp_obj_dict_get(uasyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_cur_task));
|
|
|
|
if (self_in == cur_task) {
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("can't cancel self"));
|
2020-03-12 01:46:20 -04:00
|
|
|
}
|
|
|
|
// If Task waits on another task then forward the cancel to the one it's waiting on.
|
|
|
|
while (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(self->data)), MP_OBJ_FROM_PTR(&task_type))) {
|
|
|
|
self = MP_OBJ_TO_PTR(self->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t _task_queue = mp_obj_dict_get(uasyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR__task_queue));
|
|
|
|
|
|
|
|
// Reschedule Task as a cancelled task.
|
|
|
|
mp_obj_t dest[3];
|
|
|
|
mp_load_method_maybe(self->data, MP_QSTR_remove, dest);
|
|
|
|
if (dest[0] != MP_OBJ_NULL) {
|
|
|
|
// Not on the main running queue, remove the task from the queue it's on.
|
|
|
|
dest[2] = MP_OBJ_FROM_PTR(self);
|
|
|
|
mp_call_method_n_kw(1, 0, dest);
|
|
|
|
// _task_queue.push_head(self)
|
|
|
|
dest[0] = _task_queue;
|
|
|
|
dest[1] = MP_OBJ_FROM_PTR(self);
|
|
|
|
task_queue_push_sorted(2, dest);
|
|
|
|
} else if (ticks_diff(self->ph_key, ticks()) > 0) {
|
|
|
|
// On the main running queue but scheduled in the future, so bring it forward to now.
|
|
|
|
// _task_queue.remove(self)
|
|
|
|
task_queue_remove(_task_queue, MP_OBJ_FROM_PTR(self));
|
|
|
|
// _task_queue.push_head(self)
|
|
|
|
dest[0] = _task_queue;
|
|
|
|
dest[1] = MP_OBJ_FROM_PTR(self);
|
|
|
|
task_queue_push_sorted(2, dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
self->data = mp_obj_dict_get(uasyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_CancelledError));
|
|
|
|
|
|
|
|
return mp_const_true;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_cancel_obj, task_cancel);
|
|
|
|
|
2022-02-22 03:11:11 -05:00
|
|
|
STATIC mp_obj_t task_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
|
|
|
|
(void)iter_buf;
|
|
|
|
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
if (TASK_IS_DONE(self)) {
|
|
|
|
// Signal that the completed-task has been await'ed on.
|
|
|
|
self->state = TASK_STATE_DONE_WAS_WAITED_ON;
|
|
|
|
} else if (self->state == TASK_STATE_RUNNING_NOT_WAITED_ON) {
|
|
|
|
// Allocate the waiting queue.
|
|
|
|
self->state = task_queue_make_new(&task_queue_type, 0, 0, NULL);
|
|
|
|
}
|
|
|
|
return self_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t task_iternext(mp_obj_t self_in) {
|
|
|
|
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
if (TASK_IS_DONE(self)) {
|
|
|
|
// Task finished, raise return value to caller so it can continue.
|
|
|
|
nlr_raise(self->data);
|
|
|
|
} else {
|
|
|
|
// Put calling task on waiting queue.
|
|
|
|
mp_obj_t cur_task = mp_obj_dict_get(uasyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_cur_task));
|
|
|
|
mp_obj_t args[2] = { self->state, cur_task };
|
|
|
|
task_queue_push_sorted(2, args);
|
|
|
|
// Set calling task's data to this task that it waits on, to double-link it.
|
|
|
|
((mp_obj_task_t *)MP_OBJ_TO_PTR(cur_task))->data = self_in;
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t task_await(mp_obj_t self_in) {
|
|
|
|
return task_getiter(self_in, NULL);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_await_obj, task_await);
|
|
|
|
|
2020-03-12 01:46:20 -04:00
|
|
|
STATIC void task_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
|
|
|
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
if (dest[0] == MP_OBJ_NULL) {
|
|
|
|
// Load
|
|
|
|
if (attr == MP_QSTR_coro) {
|
|
|
|
dest[0] = self->coro;
|
|
|
|
} else if (attr == MP_QSTR_data) {
|
|
|
|
dest[0] = self->data;
|
extmod/uasyncio: Fix race with cancelled task waiting on finished task.
This commit fixes a problem with a race between cancellation of task A and
completion of task B, when A waits on B. If task B completes just before
task A is cancelled then the cancellation of A does not work. Instead,
the CancelledError meant to cancel A gets passed through to B (that's
expected behaviour) but B handles it as a "Task exception wasn't retrieved"
scenario, printing out such a message (this is because finished tasks point
their "coro" attribute to themselves to indicate they are done, and
implement the throw() method, but that method inadvertently catches the
CancelledError). The correct behaviour is for B to bounce that
CancelledError back out.
This bug is mainly seen when wait_for() is used, and in that context the
symptoms are:
- occurs when using wait_for(T, S), if the task T being waited on finishes
at exactly the same time as the wait-for timeout S expires
- task T will have run to completion
- the "Task exception wasn't retrieved message" is printed with
"<class 'CancelledError'>" as the error (ie no traceback)
- the wait_for(T, S) call never returns (it's never put back on the
uasyncio run queue) and all tasks waiting on this are blocked forever
from running
- uasyncio otherwise continues to function and other tasks continue to be
scheduled as normal
The fix here reworks the "waiting" attribute of Task to be called "state"
and uses it to indicate whether a task is: running and not awaited on,
running and awaited on, finished and not awaited on, or finished and
awaited on. This means the task does not need to point "coro" to itself to
indicate finished, and also allows removal of the throw() method.
A benefit of this is that "Task exception wasn't retrieved" messages can go
back to being able to print the name of the coroutine function.
Fixes issue #7386.
Signed-off-by: Damien George <damien@micropython.org>
2021-06-14 08:32:51 -04:00
|
|
|
} else if (attr == MP_QSTR_state) {
|
|
|
|
dest[0] = self->state;
|
2020-11-30 01:38:30 -05:00
|
|
|
} else if (attr == MP_QSTR_done) {
|
|
|
|
dest[0] = MP_OBJ_FROM_PTR(&task_done_obj);
|
|
|
|
dest[1] = self_in;
|
2020-03-12 01:46:20 -04:00
|
|
|
} else if (attr == MP_QSTR_cancel) {
|
|
|
|
dest[0] = MP_OBJ_FROM_PTR(&task_cancel_obj);
|
|
|
|
dest[1] = self_in;
|
|
|
|
} else if (attr == MP_QSTR_ph_key) {
|
|
|
|
dest[0] = self->ph_key;
|
2022-02-22 03:11:11 -05:00
|
|
|
} else if (attr == MP_QSTR___await__) {
|
|
|
|
dest[0] = MP_OBJ_FROM_PTR(&task_await_obj);
|
|
|
|
dest[1] = self_in;
|
2020-03-12 01:46:20 -04:00
|
|
|
}
|
2022-02-22 03:11:11 -05:00
|
|
|
|
2020-03-12 01:46:20 -04:00
|
|
|
} else if (dest[1] != MP_OBJ_NULL) {
|
|
|
|
// Store
|
extmod/uasyncio: Fix race with cancelled task waiting on finished task.
This commit fixes a problem with a race between cancellation of task A and
completion of task B, when A waits on B. If task B completes just before
task A is cancelled then the cancellation of A does not work. Instead,
the CancelledError meant to cancel A gets passed through to B (that's
expected behaviour) but B handles it as a "Task exception wasn't retrieved"
scenario, printing out such a message (this is because finished tasks point
their "coro" attribute to themselves to indicate they are done, and
implement the throw() method, but that method inadvertently catches the
CancelledError). The correct behaviour is for B to bounce that
CancelledError back out.
This bug is mainly seen when wait_for() is used, and in that context the
symptoms are:
- occurs when using wait_for(T, S), if the task T being waited on finishes
at exactly the same time as the wait-for timeout S expires
- task T will have run to completion
- the "Task exception wasn't retrieved message" is printed with
"<class 'CancelledError'>" as the error (ie no traceback)
- the wait_for(T, S) call never returns (it's never put back on the
uasyncio run queue) and all tasks waiting on this are blocked forever
from running
- uasyncio otherwise continues to function and other tasks continue to be
scheduled as normal
The fix here reworks the "waiting" attribute of Task to be called "state"
and uses it to indicate whether a task is: running and not awaited on,
running and awaited on, finished and not awaited on, or finished and
awaited on. This means the task does not need to point "coro" to itself to
indicate finished, and also allows removal of the throw() method.
A benefit of this is that "Task exception wasn't retrieved" messages can go
back to being able to print the name of the coroutine function.
Fixes issue #7386.
Signed-off-by: Damien George <damien@micropython.org>
2021-06-14 08:32:51 -04:00
|
|
|
if (attr == MP_QSTR_data) {
|
2020-03-12 01:46:20 -04:00
|
|
|
self->data = dest[1];
|
|
|
|
dest[0] = MP_OBJ_NULL;
|
extmod/uasyncio: Fix race with cancelled task waiting on finished task.
This commit fixes a problem with a race between cancellation of task A and
completion of task B, when A waits on B. If task B completes just before
task A is cancelled then the cancellation of A does not work. Instead,
the CancelledError meant to cancel A gets passed through to B (that's
expected behaviour) but B handles it as a "Task exception wasn't retrieved"
scenario, printing out such a message (this is because finished tasks point
their "coro" attribute to themselves to indicate they are done, and
implement the throw() method, but that method inadvertently catches the
CancelledError). The correct behaviour is for B to bounce that
CancelledError back out.
This bug is mainly seen when wait_for() is used, and in that context the
symptoms are:
- occurs when using wait_for(T, S), if the task T being waited on finishes
at exactly the same time as the wait-for timeout S expires
- task T will have run to completion
- the "Task exception wasn't retrieved message" is printed with
"<class 'CancelledError'>" as the error (ie no traceback)
- the wait_for(T, S) call never returns (it's never put back on the
uasyncio run queue) and all tasks waiting on this are blocked forever
from running
- uasyncio otherwise continues to function and other tasks continue to be
scheduled as normal
The fix here reworks the "waiting" attribute of Task to be called "state"
and uses it to indicate whether a task is: running and not awaited on,
running and awaited on, finished and not awaited on, or finished and
awaited on. This means the task does not need to point "coro" to itself to
indicate finished, and also allows removal of the throw() method.
A benefit of this is that "Task exception wasn't retrieved" messages can go
back to being able to print the name of the coroutine function.
Fixes issue #7386.
Signed-off-by: Damien George <damien@micropython.org>
2021-06-14 08:32:51 -04:00
|
|
|
} else if (attr == MP_QSTR_state) {
|
|
|
|
self->state = dest[1];
|
2020-03-12 01:46:20 -04:00
|
|
|
dest[0] = MP_OBJ_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t task_type = {
|
|
|
|
{ &mp_type_type },
|
2021-07-09 15:59:37 -04:00
|
|
|
.flags = MP_TYPE_FLAG_EXTENDED,
|
2020-03-12 01:46:20 -04:00
|
|
|
.name = MP_QSTR_Task,
|
|
|
|
.make_new = task_make_new,
|
|
|
|
.attr = task_attr,
|
2021-07-12 07:57:59 -04:00
|
|
|
MP_TYPE_EXTENDED_FIELDS(
|
2021-07-06 11:37:32 -04:00
|
|
|
.getiter = task_getiter,
|
|
|
|
.iternext = task_iternext,
|
|
|
|
),
|
2020-03-12 01:46:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// C-level uasyncio module
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t mp_module_uasyncio_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__uasyncio) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_TaskQueue), MP_ROM_PTR(&task_queue_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_Task), MP_ROM_PTR(&task_type) },
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_uasyncio_globals, mp_module_uasyncio_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_uasyncio = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_uasyncio_globals,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_UASYNCIO
|