I'm trying to use a local copy of <https://github....
# ktor
r
I'm trying to use a local copy of https://github.com/ktorio/ktor/blob/master/ktor-server/ktor-server-core/jvm/src/io/ktor/http/content/OutputStreamContent.kt in ktor CIO client code. However, my client often freezes when writing to the
OutputStream
, with the stack:
Copy code
"DefaultDispatcher-worker-5@10353" daemon prio=5 tid=0x69 nid=NA waiting
  java.lang.Thread.State: WAITING
	  at sun.misc.Unsafe.park(Unsafe.java:-1)
	  at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:338)
	  at kotlinx.coroutines.io.jvm.javaio.BlockingAdapter.parkingLoop(Blocking.kt:235)
	  at kotlinx.coroutines.io.jvm.javaio.BlockingAdapter.submitAndAwait(Blocking.kt:215)
	  at kotlinx.coroutines.io.jvm.javaio.BlockingAdapter.submitAndAwait(Blocking.kt:186)
	  at kotlinx.coroutines.io.jvm.javaio.OutputAdapter.write(Blocking.kt:108)
	  - locked <merged>(a kotlinx.coroutines.io.jvm.javaio.OutputAdapter)
	  at java.io.OutputStream.write(OutputStream.java:75)
	  at [...code that writes to OutputStream...]
	  at io.ktor.client.engine.cio.UtilsKt.write(utils.kt:55)
	  at io.ktor.client.engine.cio.Endpoint$makeDedicatedRequest$1.invokeSuspend(Endpoint.kt:108)
          [...]
My call to
OutputStream
for testing is simply
write(bytes)
where bytes is a
ByteArray
.
Hmm, ok if I change the source of my copied
OutputStreamContent
to wrap the call in an IO dispatcher, then things work:
Copy code
override suspend fun writeTo(channel: ByteWriteChannel) {
    withContext(IO) {
      channel.toOutputStream().use { it.body() }
    }
  }