How to properly handle exceptions happening on the...
# kotlinx-rpc
n
How to properly handle exceptions happening on the server side? Due to browser limitations, I am sending the token as a URL parameter and doing validation in the server setup, like:
Copy code
rpc(RpcEndpoints.ADMIN) {
                val rawJwt = call.request.queryParameters[Constants.TOKEN_URL]
                val session = checkAuth(rawJwt)
                //setup services
            }
However, if an exception happens, the client never gets an answer:
Copy code
val rpcClient = client.rpc { ... }
            println("#1")
            val protectedService = rpcClient.withService<AdminStatusService>()
            println("#2")
            protectedService.currentUser() // suspend function
            println("#3")
Output is:
Copy code
#1
#2
<Exception from server>
<Timeout>
Is it a bug, or am I doing it totally wrong?
Through further experimentation, I noticed that the exception server does not send any messages; instead, it closes the connection. Then I tried to listen if connection was alive.
However, I found that calling
rpcClient.awaitCompletion()
too soon returns immediately. I am wondering what the proper way to wait until the client closes is.
I'm guessing that this snippet can work only if
createNewClient()
somehow triggers initialization. So I ended with:
Copy code
val job = launch {
                val protectedService = rpcClient.withService<AdminStatusService>()
            }
            rpcClient.webSocketSession.await()
            rpcClient.awaitCompletion()
            job.cancel(CancellationException("Socket closed"))
👀 1
a
However, I found that calling
rpcClient.awaitCompletion()
too soon returns immediately. I am wondering what the proper way to wait until the client closes is.
That's a good, point, probably it shouldn't
Is it a bug, or am I doing it totally wrong?
Probably a bug and probably this one https://github.com/Kotlin/kotlinx-rpc/pull/505
n
I was reviewing this PR, but my case looks different; I create a client and await immediate without giving a chance to call anything RPC related which triggers
callServerStreaming
which makes
isTransportReady = true
. Until it is true
awaitCompletion()
completes immediate
This code could work for my case, but I guess
isTransportReady
check has some good reasons: https://github.com/Kotlin/kotlinx-rpc/commit/df7b1ce46ec07efda6284a916bf41102330dac61
Copy code
public suspend fun awaitCompletion() {
        internalScope.coroutineContext.job.join()
    }
a
Yeah, early return is indeed a different issue, but your original post (and absence of cancellation on currentUser suspension) is about the linked PR
n
Sorry. Yes, it could be fixed. I will let you know after testing. BTW, is there someplace to write this kind of integration test?
a
Pardon me for the wording, I meant it's an issue on our side, so we'll fix it in 0.10.1
👌 1