-
Notifications
You must be signed in to change notification settings - Fork 28
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
fix: correctly handle async cancellation of call context in OkHttp engine #1063
Changes from 4 commits
9393022
372a467
b4bf0ea
0b5d381
a7b39c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"id": "289fe06b-6d65-47da-a007-360398c39244", | ||
"type": "bugfix", | ||
"description": "Correctly handle async cancellation of call context in OkHttp engine", | ||
"issues": [ | ||
"awslabs/smithy-kotlin#1061" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,15 +50,16 @@ public class OkHttpEngine( | |
val engineCall = client.newCall(engineRequest) | ||
val engineResponse = mapOkHttpExceptions { engineCall.executeAsync() } | ||
|
||
callContext.job.invokeOnCompletion { | ||
engineResponse.body.close() | ||
} | ||
|
||
val response = engineResponse.toSdkResponse() | ||
val requestTime = Instant.fromEpochMilliseconds(engineResponse.sentRequestAtMillis) | ||
val responseTime = Instant.fromEpochMilliseconds(engineResponse.receivedResponseAtMillis) | ||
|
||
return OkHttpCall(request, response, requestTime, responseTime, callContext, engineCall) | ||
return OkHttpCall(request, response, requestTime, responseTime, callContext, engineCall).also { call -> | ||
callContext.job.invokeOnCompletion { error -> | ||
if (error != null) call.cancelInFlight() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correctness: you can receive a non-null There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes we definitely want to cancel when we get a |
||
engineResponse.body.close() | ||
} | ||
} | ||
} | ||
|
||
override fun shutdown() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,4 +87,45 @@ class AsyncStressTest : AbstractEngineTest() { | |
assertEquals(engineJobsBefore.size, engineJobsAfter.size, message) | ||
} | ||
} | ||
|
||
@Test | ||
fun testJobCancellation() = testEngines { | ||
// https://github.com/smithy-lang/smithy-kotlin/issues/1061 | ||
|
||
test { _, client -> | ||
val req = HttpRequest { | ||
testSetup() | ||
url.path.decoded = "slow" | ||
} | ||
|
||
// Expect CancellationException because we're cancelling | ||
assertFailsWith<CancellationException> { | ||
coroutineScope { | ||
val parentScope = this | ||
|
||
println("Invoking call on ctx $coroutineContext") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove |
||
val call = client.call(req) | ||
|
||
val bytes = async { | ||
delay(100.milliseconds) | ||
println("Body of type ${call.response.body} on ctx $coroutineContext") | ||
try { | ||
call.response.body.readAll() | ||
} catch (e: Throwable) { | ||
// IllegalStateException: "Unbalanced enter/exit" will be thrown if body closed improperly | ||
assertIsNot<IllegalStateException>(e) | ||
null | ||
} | ||
} | ||
|
||
val cancellation = async { | ||
delay(400.milliseconds) | ||
parentScope.cancel("Cancelling!") | ||
} | ||
|
||
awaitAll(bytes, cancellation) | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit(naming):
error
->cause
to match documentation?