-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Flow.catch skips upstream exceptions #4159
Comments
Could you provide any reproducer for the scenario where an exception is thrown upstream but is completely ignored? |
In this scenario, it is possible to observe "Catch block" upstream exception without "Catch operator: " being invoked though |
suspend fun main() {
val flow =
flow<String> {
emit("yo")
throw UnsupportedOperationException()
}
.catch { println(".catch caught $it") }
println(flow.first())
} outputs
whilst: suspend fun main() {
val flow =
flow<String> {
emit("yo")
throw UnsupportedOperationException()
}
.catch { println(".catch caught $it") }
.flowOn(Dispatchers.Default)
println(flow.first())
} outputs
whilst: suspend fun main() {
val flow =
flow<String> {
emit("yo")
throw UnsupportedOperationException()
}
.flowOn(Dispatchers.Default)
.catch { println(".catch caught $it") }
println(flow.first())
} outputs
Was about to create a new issue for this but came across this one, is it the same bug? |
@qwwdfsad do you have any insight into what is going on here? |
@nbransby this is a leaking behaviour of multiple aspects: concurrency imposed by It's a bit more complicated than just an omission or a trivial bug and I'm yet to make my mind about this behaviour, its validity and whether we can fix it without breaking even more stuff in the meantime |
Yes, it looks like it is the same bug. |
It looks like under some race conditions using the Flow.catch operator skips upstream exceptions. For example:
We are using pretty old 1.6.1, but since then the catch operator implementation was not changed.
From the source code of the catch operator it looks like the behaviour is possible when both downstream and upstream exceptions are caught, which can happen in race conditions.
We believe it's totally unclear from the documentation and especially from the semantics of 'catch' operator, that any upstream exception could skip the operator. For now we had to switch channelFlow builder (instead of flow) and to use try-catch.
Could it be that the code here should be replaced with "return e"?
The text was updated successfully, but these errors were encountered: