reugn / async

Synchronization and asynchronous computation package for Go

Home Page:https://pkg.go.dev/github.com/reugn/async

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get with timeout cause goroutine leak

liguangsheng opened this issue · comments

If I use the Get method with a 2s timeout, then a goroutine leak will occur from the call to comple after 2s.

package main

import (
	"fmt"
	"runtime"
	"sync"
	"time"

	"github.com/reugn/async"
)

func main() {
	var wg sync.WaitGroup

	fmt.Println(runtime.NumGoroutine()) // 101

	for i := 0; i < 100; i++ {
		promise := async.NewPromise[string]()
		go func() {
			wg.Add(1)
			defer wg.Done()
			time.Sleep(time.Second * 4)
			promise.Success("OK")
		}()
		go func() {
			wg.Add(1)
			defer wg.Done()
			fut := promise.Future()
			_, _ = fut.Get(time.Second * 2)
			time.Sleep(time.Second * 4)
			_, _ = fut.Join()
		}()
	}

	wg.Wait()
	fmt.Println(runtime.NumGoroutine()) // 101
}