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

Channel.receiveAsFlow doesn't document the prompt cancellation guarantee #4337

Open
dkhalanskyjb opened this issue Jan 20, 2025 · 0 comments
Labels
docs KDoc and API reference

Comments

@dkhalanskyjb
Copy link
Collaborator

Nothing in the docs says that channel elements can be lost (and the resources can be leaked) if collection gets cancelled, but it can happen.

        val channel = Channel<Int>(onUndeliveredElement = {
            println("Undelivered element: $it")
        })
        val totalElements = 100
        val expectedElements = AtomicInteger(totalElements)
        launch(Dispatchers.Default) {
            repeat(totalElements) {
                channel.send(it)
            }
            channel.close()
        }
        while(!channel.isClosedForReceive) {
            coroutineScope {
                val job = launch {
                    channel.receiveAsFlow().collect {
                        expectedElements.decrementAndGet()
                    }
                }
                launch(Dispatchers.Default) {
                    job.cancel()
                }
            }
        }
        assertEquals(0, expectedElements.get())

One of the runs prints:

Undelivered element: 38
Undelivered element: 44
Undelivered element: 46
Undelivered element: 49
Undelivered element: 51
Undelivered element: 53
Undelivered element: 55
Undelivered element: 62
Undelivered element: 66
Undelivered element: 68
Undelivered element: 72
Undelivered element: 76
Undelivered element: 78
Undelivered element: 82
Undelivered element: 85
Undelivered element: 87
Undelivered element: 89
Undelivered element: 91
Undelivered element: 93
Undelivered element: 95
Undelivered element: 97

Expected :0
Actual   :21
@dkhalanskyjb dkhalanskyjb added the docs KDoc and API reference label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs KDoc and API reference
Projects
None yet
Development

No branches or pull requests

1 participant