shikhar
06/02/2026, 1:32 PM@OptIn(InternalAPI::class)
public suspend fun ByteWriteChannel.writeFully(value: ByteArray, startIndex: Int = 0, endIndex: Int = value.size) {
writeBuffer.write(value, startIndex, endIndex)
flushIfNeeded()
}
@InternalAPI
public suspend fun ByteWriteChannel.flushIfNeeded() {
rethrowCloseCauseIfNeeded()
if (autoFlush || writeBuffer.size >= CHANNEL_MAX_SIZE) flush()
}
with CHANNEL_MAX_SIZE = 1MB
Also the autoFlush variable is being ignored in GlobalScope.writer function even though true is passed.
Honouring it would have made onUpload progress smoother at the cost of extra flush calls. This seems like a bug.
So root cause seems to be that we are actually flushing to socket at 1MB steps which causes the pause. Any progress b/w MBs is synthetic. Flush optimisation is fine from a perf standpoint, reduces syscalls/copies
But the UX effect is user sees abrupt rapid progress then halt at MB boundaries.
Is there any workaround for this? to get a smoother upload progress (other than hacking my own)Aleksei Tirman [JB]
06/03/2026, 9:32 AMonUpload firing once per buffer chunk that Ktor hands to the OkHttp client (~1 MB at a time). You can animate the progress bar to make it smoother.Bruce Hamilton
06/03/2026, 9:36 AM