Skip to content

Commit fede7bd

Browse files
committed
Cancel requests on invalidated session
1 parent cd1458b commit fede7bd

5 files changed

+24
-7
lines changed

Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ let package = Package(
1111
targets: [
1212
.binaryTarget(
1313
name: "WordPressKit",
14-
url: "https://github.com/user-attachments/files/18570063/WordPressKit.zip",
15-
checksum: "fc25d3065e80af713dac970db7ed89ff37e4cc98afc98b6a2ecf7b47b2ddd0c1"
14+
url: "https://github.com/user-attachments/files/18646293/WordPressKit.zip",
15+
checksum: "eb801860f5ce489390dd5a50fbd05cb3942dec8350b3665bc51c036fd669432f"
1616
),
1717
]
1818
)

Sources/CoreAPI/WordPressAPIError+NSErrorBridge.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extension WordPressAPIError: CustomNSError {
3333
return -100003
3434
case .unknown:
3535
return -100004
36+
case .sessionInvalidated:
37+
return -100005
3638
}
3739
}
3840

@@ -42,8 +44,7 @@ extension WordPressAPIError: CustomNSError {
4244
return (endpointError as NSError).userInfo
4345
case .connection(let error):
4446
return [NSUnderlyingErrorKey: error]
45-
case .requestEncodingFailure, .unacceptableStatusCode, .unparsableResponse,
46-
.unknown:
47+
case .requestEncodingFailure, .unacceptableStatusCode, .unparsableResponse, .sessionInvalidated, .unknown:
4748
return [:]
4849
}
4950
}

Sources/CoreAPI/WordPressAPIError.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import Foundation
1919
case unacceptableStatusCode(response: HTTPURLResponse, body: Data)
2020
/// 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.
2121
case unparsableResponse(response: HTTPURLResponse?, body: Data?, underlyingError: Error)
22+
/// Attempting to start a request on an invalidated session.
23+
case sessionInvalidated
2224
/// Other error occured.
2325
case unknown(underlyingError: Error)
2426

@@ -28,7 +30,7 @@ import Foundation
2830

2931
var response: HTTPURLResponse? {
3032
switch self {
31-
case .requestEncodingFailure, .connection, .unknown:
33+
case .requestEncodingFailure, .connection, .unknown, .sessionInvalidated:
3234
return nil
3335
case let .endpointError(error):
3436
return (error as? HTTPURLResponseProviding)?.httpResponse
@@ -49,7 +51,7 @@ extension WordPressAPIError: LocalizedError {
4951
// always returns a non-nil value.
5052
let localizedErrorMessage: String
5153
switch self {
52-
case .requestEncodingFailure, .unparsableResponse, .unacceptableStatusCode:
54+
case .requestEncodingFailure, .unparsableResponse, .unacceptableStatusCode, .sessionInvalidated:
5355
// These are usually programming errors.
5456
localizedErrorMessage = Self.unknownErrorMessage
5557
case let .endpointError(error):

Sources/CoreAPI/WordPressComRestApi.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ open class WordPressComRestApi: NSObject {
105105

106106
private var useEphemeralSession: Bool
107107

108+
private var isInvalidated = false
109+
108110
/**
109111
Configure whether or not the user's preferred language locale should be appended. Defaults to true.
110112
*/
@@ -174,6 +176,10 @@ open class WordPressComRestApi: NSObject {
174176
Cancels all ongoing taks and makes the session invalid so the object will not fullfil any more request
175177
*/
176178
@objc open func invalidateAndCancelTasks() {
179+
guard !isInvalidated else {
180+
return
181+
}
182+
isInvalidated = true
177183
for session in [urlSession, uploadURLSession] {
178184
session.invalidateAndCancel()
179185
}
@@ -431,7 +437,11 @@ open class WordPressComRestApi: NSObject {
431437
taskCreated: ((Int) -> Void)? = nil,
432438
session: URLSession? = nil
433439
) async -> APIResult<T> {
434-
await (session ?? self.urlSession)
440+
guard !isInvalidated else {
441+
print("Session Invalidated: ", Thread.callStackSymbols)
442+
return APIResult.failure(.sessionInvalidated)
443+
}
444+
return await (session ?? self.urlSession)
435445
.perform(request: request, taskCreated: taskCreated, fulfilling: progress, errorType: WordPressComRestApiEndpointError.self)
436446
.mapSuccess { response -> HTTPAPIResponse<T> in
437447
let object = try decoder(response.body)

Sources/CoreAPI/WordPressOrgXMLRPCApi.swift

+4
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ private extension WordPressAPIError where EndpointError == WordPressOrgXMLRPCApi
424424
error = underlyingError as NSError
425425
data = body
426426
statusCode = nil
427+
case .sessionInvalidated:
428+
error = NSError(domain: WPXMLRPCFaultErrorDomain, code: -1)
429+
data = nil
430+
statusCode = nil
427431
case let .unknown(underlyingError):
428432
error = underlyingError as NSError
429433
data = nil

0 commit comments

Comments
 (0)