webassembly/objjsproxy: Fix logic that determines if asyncio is active.

`cur_task` can never be `None` in the webassembly port, so test it for the
top-level task to see if an asyncio Task is active or not.

This fixes a bug where await'ing on a JavaScript awaitable that ends up
raising an error would not be caught on the Python side.  The fix here
makes sure it is caught by Python, as tested by the new test.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2025-09-28 22:16:36 +10:00
parent 68ca22bbc8
commit 094000d418
4 changed files with 41 additions and 3 deletions

View File

@@ -243,8 +243,7 @@ def get_event_loop():
def current_task():
if cur_task is None:
raise RuntimeError("no running event loop")
assert cur_task is not None
return cur_task

View File

@@ -566,7 +566,8 @@ static mp_obj_t jsproxy_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
// decouples the task from the thenable and allows cancelling the task.
if (mp_asyncio_context != MP_OBJ_NULL) {
mp_obj_t cur_task = mp_obj_dict_get(mp_asyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_cur_task));
if (cur_task != mp_const_none) {
mp_obj_t top_level_task = mp_obj_dict_get(mp_asyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR__top_level_task));
if (cur_task != top_level_task) {
mp_obj_t thenable_event_class = mp_obj_dict_get(mp_asyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_ThenableEvent));
mp_obj_t thenable_event = mp_call_function_1(thenable_event_class, self_in);
mp_obj_t dest[2];