yaegi icon indicating copy to clipboard operation
yaegi copied to clipboard

Cannot stop backend goroutine

Open kimkit opened this issue 1 year ago • 0 comments

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

kimkit avatar Mar 02 '25 10:03 kimkit