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

stream.Wait() panic when callback is nil #144

Open
rhyshmwang opened this issue Nov 15, 2024 · 1 comment
Open

stream.Wait() panic when callback is nil #144

rhyshmwang opened this issue Nov 15, 2024 · 1 comment

Comments

@rhyshmwang
Copy link

rhyshmwang commented Nov 15, 2024

func Wait will panic, because callback is nil.

Can func Wait support nil callback? Maybe do nothing when callback is nil is better

st := stream.New().WithMaxGoroutines(20)

st.Go(func() stream.Callback {
    return nil
}

st.Wait()
@rhyshmwang
Copy link
Author

rhyshmwang commented Nov 15, 2024

There is an example. Recover() in st.Go works, but st.Wait() still panic.

package main
import (
    "fmt"
    "github.com/sourcegraph/conc/stream"
    "time"
)
func main() {
    for i := 0; i < 5; i++ {
       st := stream.New().WithMaxGoroutines(20)
       for i := 0; i < 10; i++ {
          idx := i
          st.Go(func() (callback stream.Callback) {
             //callback = func() {} // if comment will panic
             defer func() {
                if err := recover(); err != nil {
                   fmt.Println("haha", err)
                }
             }()
             if idx == 2 || idx == 4 {
                panic(idx) // this panic will be recovered
             }
             return func() {
             }
          })
       }
       st.Wait() // panic occurs here
    }
    time.Sleep(1 * time.Second)
}

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