From fede7bd059df98110aeaa30c3111d256656a0089 Mon Sep 17 00:00:00 2001 From: kean Date: Mon, 3 Feb 2025 12:20:40 -0500 Subject: [PATCH 1/2] Cancel requests on invalidated session --- Package.swift | 4 ++-- .../CoreAPI/WordPressAPIError+NSErrorBridge.swift | 5 +++-- Sources/CoreAPI/WordPressAPIError.swift | 6 ++++-- Sources/CoreAPI/WordPressComRestApi.swift | 12 +++++++++++- Sources/CoreAPI/WordPressOrgXMLRPCApi.swift | 4 ++++ 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Package.swift b/Package.swift index b7b5df79..6a5d068d 100644 --- a/Package.swift +++ b/Package.swift @@ -11,8 +11,8 @@ let package = Package( targets: [ .binaryTarget( name: "WordPressKit", - url: "https://github.com/user-attachments/files/18570063/WordPressKit.zip", - checksum: "fc25d3065e80af713dac970db7ed89ff37e4cc98afc98b6a2ecf7b47b2ddd0c1" + url: "https://github.com/user-attachments/files/18646293/WordPressKit.zip", + checksum: "eb801860f5ce489390dd5a50fbd05cb3942dec8350b3665bc51c036fd669432f" ), ] ) diff --git a/Sources/CoreAPI/WordPressAPIError+NSErrorBridge.swift b/Sources/CoreAPI/WordPressAPIError+NSErrorBridge.swift index 594c3e3e..85119b71 100644 --- a/Sources/CoreAPI/WordPressAPIError+NSErrorBridge.swift +++ b/Sources/CoreAPI/WordPressAPIError+NSErrorBridge.swift @@ -33,6 +33,8 @@ extension WordPressAPIError: CustomNSError { return -100003 case .unknown: return -100004 + case .sessionInvalidated: + return -100005 } } @@ -42,8 +44,7 @@ extension WordPressAPIError: CustomNSError { return (endpointError as NSError).userInfo case .connection(let error): return [NSUnderlyingErrorKey: error] - case .requestEncodingFailure, .unacceptableStatusCode, .unparsableResponse, - .unknown: + case .requestEncodingFailure, .unacceptableStatusCode, .unparsableResponse, .sessionInvalidated, .unknown: return [:] } } diff --git a/Sources/CoreAPI/WordPressAPIError.swift b/Sources/CoreAPI/WordPressAPIError.swift index cd8493f0..9f832482 100644 --- a/Sources/CoreAPI/WordPressAPIError.swift +++ b/Sources/CoreAPI/WordPressAPIError.swift @@ -19,6 +19,8 @@ import Foundation case unacceptableStatusCode(response: HTTPURLResponse, body: Data) /// The API call returned an HTTP response that WordPressKit can't parse. Receiving this error could be an indicator that there is an error response that's not handled properly by WordPressKit. case unparsableResponse(response: HTTPURLResponse?, body: Data?, underlyingError: Error) + /// Attempting to start a request on an invalidated session. + case sessionInvalidated /// Other error occured. case unknown(underlyingError: Error) @@ -28,7 +30,7 @@ import Foundation var response: HTTPURLResponse? { switch self { - case .requestEncodingFailure, .connection, .unknown: + case .requestEncodingFailure, .connection, .unknown, .sessionInvalidated: return nil case let .endpointError(error): return (error as? HTTPURLResponseProviding)?.httpResponse @@ -49,7 +51,7 @@ extension WordPressAPIError: LocalizedError { // always returns a non-nil value. let localizedErrorMessage: String switch self { - case .requestEncodingFailure, .unparsableResponse, .unacceptableStatusCode: + case .requestEncodingFailure, .unparsableResponse, .unacceptableStatusCode, .sessionInvalidated: // These are usually programming errors. localizedErrorMessage = Self.unknownErrorMessage case let .endpointError(error): diff --git a/Sources/CoreAPI/WordPressComRestApi.swift b/Sources/CoreAPI/WordPressComRestApi.swift index 17b476ef..d9a84dd9 100644 --- a/Sources/CoreAPI/WordPressComRestApi.swift +++ b/Sources/CoreAPI/WordPressComRestApi.swift @@ -105,6 +105,8 @@ open class WordPressComRestApi: NSObject { private var useEphemeralSession: Bool + private var isInvalidated = false + /** Configure whether or not the user's preferred language locale should be appended. Defaults to true. */ @@ -174,6 +176,10 @@ open class WordPressComRestApi: NSObject { Cancels all ongoing taks and makes the session invalid so the object will not fullfil any more request */ @objc open func invalidateAndCancelTasks() { + guard !isInvalidated else { + return + } + isInvalidated = true for session in [urlSession, uploadURLSession] { session.invalidateAndCancel() } @@ -431,7 +437,11 @@ open class WordPressComRestApi: NSObject { taskCreated: ((Int) -> Void)? = nil, session: URLSession? = nil ) async -> APIResult { - await (session ?? self.urlSession) + guard !isInvalidated else { + print("Session Invalidated: ", Thread.callStackSymbols) + return APIResult.failure(.sessionInvalidated) + } + return await (session ?? self.urlSession) .perform(request: request, taskCreated: taskCreated, fulfilling: progress, errorType: WordPressComRestApiEndpointError.self) .mapSuccess { response -> HTTPAPIResponse in let object = try decoder(response.body) diff --git a/Sources/CoreAPI/WordPressOrgXMLRPCApi.swift b/Sources/CoreAPI/WordPressOrgXMLRPCApi.swift index a997efd1..bec7551b 100644 --- a/Sources/CoreAPI/WordPressOrgXMLRPCApi.swift +++ b/Sources/CoreAPI/WordPressOrgXMLRPCApi.swift @@ -424,6 +424,10 @@ private extension WordPressAPIError where EndpointError == WordPressOrgXMLRPCApi error = underlyingError as NSError data = body statusCode = nil + case .sessionInvalidated: + error = NSError(domain: WPXMLRPCFaultErrorDomain, code: -1) + data = nil + statusCode = nil case let .unknown(underlyingError): error = underlyingError as NSError data = nil From efdd95aefd9fe8b94b05ecd38a2974bd471b22e1 Mon Sep 17 00:00:00 2001 From: kean Date: Mon, 3 Feb 2025 14:29:45 -0500 Subject: [PATCH 2/2] Add more logs --- Package.swift | 4 ++-- Sources/CoreAPI/WordPressComRestApi.swift | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index 6a5d068d..913331a2 100644 --- a/Package.swift +++ b/Package.swift @@ -11,8 +11,8 @@ let package = Package( targets: [ .binaryTarget( name: "WordPressKit", - url: "https://github.com/user-attachments/files/18646293/WordPressKit.zip", - checksum: "eb801860f5ce489390dd5a50fbd05cb3942dec8350b3665bc51c036fd669432f" + url: "https://github.com/user-attachments/files/18647254/WordPressKit.zip", + checksum: "6df9cf41df249237fd03eb09a4dd170b8ce80c4606ad839b2a71b44c5e8495fe" ), ] ) diff --git a/Sources/CoreAPI/WordPressComRestApi.swift b/Sources/CoreAPI/WordPressComRestApi.swift index d9a84dd9..ad506c08 100644 --- a/Sources/CoreAPI/WordPressComRestApi.swift +++ b/Sources/CoreAPI/WordPressComRestApi.swift @@ -208,6 +208,7 @@ open class WordPressComRestApi: NSObject { success: @escaping SuccessResponseBlock, failure: @escaping FailureReponseBlock) -> Progress? { let progress = Progress.discreteProgress(totalUnitCount: 100) + print("GET:", Thread.callStackSymbols) Task { @MainActor in let result = await self.perform(.get, URLString: URLString, parameters: parameters, fulfilling: progress) @@ -253,6 +254,8 @@ open class WordPressComRestApi: NSObject { parameters: [String: AnyObject]?, success: @escaping SuccessResponseBlock, failure: @escaping FailureReponseBlock) -> Progress? { + print("POST:", Thread.callStackSymbols) + let progress = Progress.discreteProgress(totalUnitCount: 100) Task { @MainActor in