Failed to receive all elements when using produce with a buffered channel
See original GitHub issueRun the following code
fun main(args: Array<String>) = runBlocking<Unit> {
val producer = produce<Int>(coroutineContext, capacity = 1) {
for(i in 1..2) {
send(i)
println("Sent: $i")
}
}
for (x in producer) {
println("Received: $x")
}
}
The output is:
Sent: 1
Sent: 2
Received: 1
while expected output is:
Sent: 1
Sent: 2
Received: 1
Received: 2
The problem shows up with any buffered chennel (ArrayChannel
and LinkedListChannel
) and does not show up with RendezvousChannel
(capacity = 0
). It only affects produce
builder. Replacing produce
with an “manual” code via launch
works as expected:
val producer = Channel<Int>(1)
launch(coroutineContext) {
for(i in 1..2) {
producer.send(i)
println("Sent: $i")
}
producer.close()
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Using buffered channel for errors in go when listening and ...
Short answer: For any channel (buffered or not), channel reads block if nothing is written to the channel. For non-buffered channels, ...
Read more >Using Go Channels - Part 2 - Buffered Channels - g14a
An unbuffered channel contains only 1 item and it blocks all sends until there is a receiver. Introduction to Buffered channels. We initialize...
Read more >Buffered Channel in Golang - GeeksforGeeks
Buffered channels allows to accept a limited number of values without a corresponding receiver for those values.
Read more >Buffered Channels and Worker Pools in Go - golangbot.com
A detailed tutorial about how buffered channels work and how they can be used to create worker pools in Go.
Read more >Buffered Channels In Go — What Are They Good For? - Medium
In a previous blog post we looked at how to build an unbounded channel, where writes to the channel would never block.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The problem is actually in
produce
. It cancels(sic!) the underlying channel (instead of closing) when producing coroutine returns. That’s the bug. will be fixed soon.@elizarov Is the only workaround not to use
produce
for now?