Skip to content

Launching coroutine in channelFlow and then canceling it produces JobCancellationException #1334

Closed
@gmarchelos

Description

@gmarchelos

I have this simple flow

runBlocking(Dispatchers.Default) {
	channelFlow {
		val j = launch {
			delay(10000000)
		}

		var i = 0
		while (true) {
			delay(1000)
			send(i++)
			if (i == 3) j.cancelAndJoin()
		}
	}.collect {
		println("received: $it")
	}
}

Actual result:

received: 0
received: 1
received: 2
Exception in thread "main" kotlinx.coroutines.JobCancellationException: Job was cancelled; job=StandaloneCoroutine{Cancelled}@185d8b6

I expected to cancel launched coroutine but looks like it also cancels channelFlow.
@qwwdfsad, Is it right behaviour ?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions