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

fix: Close CRT HttpStream on call completion #1134

Merged
merged 6 commits into from
Aug 2, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/1ab4f8b7-5721-4024-a4dc-d0c595495bd2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "1ab4f8b7-5721-4024-a4dc-d0c595495bd2",
"type": "bugfix",
"description": "Close HttpStreams in CrtHttpEngine after call completion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public class CrtHttpEngine(public override val config: CrtHttpEngineConfig) : Ht
}
}

callContext.job.invokeOnCompletion {
stream.close()
}

if (request.isChunked) {
withContext(SdkDispatchers.IO) {
stream.sendChunkedBody(request.body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ internal class SdkStreamResponseHandler(
}

// short circuit, stop buffering data and discard remaining incoming bytes
if (isCancelled) return bodyBytesIn.len
if (isCancelled) {
crtStream?.close()
stream.close()
return bodyBytesIn.len
}

val buffer = SdkBuffer().apply {
val bytes = bodyBytesIn.readAll()
Expand All @@ -166,9 +170,11 @@ internal class SdkStreamResponseHandler(
// stream is only valid until the end of this callback, ensure any further data being read downstream
// doesn't call incrementWindow on a resource that has been free'd
lock.withLock {
crtStream?.close()
crtStream = null
streamCompleted = true
}
stream.close()

// close the body channel
if (errorCode != 0) {
Expand Down
Loading