Skip to content

Commit

Permalink
Handle source close while reading on Pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolashaan committed Jan 10, 2025
1 parent d784119 commit 4e009a0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions okio/src/jvmMain/kotlin/okio/Pipe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class Pipe(internal val maxBufferSize: Long) {
if (sinkClosed) return -1L
timeout.awaitSignal(condition) // Wait until the sink fills the buffer.
if (canceled) throw IOException("canceled")
if (sourceClosed) throw IOException("closed")
}

val result = buffer.read(sink, byteCount)
Expand Down
19 changes: 19 additions & 0 deletions okio/src/jvmTest/kotlin/okio/PipeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package okio
import java.io.IOException
import java.io.InterruptedIOException
import java.util.Random
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import okio.ByteString.Companion.decodeHex
import okio.HashingSink.Companion.sha1
Expand Down Expand Up @@ -280,6 +281,24 @@ class PipeTest {
pipe.source.close()
}

@Test
fun sourceCloseWhileReadingThrows() {
val pipe = Pipe(100L)
val latch = CountDownLatch(1)
executorService.execute {
try {
pipe.source.read(Buffer(), 1)
fail()
} catch (expected: IOException) {
assertEquals("closed", expected.message)
latch.countDown()
}
}
Thread.sleep(1000)
pipe.source.close()
latch.await()
}

@Test
fun sourceReadUnblockedByClosedSink() {
val pipe = Pipe(3L)
Expand Down

0 comments on commit 4e009a0

Please sign in to comment.