tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
14.82k stars 872 forks source link

time.NewTimer() fails from init() #4123

Closed xudongzheng closed 5 months ago

xudongzheng commented 5 months ago

The following code results in a fault:

package main

import "time"

var timer = time.NewTimer(time.Second * 5)

func main() {
    println("Hello world!")
}

Issue also happens if time.NewTimer() is called from init().

The following code works correctly

package main

import "time"

var timer *time.Timer

func main() {
    timer = time.NewTimer(time.Second * 5)
    println("Hello world!")
}

This was tested on both desktop tinygo build -o ~/hello ~/hello.go and RP2040 Pico.

It looks like tn.timer.f here is nil: https://github.com/tinygo-org/tinygo/blob/f4395147039fc356277399853821cb772ebda883/src/runtime/time.go#L39

aykevl commented 5 months ago

Thanks, here is a fix: https://github.com/tinygo-org/tinygo/pull/4134

xudongzheng commented 5 months ago

Thanks! Closing as I've confirmed the fix.