Description
The uasyncio library is not working because of this problem:
Try running a code like this:
import uasyncio as asyncio
import machine
async def test():
print("test")
machine.idle()
loop=asyncio.get_event_loop()
loop.run_until_complete(test())
It is very simple but the test() coroutine is never entered and instead after a few seconds, the watchdog gets triggered:
Task watchdog got triggered. The following tasks did not reset the watchdog in time:
- mp_task (CPU 0/1)
Tasks currently running:
CPU 0: IDLE
CPU 1: IDLE
So I got into debugging the issue and found, that the function utimeq.peektime() that is used inside the asyncio.wait() function (core.py line 71) for getting the delay to wait, is returning an incredibly high number leading to a very long poller.ipoll() triggering the watchdog.
This was the output:
>>> loop.run_until_complete(test())
DEBUG:uasyncio.core:Scheduling (88451, <generator object '_run_and_stop' at 3f8189e0>, ())
DEBUG:uasyncio:poll.wait(532638518)
As you can see the function utimeq.peektime() returns a huge number even before the coroutine is started, but it should return 0 (?) in the first iteration so self.wait() is not called yet as you can see in the following test on esp8266:
async def test():
print("hi")
await asyncio.sleep(1)
print("ho")
loop.run_until_complete(test())
debug::Scheduling (110138, <generator object '_run_and_stop' at 3fff4b20>, ())
debug::Next coroutine to run: (110138, <generator object '_run_and_stop' at 3fff4b20>, ())
debug::Coroutine <generator object '_run_and_stop' at 3fff4b20> send args: ()
hi
debug::Coroutine <generator object '_run_and_stop' at 3fff4b20> yield result: 1000
debug::Scheduling (111170, <generator object '_run_and_stop' at 3fff4b20>, ())
debug::poll.wait(992)
debug::Next coroutine to run: (111170, <generator object '_run_and_stop' at 3fff4b20>, ())
debug::Coroutine <generator object '_run_and_stop' at 3fff4b20> send args: ()
ho
debug::Coroutine <generator object '_run_and_stop' at 3fff4b20> yield result: <StopLoop object at 3fff5380>
The function utimeq.peektime() in modutimeq.c is this:
STATIC mp_obj_t mod_utimeq_peektime(mp_obj_t heap_in) {
mp_obj_utimeq_t *heap = get_heap(heap_in);
if (heap->len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
}
struct qentry *item = &heap->items[0];
return MP_OBJ_NEW_SMALL_INT(item->time);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_peektime_obj, mod_utimeq_peektime);
But that's where my knowledge ends, I have no idea why this function returns this huge number even before the coroutine is executed.