tinygo
tinygo copied to clipboard
runtime: reset on abort for cortexm
Panics happen. And right now (and still by default after this PR) system just hangs. Feels like it can be beneficial to let users choose whether to hang or restart the system on panic.
It is not always possible to have easy physical access to embedded devices. Some may have been deployed to run 24/7 at a remote site.
Please suggest better API, this is just some quick attempt to make it useable.
Verified on my Nano RP2040, 24/7 setup, panicking and rebooting on wifinina and lsm6dsox weird situations that hard to catch and debug.
Made it more flexible with function, as suggested.
OnPanic in runtime/panic.go to enable it for all architectures, not just cortexm.
Example of resetting system on panic (for cortexm).
Once and if we settle on a final implementation, I squash commits in this PR.
After a short discussion in slack, I feel like I shall not push this PR anymore. It's problematic with extending public API in general and runtime package in particular (that we want to keep close API-wise to upstream Go).
Feel free to close PR. @aykevl
I've solved resets on panic for me in my fork and just wanted to enable others do the same. But the costs outweight benefits and if anyone has the same problem they shall be able to find the solution now. I'll try and solve the problem (resets on panic for 24/7 devices) with classic defer/recover and, if successful, can probably write about the pattern in tinygo docs instead.
func main() {
defer func() {
a := recover()
if a != nil {
println("panic encountered. recovering:",a)
time.Sleep(time.Second)
go main()
runtime.Goexit() // Courtesy of the #darkarts channel on Slack.
}
}()
panic("oh no!")
}
I believe this has the desired effect on upstream Go. Not sure if it would work on TinyGo though.
I believe this has the desired effect on upstream Go. Not sure if it would work on TinyGo though.
Unfortunately no.
This does not work in TinyGo, since abort() function just lock on arm.Asm("wfi") and never executes defer
Check an example in my fork's branch https://github.com/ysoldak/tinygo/blob/defer-on-panic_does-not-work/src/examples/resetonpanic/resetonpanic.go
Recommend close this PR as @ysoldak suggests and focus on #2742.
Closing as requested.