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

Extend lifetime of client interceptor pipeline #1265

Merged
merged 6 commits into from
Sep 16, 2021
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
40 changes: 27 additions & 13 deletions Sources/GRPC/Interceptor/ClientInterceptorPipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,17 @@ internal final class ClientInterceptorPipeline<Request, Response> {
internal let _errorDelegate: ClientErrorDelegate?

@usableFromInline
internal let _onError: (Error) -> Void
internal private(set) var _onError: ((Error) -> Void)?

@usableFromInline
internal let _onCancel: (EventLoopPromise<Void>?) -> Void
internal private(set) var _onCancel: ((EventLoopPromise<Void>?) -> Void)?

@usableFromInline
internal let _onRequestPart: (GRPCClientRequestPart<Request>, EventLoopPromise<Void>?) -> Void
internal private(set) var _onRequestPart:
((GRPCClientRequestPart<Request>, EventLoopPromise<Void>?) -> Void)?

@usableFromInline
internal let _onResponsePart: (GRPCClientResponsePart<Response>) -> Void
internal private(set) var _onResponsePart: ((GRPCClientResponsePart<Response>) -> Void)?

/// The index after the last user interceptor context index. (i.e. `_userContexts.endIndex`).
@usableFromInline
Expand Down Expand Up @@ -217,9 +218,13 @@ internal final class ClientInterceptorPipeline<Request, Response> {

case self._tailIndex:
if part.isEnd {
// Update our state before handling the response part.
self._isOpen = false
self._onResponsePart?(part)
self.close()
} else {
self._onResponsePart?(part)
}
self._onResponsePart(part)

default:
self._userContexts[index].invokeReceive(part)
Expand Down Expand Up @@ -275,9 +280,8 @@ internal final class ClientInterceptorPipeline<Request, Response> {
/// Handles a caught error which has traversed the interceptor pipeline.
@usableFromInline
internal func _errorCaught(_ error: Error) {
// We're about to complete, close the pipeline.
self.close()

// We're about to call out to an error handler: update our state first.
self._isOpen = false
var unwrappedError: Error

// Unwrap the error, if possible.
Expand All @@ -295,7 +299,10 @@ internal final class ClientInterceptorPipeline<Request, Response> {
}

// Emit the unwrapped error.
self._onError(unwrappedError)
self._onError?(unwrappedError)

// Close the pipeline.
self.close()
glbrntt marked this conversation as resolved.
Show resolved Hide resolved
}

/// Writes a request message into the interceptor pipeline.
Expand Down Expand Up @@ -351,7 +358,7 @@ internal final class ClientInterceptorPipeline<Request, Response> {
) {
switch index {
case self._headIndex:
self._onRequestPart(part, promise)
self._onRequestPart?(part, promise)

case self._tailIndex:
self._invokeSend(
Expand Down Expand Up @@ -407,7 +414,7 @@ internal final class ClientInterceptorPipeline<Request, Response> {
) {
switch index {
case self._headIndex:
self._onCancel(promise)
self._onCancel?(promise)

case self._tailIndex:
self._invokeCancel(
Expand All @@ -425,7 +432,7 @@ internal final class ClientInterceptorPipeline<Request, Response> {

extension ClientInterceptorPipeline {
/// Closes the pipeline. This should be called once, by the tail interceptor, to indicate that
/// the RPC has completed.
/// the RPC has completed. If this is not called, we will leak.
/// - Important: This *must* to be called from the `eventLoop`.
@inlinable
internal func close() {
Expand All @@ -437,7 +444,14 @@ extension ClientInterceptorPipeline {
self._scheduledClose = nil

// Cancel the transport.
self._onCancel(nil)
self._onCancel?(nil)
glbrntt marked this conversation as resolved.
Show resolved Hide resolved

// `ClientTransport` holds a reference to us and references to itself via these callbacks. Break
// these references now by replacing the callbacks.
self._onError = nil
self._onCancel = nil
self._onRequestPart = nil
self._onResponsePart = nil
}

/// Sets up a deadline for the pipeline.
Expand Down
10 changes: 5 additions & 5 deletions Sources/GRPC/Interceptor/ClientTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ internal final class ClientTransport<Request, Response> {
// trailers here and only forward them when we receive the status.
private var trailers: HPACKHeaders?

/// The interceptor pipeline connected to this transport. This must be set to `nil` when removed
/// from the `ChannelPipeline` in order to break reference cycles.
/// The interceptor pipeline connected to this transport. The pipeline also holds references
/// to `self` which are dropped when the interceptor pipeline is closed.
@usableFromInline
internal var _pipeline: ClientInterceptorPipeline<Request, Response>?

Expand Down Expand Up @@ -118,6 +118,7 @@ internal final class ClientTransport<Request, Response> {
self.logger = logger
self.serializer = serializer
self.deserializer = deserializer
// The references to self held by the pipeline are dropped when it is closed.
self._pipeline = ClientInterceptorPipeline(
eventLoop: eventLoop,
details: details,
Expand Down Expand Up @@ -241,7 +242,8 @@ extension ClientTransport {

if self.state.cancel() {
let error = GRPCError.RPCCancelledByClient()
self.forwardErrorToInterceptors(error)
let status = error.makeGRPCStatus()
self.forwardToInterceptors(.end(status, [:]))
self.failBufferedWrites(with: error)
self.channel?.close(mode: .all, promise: nil)
self.channelPromise?.fail(error)
Expand Down Expand Up @@ -363,11 +365,9 @@ extension ClientTransport {
private func dropReferences() {
if self.callEventLoop.inEventLoop {
self.channel = nil
self._pipeline = nil
} else {
self.callEventLoop.execute {
self.channel = nil
self._pipeline = nil
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions Tests/GRPCTests/ClientCallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ class ClientCallTests: GRPCTestCase {
// Cancellation should succeed.
assertThat(try get.cancel().wait(), .doesNotThrow())

// The status promise will fail.
assertThat(
try promise.futureResult.wait(),
.throws(.instanceOf(GRPCError.RPCCancelledByClient.self))
)
assertThat(try promise.futureResult.wait(), .hasCode(.cancelled))

// Cancellation should now fail, we've already cancelled.
assertThat(try get.cancel().wait(), .throws(.instanceOf(GRPCError.AlreadyComplete.self)))
Expand Down
4 changes: 2 additions & 2 deletions Tests/GRPCTests/ClientCancellingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ClientCancellingTests: EchoTestCaseBase {
call.cancel(promise: nil)

call.response.whenFailure { error in
XCTAssertTrue(error is GRPCError.RPCCancelledByClient)
XCTAssertEqual((error as? GRPCStatus)?.code, .cancelled)
responseReceived.fulfill()
}

Expand All @@ -47,7 +47,7 @@ class ClientCancellingTests: EchoTestCaseBase {
call.cancel(promise: nil)

call.response.whenFailure { error in
XCTAssertTrue(error is GRPCError.RPCCancelledByClient)
XCTAssertEqual((error as? GRPCStatus)?.code, .cancelled)
responseReceived.fulfill()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2021, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import EchoModel
import GRPC

// MARK: - Client

internal final class EchoClientInterceptors: Echo_EchoClientInterceptorFactoryProtocol {
internal typealias Factory = () -> ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>
private var factories: [Factory] = []

internal init(_ factories: Factory...) {
self.factories = factories
}

internal func register(_ factory: @escaping Factory) {
self.factories.append(factory)
}

private func makeInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
return self.factories.map { $0() }
}

func makeGetInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
return self.makeInterceptors()
}

func makeExpandInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
return self.makeInterceptors()
}

func makeCollectInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
return self.makeInterceptors()
}

func makeUpdateInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
return self.makeInterceptors()
}
}

// MARK: - Server

internal final class EchoServerInterceptors: Echo_EchoServerInterceptorFactoryProtocol {
internal typealias Factory = () -> ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>
private var factories: [Factory] = []

internal init(_ factories: Factory...) {
self.factories = factories
}

internal func register(_ factory: @escaping Factory) {
self.factories.append(factory)
}

private func makeInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
return self.factories.map { $0() }
}

func makeGetInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
return self.makeInterceptors()
}

func makeExpandInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
return self.makeInterceptors()
}

func makeCollectInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
return self.makeInterceptors()
}

func makeUpdateInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
return self.makeInterceptors()
}
}
Loading