Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTOR-5372 Fix EOF in ByteBufferChannel when min is 0 #3327

Merged
merged 1 commit into from Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt
Expand Up @@ -1655,7 +1655,7 @@ internal open class ByteBufferChannel(
}

if (!read) {
if (isClosedForRead) {
if (isClosedForRead && min > 0) {
throw EOFException("Got EOF but at least $min bytes were expected")
}

Expand Down
5 changes: 3 additions & 2 deletions ktor-io/jvm/src/io/ktor/utils/io/jvm/nio/Writing.kt
Expand Up @@ -5,8 +5,9 @@ import java.nio.*
import java.nio.channels.*

/**
* Copy up to [limit] bytes to blocking NIO [channel]. Copying to non-blocking channel requires selection and
* not supported. It does suspend if no data available in byte channel but may block if destination NIO channel blocks.
* Copy up to [limit] bytes to blocking NIO [channel].
* Copying to a non-blocking channel requires selection and not supported.
* It is suspended if no data are available in a byte channel but may block if destination NIO channel blocks.
*
* @return number of bytes copied
*/
Expand Down
15 changes: 15 additions & 0 deletions ktor-io/jvm/test/io/ktor/utils/io/ByteBufferChannelTest.kt
Expand Up @@ -253,4 +253,19 @@ class ByteBufferChannelTest {
reader.await()
writer.join()
}

@Test
fun testReadWithNoMinDoesntThrow() = runBlocking {
val channel = ByteChannel(true)

channel.writeByte(1)
channel.read(0) {
assertEquals(1, it.remaining())
it.get()
}
channel.close()
channel.read(0) {
assertEquals(0, it.remaining())
}
}
}