Skip to content

Commit 8d6ccc9

Browse files
authored
fix(rt): second fix for okhttp engine crashing on Android when coroutine is cancelled uploading request body (#728)
1 parent 01c872a commit 8d6ccc9

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "3146b641-ffcb-4043-9fdf-1f8b3b324113",
3+
"type": "bugfix",
4+
"description": "Fix OkHttp engine crashing on Android when coroutine is cancelled while uploading request body",
5+
"issues": [
6+
"awslabs/aws-sdk-kotlin#733"
7+
]
8+
}

runtime/protocol/http-client-engines/http-client-engine-okhttp/jvm/src/aws/smithy/kotlin/runtime/http/engine/okhttp/ByteChannelRequestBody.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ internal class ByteChannelRequestBody(
2929
override fun isOneShot(): Boolean = !body.isReplayable
3030
override fun isDuplex(): Boolean = body.isDuplex
3131

32+
override fun writeTo(sink: BufferedSink) = try {
33+
doWriteTo(sink)
34+
} catch (ex: Exception) {
35+
throw when (ex) {
36+
is IOException -> ex
37+
// wrap all exceptions thrown from inside `okhttp3.RequestBody#writeTo(..)` as an IOException
38+
// see https://github.com/awslabs/aws-sdk-kotlin/issues/733
39+
else -> IOException(ex)
40+
}
41+
}
42+
3243
@OptIn(ExperimentalStdlibApi::class)
33-
override fun writeTo(sink: BufferedSink) {
44+
private fun doWriteTo(sink: BufferedSink) {
3445
if (isDuplex()) {
3546
// launch coroutine that writes to sink in the background
3647
launch {
@@ -77,9 +88,7 @@ private inline fun <T> withJob(job: CompletableJob, block: () -> T): T {
7788
return block()
7889
} catch (ex: Exception) {
7990
job.completeExceptionally(ex)
80-
// wrap all exceptions thrown from inside `okhttp3.RequestBody#writeTo(..)` as an IOException
81-
// see https://github.com/awslabs/aws-sdk-kotlin/issues/733
82-
throw IOException(ex)
91+
throw ex
8392
} finally {
8493
job.complete()
8594
}

0 commit comments

Comments
 (0)