square / okio

A modern I/O library for Android, Java, and Kotlin Multiplatform.

Home Page:https://square.github.io/okio/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Aborting read on Pipe

nicolashaan opened this issue · comments

Hi all,
The following source is blocking indefinitely on the readByte() line.
I expected the close() call to unlock the read call by throwing an IOException.

Is this the intended behavior?
Is there a way to unlock a blocking read call?

fun `Pipe playground`() {
    val pipe = Pipe(1024)

    val bufferedSource = pipe.source.buffer()

    thread {
        Thread.sleep(1000)
        println("Closing source...")
        bufferedSource.close()
        println("Source closed")
    }

    println("Before read()")
    val out = bufferedSource.readByte()
    println("After read()")
}

output:

Before read()
Closing source...
Source closed

Thanks!

No Okio streams are safe for concurrent use, including calls to close() that coincide with other operations.

The function you want is Pipe.cancel(). It had the side-effects you’re looking for.

https://square.github.io/okio/3.x/okio/okio/okio/-pipe/cancel.html