Skip to content

abortHook does not work inside async function #1186

@linfan68

Description

@linfan68

Description
abortHook does not work when the exception is from async function

Steps to Reproduce

import Timer from "timer"

async function throwError() {
  trace("========throwError: An error will thrown now=========\n")
  throw new Error("exception from timer")
}

Timer.set(() => {
  throwError()
}, 1000)

globalThis.abort = function (msg, exception) {
  trace(`abort hook called with "${msg}"\n`)
  trace(exception.stack, "\n")
  return true
}

Expected behavior
(by removing the async keyword of throw Error() function)

instruments: 1,0,0,0,0,2,0,0,0,0,0,8587906,0,100,388,78848,2384,78832,1344,8192,0,1,1,0,0
instruments: 29,0,0,0,0,2,0,0,0,0,29,8587906,0,0,388,78848,2384,78832,848,8192,0,1,1,0,0
========throwError: An error will thrown now=========
abort hook called with "unhandled exception"
Error: exception from timer
 at throwError ()
 at ()
XS abort: unhandled exception

The board reboots after the error

Actual behavior

ases,Turns,System bytes free,CPU 0,CPU 1,Chunk used,Chunk available,Slot used,Slot available,Stack used,Stack available,Garbage collections,Keys used,Modules loaded,Parser used,Floating Point
instruments: 1,0,0,0,0,2,0,0,0,0,0,8587906,0,100,388,78848,2336,78832,1344,8192,0,1,1,0,0
instruments: 29,0,0,0,0,2,0,0,0,0,29,8587906,0,0,388,78848,2336,78832,848,8192,0,1,1,0,0
========throwError: An error will thrown now=========
instruments: 30,0,0,0,0,1,0,0,0,0,31,8587962,0,0,604,78848,2928,78832,960,8192,0,1,1,0,0
instruments: 31,0,0,0,0,1,0,0,0,0,31,8587962,0,0,604,78848,2928,78832,848,8192,0,1,1,0,0

The board does NOT reboot after the error

When an error is thrown inside an async function, the abortHook does not get called, and the code continues to execute. This can be dangerous as the code may run into some uncontrolled states.

Also, I tried if using -d build and test with debugger, the debugger does break when error throwing.

And a few more words about the abortHook:
I believe that being able to capture and diagnose problems in a production environment is crucial. In general, when a program crashes, we should:

  1. Print logs, providing as much context as possible, at least the call stack.
  2. Ideally, store the information in flash memory, so it can be read or uploaded upon next startup.
  3. Restart the program, rather than continuing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions