Skip to content

Fix invalid flow coroutine context#251

Merged
devnatan merged 1 commit intodevnatan:mainfrom
ptitjes:fix-flow-context
Feb 15, 2026
Merged

Fix invalid flow coroutine context#251
devnatan merged 1 commit intodevnatan:mainfrom
ptitjes:fix-flow-context

Conversation

@ptitjes
Copy link
Copy Markdown
Contributor

@ptitjes ptitjes commented Feb 12, 2026

Currently, some flow builders are used alongside with Ktor Http client. However, this makes the flow emissions being done in the wrong coroutine context. channelFlow should be used in these cases.

Othewise, when using Ktor 3.4.0, you will get these kind of exceptions:

java.lang.IllegalStateException: Flow invariant is violated:
		Flow was collected in [RunningInRunTest, kotlinx.coroutines.test.TestCoroutineScheduler@4027879f, kotlinx.coroutines.test.TestScopeKt$TestScope$$inlined$CoroutineExceptionHandler$1@1be6036f, BackgroundWork, CoroutineId(23), "coroutine#23":ScopeCoroutine{Active}@2ce0344d, Dispatchers.IO],
		but emission happened in [RunningInRunTest, kotlinx.coroutines.test.TestCoroutineScheduler@4027879f, kotlinx.coroutines.test.TestScopeKt$TestScope$$inlined$CoroutineExceptionHandler$1@1be6036f, BackgroundWork, CoroutineId(23), kotlinx.coroutines.UndispatchedMarker@1f2e5d3a, "coroutine#23":UndispatchedCoroutine{Active}@63c93a3f, Dispatchers.IO].
		Please refer to 'flow' documentation or use 'flowOn' instead
	at kotlinx.coroutines.flow.internal.SafeCollector_commonKt.checkContext(SafeCollector.common.kt:84)
	at kotlinx.coroutines.flow.internal.SafeCollector.checkContext(SafeCollector.kt:132)
	at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:109)
	at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:82)
	at me.devnatan.dockerkt.resource.image.ImageResource$pull$1$2.invokeSuspend(ImageResource.kt:46)
	at me.devnatan.dockerkt.resource.image.ImageResource$pull$1$2.invoke(ImageResource.kt)
	at me.devnatan.dockerkt.resource.image.ImageResource$pull$1$2.invoke(ImageResource.kt)
	at io.ktor.client.statement.HttpStatement$execute$2$1.invokeSuspend(HttpStatement.kt:63)
	at io.ktor.client.statement.HttpStatement$execute$2$1.invoke(HttpStatement.kt)
	at io.ktor.client.statement.HttpStatement$execute$2$1.invoke(HttpStatement.kt)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndspatched(Undispatched.kt:66)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:43)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:165)
	at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:62)
	at io.ktor.client.statement.HttpStatement$execute$1.invokeSuspend(HttpStatement.kt)
	at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt:42)
	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:62)
	at me.devnatan.dockerkt.resource.image.ImageResource$pull$1.invokeSuspend(ImageResource.kt:42)
	...

@ptitjes
Copy link
Copy Markdown
Contributor Author

ptitjes commented Feb 12, 2026

@devnatan btw, you have those errors in Dependabot's MR: https://github.com/devnatan/docker-kotlin/actions/runs/21557264545/job/62115734965?pr=245#step:6:120

Copy link
Copy Markdown
Owner

@devnatan devnatan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thx!!

@devnatan devnatan merged commit 3afbc80 into devnatan:main Feb 15, 2026
2 of 4 checks passed
@ptitjes ptitjes deleted the fix-flow-context branch February 15, 2026 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants