-
Notifications
You must be signed in to change notification settings - Fork 5
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
chann: unbounded channel may result in a spin loop if there is no receiver after the close #3
Comments
If there is no receiver, this default branch can prevent the goroutine block forever at the close. If there is a receiver, the receiver will always receive all unsent data because |
Won't the default branch cause a cpu spin if executed? |
Interesting. Looks like a bug. Thanks for reporting! |
default
statement?
I believe ad8c992 did not solve the issue but actually introduced a different bug. Consider this test: func TestUnboundedChannRecvAfterClose(t *testing.T) {
var wg sync.WaitGroup
ch := chann.New[int]()
wg.Add(1)
c := 0
go func() {
for range ch.Out() {
c++
}
wg.Done()
}()
for i := 0; i < 2048; i++ {
ch.In() <- 42
}
ch.Close()
wg.Wait()
if c != 2048 {
t.Fatalf("not all elements are received after channel being closed, want %v got %v", 2048, c)
}
} |
How about we just delete deafult? Leaving the decision about the data to the consumer? |
Is this a typo? |
Removing the default will lead to leaking goroutine. We must always guarantee the processing goroutine will terminate even if there is no receiver. |
Then we may have to just close out chan and delete all data immediately. |
Deleting all data immediately may not be a good idea because there may be a receiver waiting for all data. |
chann/chann.go
Line 176 in 619cda1
If not will it be any problem?
The text was updated successfully, but these errors were encountered: