Skip to content

Should runInterruptible throw InterruptedIOException as CancellationException? #3551

Closed as not planned
@ursusursus

Description

@ursusursus

When used with retrofit/okhttp/okio stack, reading and writing bytes becomes

val source = api.download("..").source()
source.use { it.readAll(sink) }

However the read/writes become blocking and non cancellable. Fortunatelly okio checks for java thread interrupts, so runInterruptible can be used

val source = api.download("..").source()
runInterruptible { 
   source.use { it.readAll(sink) } 
}

However, okio throws java.io.InterruptedIOException, which the runInterruptible doesn't then repackage as CancellationException

Should be repackage this exception as well?

private fun <T> runInterruptibleInExpectedContext(coroutineContext: CoroutineContext, block: () -> T): T {
    try {
        val threadState = ThreadState(coroutineContext.job)
        threadState.setup()
        try {
            return block()
        } finally {
            threadState.clearInterrupt()
        }
    } catch (e: InterruptedException) { <---------------------- 
        throw CancellationException("Blocking call was interrupted due to parent cancellation").initCause(e)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions