question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to receive all elements when using produce with a buffered channel

See original GitHub issue

Run 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:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
elizarovcommented, Feb 22, 2018

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.

0reactions
ansmancommented, Feb 24, 2019

@elizarov Is the only workaround not to use produce for now?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found