I'm trying to make a plugin that logs all calls an...
# ktor
m
I'm trying to make a plugin that logs all calls and exceptions, but the following snippet for my plugin seems to hang because I'm catching the exception, even if I'm re-throwing it later.
Copy code
pipeline.intercept(ApplicationCallPipeline.Monitoring) {
    val start = Clock.System.now()
    val (exception, duration) = measureTimedValue {
        try {
            proceed()
            null
        } catch (e: Exception) {
            exceptionsChannel.send(e)
            e
        }
    }

    // ...

    if (exception != null) {
        throw exception
    }
}
Am I not supposed to call proceed, or should I catch exceptions some other way?
d
If your channel is configured as rendezvous, it could suspend indefinitely if the receiver suspended or blocked on other things.
For testing purposes, you might consider logging the exception when you catch it, logging that the channel send was successful, and logging that you’re about to re-throw.
m
The channel isn't relevant, just catching alone seems enough to halt the pipeline until the client disconnects.
a
On my machine, the interceptor works as expected if the code for sending to the channel is removed.
m
Strange, it was still happening for me when I'd removed it. Assuming it's the channel causing the problem, how would I still emit the exception to it?
a
You can try to increase the channel's buffer size for testing.
m
Where can I do that? I just create it with
Channel<Exception>()
which I'd assumed would grow as necessary similar to a List.
a
Copy code
Channel<Exception>(10)
What do you mean by growing channel?
By default, it will suspend if more than one element is sent and hasn't been received on the other end.
m
Ah, in that case, what data type does kx.coroutines provide that acts like a MutableList, but with the ability to listen for new elements being appended?
a
I suggest asking it in the #C1CFAFJSK channel.