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: Only call release on managedSelf once #2943

Merged
merged 1 commit into from
Apr 10, 2023
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
9 changes: 7 additions & 2 deletions Sources/Apollo/InterceptorRequestChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ final public class InterceptorRequestChain: Cancellable, RequestChain {
@Atomic public var isCancelled: Bool = false

private var managedSelf: Unmanaged<InterceptorRequestChain>!
// This is to fix #2932, caused by overreleasing managedSelf on cancellation of a query or
// mutation. It can now only be released once.
private lazy var releaseManagedSelf: Void = {
self.managedSelf.release()
}()

/// Something which allows additional error handling to occur when some kind of error has happened.
public var additionalErrorHandler: ApolloErrorInterceptor?
Expand Down Expand Up @@ -136,7 +141,7 @@ final public class InterceptorRequestChain: Cancellable, RequestChain {
)

if Operation.operationType != .subscription {
self.managedSelf.release()
_ = self.releaseManagedSelf
}
} else {
// We got to the end of the chain and no parsed response is there, there needs to be more
Expand Down Expand Up @@ -166,7 +171,7 @@ final public class InterceptorRequestChain: Cancellable, RequestChain {
interceptor.cancel()
}

self.managedSelf.release()
_ = self.releaseManagedSelf
}

/// Restarts the request starting from the first interceptor.
Expand Down
41 changes: 41 additions & 0 deletions Tests/ApolloTests/RequestChainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -482,4 +482,45 @@ class RequestChainTests: XCTestCase {
requestChain = nil
XCTAssertNil(weakRequestChain)
}

func test__managedSelf__givenQuery_whenCancelled_shouldNotCrash() throws {
// given
let client = MockURLSessionClient(
response: .mock(
url: TestURL.mockServer.url,
statusCode: 200,
httpVersion: nil,
headerFields: nil
),
data: """
{
"data": {
"__typename": "Hero",
"name": "R2-D2"
}
}
""".data(using: .utf8)
)

let provider = MockInterceptorProvider([
NetworkFetchInterceptor(client: client),
JSONResponseParsingInterceptor()
])
let transport = RequestChainNetworkTransport(
interceptorProvider: provider,
endpointURL: TestURL.mockServer.url
)

let expectation = expectation(description: "Response received")

let cancellable = transport.send(operation: MockQuery.mock()) { result in
expectation.fulfill()
}

wait(for: [expectation], timeout: 1)

DispatchQueue.main.async {
cancellable.cancel()
}
}
}