https://kotlinlang.org logo
Title
j

jeggy

06/07/2021, 5:29 PM
class XmlConverter : ContentConverter {
  override suspend fun convertForReceive(context: PipelineContext<ApplicationReceiveRequest, ApplicationCall>): Any?  {
    val request = context.subject
    val channel = request.value as? ByteReadChannel ?: return null
    val inputStream = channel.toInputStream()
    val reader = inputStream.reader(context.call.request.contentCharset() ?: Charsets.UTF_8)
    val type = request.type
    return convertForReceive(type.javaObjectType, reader::readText)
  }
}
Does anyone know why the
.toInputStream()
throws this exception?
java.lang.IllegalStateException: Using blocking primitives on this dispatcher is not allowed. Consider using async channel instead or use blocking primitives in withContext(<http://Dispatchers.IO|Dispatchers.IO>) instead.
io.ktor.utils.io.jvm.javaio.BlockingKt.ensureParkingAllowed(Blocking.kt:302)
io.ktor.utils.io.jvm.javaio.BlockingKt.access$ensureParkingAllowed(Blocking.kt:1)
io.ktor.utils.io.jvm.javaio.InputAdapter.<init>(Blocking.kt:30)
io.ktor.utils.io.jvm.javaio.BlockingKt.toInputStream(Blocking.kt:20)
io.ktor.utils.io.jvm.javaio.BlockingKt.toInputStream$default(Blocking.kt:20)
com.apurebase.concur.server.XmlConverter.convertForReceive(XmlConverter.kt:28)
This happened after I upgraded from kotlin 1.3 and ktor 1.1.3 to the latest versions.
r

Rustam Siniukov

06/07/2021, 5:47 PM
As it says in the exception, blocking on this dispatcher is not allowed. If you do it, some engines may hang and drop in performance. Why do you need to convert content to
InputStream
in the first place? You can do
val text = channel.readRemaining().readText(charset)
. This is a no-blocking call
👍 1
j

jeggy

06/07/2021, 8:49 PM
Thanks 🙂 This was exactly the solution I was looking for