Skip to content

fix: retry Apple gateway 502/503/504 during authentication - #170

Merged
sebsto merged 3 commits into
mainfrom
fix/retry-apple-gateway-errors
Nov 19, 2026
Merged

sebsto merged 3 commits into
mainfrom
fix/retry-apple-gateway-errors

Conversation

@sebsto

@sebsto sebsto commented Nov 17, 2026

Copy link
Copy Markdown
Owner

xcodeinstall authenticate now retries when Apple's servers answer 502, 503 or 504.

Right now about half of the requests to idmsa.apple.com fail that way, so each run broke at a different step (502, 504, or a JSON error from an HTML error page).

Read-only requests retry up to 5 times with a short pause: the service key lookup, the olympus config call and the hashcash challenge. Sign-in POSTs never retry.

Tests: swift test passes (207 existing plus 2 new for apiCall).

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 {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats sendRetryingTransientErrors from HTTPClient.swift. Could this call go through the same helper, so the retry rule lives in one place?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 97e7799: both calls now use one TransientRetryPolicy.send.

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),

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5, 300 and [502, 503, 504] now appear in two places. Could they be named constants, for example maxAttempts, retryDelay and transientStatusCodes?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 97e7799: maxAttempts, retryDelay and transientStatusCodes live in TransientRetryPolicy.

}

@Test("apiCall retries 504 answers when asked to")
func apiCallRetriesTransientErrors() async throws {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test really sleeps 300 ms plus 600 ms. Could the delay be injectable, so the test runs instantly?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 97e7799: retryDelay is injectable and tests set it to zero.

guard let httpResponse = response as? HTTPURLResponse else {
return nil
}
if [502, 503, 504].contains(httpResponse.statusCode), attempt < 5 {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test for this retry path? The two new tests only cover apiCall.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 97e7799: added a test for the /logout retry.

ec2-user added 3 commits November 19, 2026 10:23
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.
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.
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.
@sebsto
sebsto force-pushed the fix/retry-apple-gateway-errors branch from 97e7799 to f24390c Compare November 19, 2026 10:24
@sebsto
sebsto merged commit 72a42d7 into main Nov 19, 2026
2 checks passed
@sebsto
sebsto deleted the fix/retry-apple-gateway-errors branch November 19, 2026 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant