From 8f68878f599b25d4aa53f168966daa4128f056fc Mon Sep 17 00:00:00 2001 From: ec2-user Date: Tue, 17 Nov 2026 20:02:32 +0000 Subject: [PATCH 1/3] fix: retry Apple gateway 502/503/504 during authentication Apple's sign-in servers answer 502/503/504 for a share of valid requests. Read-only calls (service key, olympus config, hashcash challenge) now retry up to 5 times. Sign-in POSTs are never retried. --- .../API/Authentication+Hashcash.swift | 3 +- Sources/xcodeinstall/API/Authentication.swift | 20 +++++++--- Sources/xcodeinstall/API/HTTPClient.swift | 35 ++++++++++++++++-- .../API/HTTPClientTests.swift | 37 +++++++++++++++++++ 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/Sources/xcodeinstall/API/Authentication+Hashcash.swift b/Sources/xcodeinstall/API/Authentication+Hashcash.swift index 99b5d00..0d2fa7a 100644 --- a/Sources/xcodeinstall/API/Authentication+Hashcash.swift +++ b/Sources/xcodeinstall/API/Authentication+Hashcash.swift @@ -55,7 +55,8 @@ extension AppleAuthenticator { "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=\(itServiceKey)" let (_, response) = try await apiCall( url: url, - validResponse: .value(200) + validResponse: .value(200), + retryTransientErrors: true ) guard let hcString = response.allHeaderFields["X-Apple-HC-Bits"] as? String, diff --git a/Sources/xcodeinstall/API/Authentication.swift b/Sources/xcodeinstall/API/Authentication.swift index 55716b3..732c859 100644 --- a/Sources/xcodeinstall/API/Authentication.swift +++ b/Sources/xcodeinstall/API/Authentication.swift @@ -348,11 +348,20 @@ class AppleAuthenticator: HTTPClient, AppleAuthenticatorProtocol { var request = URLRequest(url: URL(string: "https://appstoreconnect.apple.com/logout")!) request.httpMethod = HTTPVerb.GET.rawValue - let (_, response) = try await session.data(for: request) - guard let httpResponse = response as? HTTPURLResponse else { - return nil + // Apple's gateway answers 502 / 503 / 504 for a share of requests; this + // read-only request is safe to repeat. + for attempt in 1...5 { + let (_, response) = try await session.data(for: request) + guard let httpResponse = response as? HTTPURLResponse else { + return nil + } + if [502, 503, 504].contains(httpResponse.statusCode), attempt < 5 { + try await Task.sleep(for: .milliseconds(300 * attempt)) + continue + } + return httpResponse.value(forHTTPHeaderField: "Location") } - return httpResponse.value(forHTTPHeaderField: "Location") + return nil } internal func fetchServiceKeyFromOlympus() async throws -> AppleServiceKey { @@ -369,7 +378,8 @@ class AppleAuthenticator: HTTPClient, AppleAuthenticatorProtocol { "https://appstoreconnect.apple.com/olympus/v1/app/config?hostname=itunesconnect.apple.com" let (data, _) = try await apiCall( url: url, - validResponse: .range(200..<400) //FIXME: should this be .value(200) ? + validResponse: .range(200..<400), //FIXME: should this be .value(200) ? + retryTransientErrors: true ) return try JSONDecoder().decode(AppleServiceKey.self, from: data) diff --git a/Sources/xcodeinstall/API/HTTPClient.swift b/Sources/xcodeinstall/API/HTTPClient.swift index 28e0baa..e336e2b 100644 --- a/Sources/xcodeinstall/API/HTTPClient.swift +++ b/Sources/xcodeinstall/API/HTTPClient.swift @@ -124,7 +124,8 @@ class HTTPClient { method: HTTPVerb = .GET, body: Data? = nil, headers: [String: String] = [:], - validResponse: ExpectedResponseCode = .value(0) + validResponse: ExpectedResponseCode = .value(0), + retryTransientErrors: Bool = false ) async throws -> (Data, HTTPURLResponse) { let request: URLRequest @@ -145,8 +146,14 @@ class HTTPClient { _log(request: request, to: log) - // send request with that session - let (data, response) = try await self.urlSession.data(for: request, delegate: nil) + // send request with that session. + // Apple's sign in servers answer 502 / 503 / 504 for a share of otherwise + // valid requests. Callers whose request is safe to repeat (read-only GETs) + // opt in to a few retries; sign in POSTs never do. + let (data, response) = try await sendRetryingTransientErrors( + request, + attempts: retryTransientErrors ? 5 : 1 + ) guard let httpResponse = response as? HTTPURLResponse, validResponse.isValid(response: httpResponse.statusCode) else { @@ -162,6 +169,28 @@ class HTTPClient { return (data, httpResponse) } + // Send `request`, repeating it up to `attempts` times in total while Apple's + // gateway answers 502, 503 or 504. Any other answer is returned as is. + private func sendRetryingTransientErrors( + _ request: URLRequest, + attempts: Int + ) async throws -> (Data, URLResponse) { + var attempt = 1 + while true { + let (data, response) = try await self.urlSession.data(for: request, delegate: nil) + if let http = response as? HTTPURLResponse, + [502, 503, 504].contains(http.statusCode), + attempt < attempts + { + log.debug("Transient HTTP \(http.statusCode), retrying (attempt \(attempt) of \(attempts))") + try await Task.sleep(for: .milliseconds(300 * attempt)) + attempt += 1 + continue + } + return (data, response) + } + } + // prepare an URLRequest for a given url, method, body, and headers // https://softwareengineering.stackexchange.com/questions/100959/how-do-you-unit-test-private-methods internal func request( diff --git a/Tests/xcodeinstallTests/API/HTTPClientTests.swift b/Tests/xcodeinstallTests/API/HTTPClientTests.swift index 2ea8770..9a26e5a 100644 --- a/Tests/xcodeinstallTests/API/HTTPClientTests.swift +++ b/Tests/xcodeinstallTests/API/HTTPClientTests.swift @@ -121,4 +121,41 @@ extension HTTPClientTests { // Then #expect(self.sessionData.lastURL?.debugDescription == url) } + + // MARK: - Transient gateway errors + + private func response(_ code: Int) -> HTTPURLResponse { + HTTPURLResponse( + url: URL(string: "https://idmsa.apple.com")!, statusCode: code, httpVersion: nil, headerFields: nil)! + } + + @Test("apiCall retries 504 answers when asked to") + func apiCallRetriesTransientErrors() async throws { + sessionData.enqueueResponse(data: Data(), response: response(504)) + sessionData.enqueueResponse(data: Data(), response: response(502)) + sessionData.enqueueResponse(data: Data(), response: response(200)) + + let (_, http) = try await client.apiCall( + url: "https://idmsa.apple.com/appleauth/auth/signin", + validResponse: .value(200), + retryTransientErrors: true + ) + + #expect(http.statusCode == 200) + #expect(sessionData.allRequests.count == 3) + } + + @Test("apiCall does not retry unless asked to") + func apiCallDoesNotRetryByDefault() async throws { + sessionData.enqueueResponse(data: Data(), response: response(504)) + sessionData.enqueueResponse(data: Data(), response: response(200)) + + await #expect(throws: URLError.self) { + _ = try await client.apiCall( + url: "https://idmsa.apple.com/appleauth/auth/signin", + validResponse: .value(200) + ) + } + #expect(sessionData.allRequests.count == 1) + } } From e1c44edb4964630b3bd384414c28ddf568207aa3 Mon Sep 17 00:00:00 2001 From: ec2-user Date: Thu, 19 Nov 2026 10:18:32 +0000 Subject: [PATCH 2/3] refactor: share one retry policy for Apple gateway errors Move the retry loop into TransientRetryPolicy (named attempts, delay and status codes) used by apiCall and the /logout request. The delay is injectable so tests do not sleep. Add a test for the /logout retry. --- Sources/xcodeinstall/API/Authentication.swift | 25 ++++--- Sources/xcodeinstall/API/HTTPClient.swift | 72 ++++++++++++------- .../API/AuthenticationTests.swift | 23 ++++++ .../API/HTTPClientTests.swift | 2 + 4 files changed, 83 insertions(+), 39 deletions(-) diff --git a/Sources/xcodeinstall/API/Authentication.swift b/Sources/xcodeinstall/API/Authentication.swift index 732c859..c00b520 100644 --- a/Sources/xcodeinstall/API/Authentication.swift +++ b/Sources/xcodeinstall/API/Authentication.swift @@ -345,23 +345,22 @@ class AppleAuthenticator: HTTPClient, AppleAuthenticatorProtocol { let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil) defer { session.finishTasksAndInvalidate() } + return try await signoutLocation(using: session) + } + + // Send the GET /logout request on `session` and return the Location header. + // The request is read-only, so it goes through the same retry policy as the + // other authentication calls. Split from `signoutRedirectLocation` so tests + // can drive it with a mocked session. + internal func signoutLocation(using session: URLSessionProtocol) async throws -> String? { + var request = URLRequest(url: URL(string: "https://appstoreconnect.apple.com/logout")!) request.httpMethod = HTTPVerb.GET.rawValue - // Apple's gateway answers 502 / 503 / 504 for a share of requests; this - // read-only request is safe to repeat. - for attempt in 1...5 { - let (_, response) = try await session.data(for: request) - guard let httpResponse = response as? HTTPURLResponse else { - return nil - } - if [502, 503, 504].contains(httpResponse.statusCode), attempt < 5 { - try await Task.sleep(for: .milliseconds(300 * attempt)) - continue - } - return httpResponse.value(forHTTPHeaderField: "Location") + let (_, response) = try await retryPolicy.send(log: log) { + try await session.data(for: request, delegate: nil) } - return nil + return (response as? HTTPURLResponse)?.value(forHTTPHeaderField: "Location") } internal func fetchServiceKeyFromOlympus() async throws -> AppleServiceKey { diff --git a/Sources/xcodeinstall/API/HTTPClient.swift b/Sources/xcodeinstall/API/HTTPClient.swift index e336e2b..552979b 100644 --- a/Sources/xcodeinstall/API/HTTPClient.swift +++ b/Sources/xcodeinstall/API/HTTPClient.swift @@ -69,6 +69,9 @@ class HTTPClient { // some ID returned by Apple API to authenticate us var session = AppleSession() + // how read-only requests are repeated when Apple's gateway fails + var retryPolicy = TransientRetryPolicy() + // to be shared between apiCall and download methods // prepare headers with correct cookies and X- value for Apple authentication func prepareAuthenticationHeaders() async -> [String: String] { @@ -150,10 +153,12 @@ class HTTPClient { // Apple's sign in servers answer 502 / 503 / 504 for a share of otherwise // valid requests. Callers whose request is safe to repeat (read-only GETs) // opt in to a few retries; sign in POSTs never do. - let (data, response) = try await sendRetryingTransientErrors( - request, - attempts: retryTransientErrors ? 5 : 1 - ) + let (data, response) = try await retryPolicy.send( + maxAttempts: retryTransientErrors ? retryPolicy.maxAttempts : 1, + log: log + ) { + try await self.urlSession.data(for: request, delegate: nil) + } guard let httpResponse = response as? HTTPURLResponse, validResponse.isValid(response: httpResponse.statusCode) else { @@ -169,28 +174,6 @@ class HTTPClient { return (data, httpResponse) } - // Send `request`, repeating it up to `attempts` times in total while Apple's - // gateway answers 502, 503 or 504. Any other answer is returned as is. - private func sendRetryingTransientErrors( - _ request: URLRequest, - attempts: Int - ) async throws -> (Data, URLResponse) { - var attempt = 1 - while true { - let (data, response) = try await self.urlSession.data(for: request, delegate: nil) - if let http = response as? HTTPURLResponse, - [502, 503, 504].contains(http.statusCode), - attempt < attempts - { - log.debug("Transient HTTP \(http.statusCode), retrying (attempt \(attempt) of \(attempts))") - try await Task.sleep(for: .milliseconds(300 * attempt)) - attempt += 1 - continue - } - return (data, response) - } - } - // prepare an URLRequest for a given url, method, body, and headers // https://softwareengineering.stackexchange.com/questions/100959/how-do-you-unit-test-private-methods internal func request( @@ -234,3 +217,40 @@ internal func buildURLRequest( return request } + +// Apple's sign in servers answer 502, 503 or 504 for a share of otherwise valid +// requests. This policy repeats a request while one of those comes back, waiting +// a little longer before each new attempt. Only requests that are safe to +// repeat (read-only GETs) should use it. +struct TransientRetryPolicy: Sendable { + // total number of attempts, the first one included + var maxAttempts = 5 + // pause before the second attempt; each later pause adds one more of these + var retryDelay: Duration = .milliseconds(300) + // gateway status codes worth another attempt + var transientStatusCodes: Set = [502, 503, 504] + + // Run `request` up to `maxAttempts` times (this policy's own limit when the + // caller passes none) and return the first answer that is not transient. + func send( + maxAttempts: Int? = nil, + log: Logger, + _ request: () async throws -> (Data, URLResponse) + ) async throws -> (Data, URLResponse) { + let limit = maxAttempts ?? self.maxAttempts + var attempt = 1 + while true { + let (data, response) = try await request() + if let http = response as? HTTPURLResponse, + transientStatusCodes.contains(http.statusCode), + attempt < limit + { + log.debug("Transient HTTP \(http.statusCode), retrying (attempt \(attempt) of \(limit))") + try await Task.sleep(for: retryDelay * attempt) + attempt += 1 + continue + } + return (data, response) + } + } +} diff --git a/Tests/xcodeinstallTests/API/AuthenticationTests.swift b/Tests/xcodeinstallTests/API/AuthenticationTests.swift index f577c60..25ed00f 100644 --- a/Tests/xcodeinstallTests/API/AuthenticationTests.swift +++ b/Tests/xcodeinstallTests/API/AuthenticationTests.swift @@ -76,6 +76,29 @@ extension AuthenticationTests { #expect(serviceKey.authServiceUrl == "https://idmsa.apple.com/appleauth") } + @Test("Test sign out redirect request retries transient gateway errors") + func testSignoutLocationRetriesTransientErrors() async throws { + let authenticator = getAppleAuthenticator() + authenticator.retryPolicy.retryDelay = .zero + let url = URL(string: "https://appstoreconnect.apple.com/logout")! + let location = "https://idmsa.apple.com/appleauth/signout?widgetKey=key&asop=destroy-session" + + self.sessionData.enqueueResponse( + data: Data(), + response: HTTPURLResponse(url: url, statusCode: 504, httpVersion: nil, headerFields: nil)! + ) + self.sessionData.enqueueResponse( + data: Data(), + response: HTTPURLResponse( + url: url, statusCode: 302, httpVersion: nil, headerFields: ["Location": location])! + ) + + let result = try await authenticator.signoutLocation(using: self.sessionData) + + #expect(result == location) + #expect(self.sessionData.allRequests.count == 2) + } + @Test("Test Apple Service Key Retrieval (olympus fallback)") func testAppleServiceKey() async throws { let url = "https://dummy" diff --git a/Tests/xcodeinstallTests/API/HTTPClientTests.swift b/Tests/xcodeinstallTests/API/HTTPClientTests.swift index 9a26e5a..24e48ef 100644 --- a/Tests/xcodeinstallTests/API/HTTPClientTests.swift +++ b/Tests/xcodeinstallTests/API/HTTPClientTests.swift @@ -29,6 +29,8 @@ struct HTTPClientTests { self.sessionData = env.urlSessionData as? MockedURLSession self.sessionDownload = env.urlSessionDownload as? MockedURLSession self.client = HTTPClient(secrets: env.secrets!, urlSession: env.urlSessionData, log: log) + // no real pause between attempts in tests + self.client.retryPolicy.retryDelay = .zero try await env.secrets!.clearSecrets() } From f24390cfbeed570ea396da3c885889d57338e115 Mon Sep 17 00:00:00 2001 From: ec2-user Date: Thu, 19 Nov 2026 10:24:18 +0000 Subject: [PATCH 3/3] fix: lower swift-log to 1.17.1 so it resolves with Noora Noora 0.57.8 requires swift-log below 1.17.2, so the 1.17.2 bump (#167) made dependency resolution fail. 1.17.1 is the highest version both accept. --- Package.resolved | 14 +++++++------- Package.swift | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Package.resolved b/Package.resolved index c308c35..69686db 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "18299c90f6f7c8e213a41f48c82275419484a05ec8b82bf27f1870c5b2f765bb", + "originHash" : "039cc2d9ea63f69b36ac7d088f67ebee209f15db47157fcceb430ae49c0e332d", "pins" : [ { "identity" : "async-http-client", @@ -51,8 +51,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/onevcat/Rainbow", "state" : { - "revision" : "cdf146ae671b2624917648b61c908d1244b98ca1", - "version" : "4.2.1" + "revision" : "711ca908ca19d16e577d575c8d0a48151a16bb1b", + "version" : "4.2.2" } }, { @@ -69,8 +69,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/soto-project/soto-core.git", "state" : { - "revision" : "47454bf79649691da406978ae803b51b54246d6e", - "version" : "7.14.0" + "revision" : "4309f0c0f608b1f4db1d84e5928a925a3894ee91", + "version" : "7.17.0" } }, { @@ -186,8 +186,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-log.git", "state" : { - "revision" : "c7413d967b97aae67c46452bf7645bdb7b200e8e", - "version" : "1.17.2" + "revision" : "34b131f794c303b62c38d19263bcf146e538da75", + "version" : "1.17.1" } }, { diff --git a/Package.swift b/Package.swift index 4b83463..64976fb 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( .executable(name: "xcodeinstall", targets: ["xcodeinstall"]) ], dependencies: [ - .package(url: "https://github.com/apple/swift-log.git", from: "1.17.2"), + .package(url: "https://github.com/apple/swift-log.git", from: "1.17.1"), .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.8.2"), .package(url: "https://github.com/apple/swift-crypto", from: "4.5.2"), .package(url: "https://github.com/apple/swift-system", from: "1.9.0"),