Skip to content

Conversation

@y9maly
Copy link
Contributor

@y9maly y9maly commented Aug 12, 2025

Problem Description

I encountered an issue where an RPC service returned null even though the RPC function never actually returns null

After investigation, it turned out that the error occurred during the deserialization of the server's response. However, kotlinx-rpc silently ignored the deserialization exception and performed an unchecked cast of null to the RPC function's return type T

This happens inside the KrpcClient class in the handleServerStreamingMessage function:

is KrpcCallMessage.CallSuccess, is KrpcCallMessage.StreamMessage -> {
    val value = runCatching {
        val serializerResult = serialFormat.serializersModule
            .buildContextual(callable.returnType)
        decodeMessageData(serialFormat, serializerResult, message)
    }
    @Suppress("UNCHECKED_CAST")
    channel.send(value.getOrNull() as T) // Thats not ok
}

When deserialization fails, getOrNull() returns null, which is then unchecked-cast to T. This may propagate unexpected null values

Expected behaviour

If deserialization fails, the error should be propagated back to the remote function call site, instead of sending a null value through the channel

Replacing getOrNull() with getOrThrow() would throw the exception immediately inside the library's internals, but this would not deliver it to the location where the developer actually invokes the remote function.
Therefore, I switched from Channel to Channel<Result>, allowing the deserialization error to be captured and then rethrown at the correct call site

This is my first attempt at contributing, please don’t hit me 🙃

@Mr3zee
Copy link
Member

Mr3zee commented Aug 12, 2025

Hey @y9maly! Thanks for you contribution! I'll take a look at it tomorrow

@Mr3zee
Copy link
Member

Mr3zee commented Aug 13, 2025

@y9maly lgtm! Please write a test, and then I'll run the CI checks. If everything is fine - we'll merge it.

@Mr3zee Mr3zee self-requested a review August 13, 2025 14:44
@Mr3zee Mr3zee added the bug Something isn't working label Aug 13, 2025
@y9maly
Copy link
Contributor Author

y9maly commented Aug 13, 2025

@Mr3zee i did it

Copy link
Member

@Mr3zee Mr3zee left a comment

Choose a reason for hiding this comment

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

@y9maly thanks! One small change, please, while I'll run the tests

…cTransportTestBase.kt

Co-authored-by: Alexander Sysoev <[email protected]>
@Mr3zee
Copy link
Member

Mr3zee commented Aug 13, 2025

Our CI is broken, so I ran tests locally, and everything seams good.
Thank you for your great contribution!

@Mr3zee Mr3zee self-requested a review August 13, 2025 20:21
@Mr3zee Mr3zee merged commit 9244646 into Kotlin:main Aug 13, 2025
@Mr3zee
Copy link
Member

Mr3zee commented Aug 13, 2025

Will be released in 0.10.0 in upcoming weeks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants