Skip to content

Commit

Permalink
KTOR-6147 Darwin: App hangs when streaming request without connection (
Browse files Browse the repository at this point in the history
…#3720)

(cherry picked from commit c75fce9)
  • Loading branch information
rsinukov authored and e5l committed Aug 30, 2023
1 parent 3c7e8c0 commit e637c4a
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import io.ktor.client.engine.*
import io.ktor.http.content.*
import io.ktor.util.*
import io.ktor.utils.io.*
import io.ktor.utils.io.core.*
import io.ktor.utils.io.errors.*
import kotlinx.cinterop.*
import kotlinx.cinterop.ByteVar
import kotlinx.coroutines.*
import platform.Foundation.*
import platform.darwin.*
import platform.posix.*

@OptIn(DelicateCoroutinesApi::class, UnsafeNumber::class, InternalAPI::class)
Expand Down Expand Up @@ -53,10 +50,19 @@ internal suspend fun OutgoingContent.toDataOrStream(): Any? {
var offset = 0
val read = channel.readAvailable(buffer, 0, 4096)
while (offset < read) {
while (!outputStream.hasSpaceAvailable) {
yield()
}
@Suppress("UNCHECKED_CAST")
val written = outputStream
.write(buffer.plus(offset) as CPointer<UByteVar>, (read - offset).convert()).convert<Int>()
.write(buffer.plus(offset) as CPointer<UByteVar>, (read - offset).convert())
.convert<Int>()
offset += written
if (written < 0) {
throw outputStream.streamError?.let { DarwinHttpRequestException(it) }
?: inputStream.streamError?.let { DarwinHttpRequestException(it) }
?: IOException("Failed to write to the network")
}
}
}
}
Expand Down

0 comments on commit e637c4a

Please sign in to comment.