Skip to content

time.Sleep under 300us causes ATSAMD21 to freeze in some circumstances #526

@bgould

Description

@bgould

Go version: go version go1.12.9 linux/amd64
Tinygo version: tinygo version 0.8.0-dev linux/amd64 (commit 8a5fa51)

This program will cause Itsy Bitsy M0 to freeze up after println("about to sleep"):

package main

import (
        "fmt"
        "time"
)

func main() {
        for i := 0; ; i++ {
                if i > 0 && i%10 == 0 {
                        println("about to sleep")
                        time.Sleep(30 * time.Microsecond)
                        println("waking up")
                }
                fmt.Printf("tick %d\r\n", i)
                time.Sleep(1 * time.Second)
        }
}

With trial and error I found that increasing the sleep duration to something around 300us or so allows the program to work as expected; anything less seems to leading to the freezing behavior.

Based on advice from @jadr2ddude I tried enabling schedulerDebug = true in src/runtime/scheduler.go but there was no output. This led me to add an empty goroutine which not only caused the scheduleDebug messages to be outputted as expected, but it evidently causes the above code to work without freezing:

package main

import (
        "fmt"
        "time"
)

func main() {
        go func() {
        }()
        for i := 0; ; i++ {
                if i > 0 && i%10 == 0 {
                        println("about to sleep")
                        time.Sleep(30 * time.Microsecond)
                        println("waking up")
                }
                fmt.Printf("tick %d\r\n", i)
                time.Sleep(1 * time.Second)
        }
}

It appears that when the scheduler is not activate in the program, short sleeps as shown above are not working. I should note as well that I tested this with #513 and those changes appear to have solved the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions