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

Cannot stop backend goroutine #1686

Open
kimkit opened this issue Mar 2, 2025 · 0 comments
Open

Cannot stop backend goroutine #1686

kimkit opened this issue Mar 2, 2025 · 0 comments

Comments

@kimkit
Copy link

kimkit commented Mar 2, 2025

The following program sample.go triggers an unexpected result

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/traefik/yaegi/interp"
	"github.com/traefik/yaegi/stdlib"
)

func main() {
	i := interp.New(interp.Options{})
	i.Use(stdlib.Symbols)
	i.Use(interp.Symbols)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)

	_, err := i.EvalWithContext(ctx, `package main
	import (
		"fmt"
		"time"
	)

	func main() {
		fmt.Println("print in main")
		go func() {
			for {
				select {
				case <-time.After(time.Second):
					fmt.Println("print in goroutine")
				}
			}
		}()
	}`)
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		<-ctx.Done()
		fmt.Println("context canceled")
		// i.Stop() // calling stop method manually can stop backend goroutine
	}()
	time.Sleep(time.Second * 3)
	cancel()

	select {}
}

Expected result

$ go run ./sample.go 
print in main
print in goroutine
print in goroutine
context canceled
print in goroutine
print in goroutine
print in goroutine
print in goroutine
print in goroutine
print in goroutine
print in goroutine

Got

$ yaegi ./sample.go
// output

Yaegi Version

v0.16.1

Additional Notes

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant