Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select dead locked by time.After #4568

Open
d-enk opened this issue Oct 30, 2024 · 2 comments
Open

select dead locked by time.After #4568

d-enk opened this issue Oct 30, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@d-enk
Copy link

d-enk commented Oct 30, 2024

on my machine
tinygo version 0.34.0 linux/amd64 (using go version go1.23.2 and LLVM version 18.1.2

go.dev/play

package main

import (
	"fmt"
	"time"
)

func debug(v ...any) {
	fmt.Println(time.Now().Format(".000000"), v)
}

func Run(input <-chan int) {
	taskDone := make(chan struct{}, 1)

	for {

		debug("SELECT")

		select {
		case <-time.After(time.Millisecond):
			panic("timeout")

		case <-taskDone:
			debug("WAKEUP BY TASK DONE")

		case value := <-input:
			debug("WAKEUP BY INPUT")

			go func() {
				fmt.Println("PROCESS...", value)

				select {
				case taskDone <- struct{}{}:
				default: // already notified
				}
			}()

		}

		debug("SELECT DONE")
	}
}

func main() {
	input := make(chan int, 3)

	for i := 0; i < cap(input); i++ {
		input <- i
	}
	// close(input)

	Run(input)
}

output by playgound

​.001075 [SELECT]
​.003140 [WAKEUP BY INPUT]
​.003225 [SELECT DONE]
​.003240 [SELECT]
​.003265 [WAKEUP BY INPUT]
​.005485 [SELECT DONE]
​.005555 [SELECT]
​.005575 [WAKEUP BY INPUT]
​.005700 [SELECT DONE]
​.005725 [SELECT]
​.005845 [PROCESS... 0]
​.006030 [PROCESS... 1]
​.006050 [PROCESS... 2]
​.006075 [WAKEUP BY TASK DONE]
​.006085 [SELECT DONE]
​.006095 [SELECT]
​.006105 [WAKEUP BY TASK DONE]
​.006120 [SELECT DONE]
​.006130 [SELECT]

but then expected: ​panic: timeout

@aykevl aykevl added the bug Something isn't working label Oct 30, 2024
@aykevl
Copy link
Member

aykevl commented Oct 30, 2024

I can confirm this bug. Also verified it's present in v0.33.0, so it's not a regression in the latest release.

aykevl added a commit that referenced this issue Oct 30, 2024
There are rare cases where this can happen, see for example
#4568
@aykevl
Copy link
Member

aykevl commented Oct 30, 2024

Here is a fix: #4569
The issue was that the runtime would try to sleep for a negative duration, which was cast to an unsigned integer, which meant it would have been sleeping for a very long time.

aykevl added a commit that referenced this issue Nov 1, 2024
There are rare cases where this can happen, see for example
#4568
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants