fix wrong unchecked null cast (potential NPE) #445
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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 🙃