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: stream leak in akka-http client backend for indefinite server st… #1833

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ object AkkaHttpClientUtils {
s"${scheme}://${settings.overrideAuthority.getOrElse(settings.serviceName)}/" + descriptor.getFullMethodName),
GrpcEntityHelpers.metadataHeaders(headers.entries),
source)
responseToSource(httpRequest.uri, singleRequest(httpRequest), deserializer)
responseToSource(httpRequest.uri, singleRequest(httpRequest), deserializer, streamingResponse)
}
}
}
Expand All @@ -182,7 +182,11 @@ object AkkaHttpClientUtils {
* INTERNAL API
*/
@InternalApi
def responseToSource[O](requestUri: Uri, response: Future[HttpResponse], deserializer: ProtobufSerializer[O])(
def responseToSource[O](
requestUri: Uri,
response: Future[HttpResponse],
deserializer: ProtobufSerializer[O],
streamingResponse: Boolean)(
implicit ec: ExecutionContext,
mat: Materializer): Source[O, Future[GrpcResponseMetadata]] = {
Source.lazyFutureSource[O, Future[GrpcResponseMetadata]](() => {
Expand Down Expand Up @@ -221,14 +225,19 @@ object AkkaHttpClientUtils {
response.entity.discardBytes()
throw mapToStatusException(requestUri, response, Seq.empty)
}
responseData
val baseFlow = responseData
// This never adds any data to the stream, but makes sure it fails with the correct error code if applicable
.concat(
Source
.maybe[ByteString]
.mapMaterializedValue(promise => promise.completeWith(completionFuture.map(_ => None))))
val flow = if (streamingResponse) {
baseFlow
} else {
// Make sure we continue reading to get the trailing header even if we're no longer interested in the rest of the body
.via(new CancellationBarrierGraphStage)
baseFlow.via(new CancellationBarrierGraphStage)
}
flow
.via(reader.dataFrameDecoder)
.map(deserializer.deserialize)
.mapMaterializedValue(_ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AkkaHttpClientUtilsSpec extends TestKit(ActorSystem()) with AnyWordSpecLik
val requestUri = Uri("https://example.com/GuestExeSample/GrpcHello")
val response =
Future.successful(HttpResponse(NotFound, entity = Strict(GrpcProtocolNative.contentType, ByteString.empty)))
val source = AkkaHttpClientUtils.responseToSource(requestUri, response, null)
val source = AkkaHttpClientUtils.responseToSource(requestUri, response, null, false)

val failure = source.run().failed.futureValue
// https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
Expand All @@ -43,7 +43,7 @@ class AkkaHttpClientUtilsSpec extends TestKit(ActorSystem()) with AnyWordSpecLik
val requestUri = Uri("https://example.com/GuestExeSample/GrpcHello")
val response = Future.successful(
HttpResponse(OK, List(RawHeader("grpc-status", "9")), Strict(GrpcProtocolNative.contentType, ByteString.empty)))
val source = AkkaHttpClientUtils.responseToSource(requestUri, response, null)
val source = AkkaHttpClientUtils.responseToSource(requestUri, response, null, false)

val failure = source.run().failed.futureValue
failure.asInstanceOf[StatusRuntimeException].getStatus.getCode should be(Status.Code.FAILED_PRECONDITION)
Expand Down