Skip to content

Fix flows that emit Units #323

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

Merged
merged 2 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ internal class KrpcServerService<@Rpc T : Any>(
if (callable.isNonSuspendFunction && !markedNonSuspending) {
@Suppress("detekt.MaxLineLength")
error(
"Server flow returned from non-suspend function but marked so by a client: ${descriptor.fqName}::$callableName." +
"Server flow returned from non-suspend function but marked so by a client: ${descriptor.fqName}::$callableName. " +
"Probable cause is outdated client version, that does not support non-suspending flows, " +
"but calls the function with the same name. Change the function name or update the client."
)
Expand All @@ -181,7 +181,7 @@ internal class KrpcServerService<@Rpc T : Any>(
}
}.let { interceptedValue ->
// KRPC-173
if (callable.returnType.kType == typeOf<Unit>()) {
if (!callable.isNonSuspendFunction && callable.returnType.kType == typeOf<Unit>()) {
Unit
} else {
interceptedValue
Expand All @@ -194,8 +194,9 @@ internal class KrpcServerService<@Rpc T : Any>(
if (callable.isNonSuspendFunction) {
if (value !is Flow<*>) {
error(
"Return value of non-suspend function must be a non nullable flow, " +
"but was: ${value?.let { it::class.simpleName }}"
"Return value of non-suspend function '${callable.name}' " +
"with callId '$callId' must be a non nullable flow, " +
"but was: ${value?.let { it::class.simpleName }}"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ interface KrpcTestService : RemoteService {

suspend fun krpc173()

fun unitFlow(): Flow<Unit>

val plainFlowOfInts : Flow<Int>

val plainFlowOfFlowsOfInts : Flow<Flow<Int>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ class KrpcTestServiceBackend(override val coroutineContext: CoroutineContext) :
doWork()
}

override fun unitFlow(): Flow<Unit> {
return flow {
emit(Unit)
}
}

override val plainFlowOfInts: Flow<Int> = plainFlow { it }

override val plainFlowOfFlowsOfInts: Flow<Flow<Int>> = plainFlow { plainFlow { i -> i } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,11 @@ abstract class KrpcTransportTestBase {
assertEquals(Unit, client.krpc173())
}

@Test
fun testUnitFlow() = runTest {
assertEquals(Unit, client.unitFlow().toList().single())
}

@Test
fun testPlainFlowOfInts() = runTest {
val flow = client.plainFlowOfInts.toList()
Expand Down