1 day ago · Tech · 0 comments

Go 1.27 is getting a goroutine leak detector in runtime/pprof. The proposal was accepted in April. A few common goroutine leaks # A goroutine leaks when it blocks forever on a channel or a lock that nothing will ever release, so it lingers for the life of the process. I’ve been using uber-go/goleak to catch them in tests. One is an early return that strands a sender, which I covered in Early return and goroutine leak . It looks like this: func run(tasks []func() error) error { errs := make(chan error) // unbuffered var wg sync.WaitGroup for _, task := range tasks { wg.Go(func() { errs <- task() }) // (1) } for range tasks { if err := <-errs; err != nil { return err // (2) } } wg.Wait() return nil } Here: (1) each task sends its result on the unbuffered channel through wg.Go (2) the first error returns early, so the tasks still queued to send block forever Giving errs a buffer big enough for every task, or draining all the results before returning, keeps the sends from blocking. A…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.