Repository navigation
Bound iOS pairing attempts #6495
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
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
8c0e056
Add iOS pairing timeout regression tests
lawrencecchen 5686dec
Bound iOS pairing attempts
lawrencecchen d33c2ad
Add launch restore timeout regression test
lawrencecchen d5902c4
Bound launch session restore
lawrencecchen 333157f
Merge remote-tracking branch 'origin/main' into feat-ios-pairing-timeout
lawrencecchen 0b0b726
Restore cached sessions immediately
lawrencecchen e38879b
Prefer cached session over dev auto-login restore
lawrencecchen df6ad97
Tighten iOS pairing timeout races
lawrencecchen df9d816
Tear down timed-out mobile RPC connects
lawrencecchen c2ec2a1
Claim mobile RPC timeout before teardown
lawrencecchen e8a2711
Bound mobile RPC auth augmentation
lawrencecchen a2666ae
Cancel preinstalled mobile RPC connects
lawrencecchen d6c3a2b
Bound timed-out auth phase cleanup
lawrencecchen a674753
Refine mobile timeout cleanup
lawrencecchen c3b3453
Merge remote-tracking branch 'origin/main' into feat-ios-pairing-timeout
lawrencecchen dffd30f
Fold timeout race helpers into timeout files
lawrencecchen 690aba5
Allow RPC connect retry after timeout
lawrencecchen 3560472
Add CLI session restore debug command
lawrencecchen 66cd6fe
Rename session restore diagnostics to sessions list
lawrencecchen 306fa38
Merge remote-tracking branch 'origin/main' into feat-ios-pairing-timeout
lawrencecchen 055e961
Guard late exchange cleanup during sign-out capture
lawrencecchen 547a53d
Hold connect gate while abandoned close hangs
lawrencecchen bc3fdcb
Bound abandoned close cleanup gate release
lawrencecchen 451a73a
Move CLI regression tests to Swift Testing
lawrencecchen e592ac7
Split sessions CLI diagnostics
lawrencecchen ad288e2
Localize sessions CLI diagnostics
lawrencecchen 7ef841e
Fix sessions CLI tuple inference
lawrencecchen 686b390
Stop canceled validation token probes
lawrencecchen c79ed0b
Fix Swift Testing assertion shims
lawrencecchen 2d475b6
Scope CLI Swift Testing helpers
lawrencecchen 5528388
Clear late exchange tokens after timeout
lawrencecchen 624fdab
Bound host status token fallback
lawrencecchen 99d0ec6
Deflake render grid event test
lawrencecchen ad0fa68
Speed up browser sign-out race test
lawrencecchen 810f2f7
Deflake auth runtime package CI
lawrencecchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...CmuxAuthRuntime/Sources/CmuxAuthRuntime/Coordinator/AuthCoordinator+ExchangeTimeout.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import Foundation | ||
|
|
||
| @MainActor | ||
| extension AuthCoordinator { | ||
| /// Run a sign-in flow's credential exchange as a coordinator-owned child | ||
| /// task registered under the flow's attempt id, racing the phase deadline | ||
| /// like ``runPhase(_:timeout:_:)``. | ||
| /// | ||
| /// The registration is what makes sign-out able to win against a parked | ||
| /// exchange: ``signOut(onSignedOut:teardownTimeout:)`` cancels every | ||
| /// registered exchange before clearing local state, and the SDK's write | ||
| /// chokepoint drops the token store write of a cancelled flow, so the | ||
| /// stale exchange can neither resurrect the signed-out session nor | ||
| /// clobber a newer sign-in's freshly written tokens. Caller cancellation | ||
| /// is forwarded to the child task. | ||
| func runExchange( | ||
| _ phase: AuthPhase, | ||
| flow: SignInFlowContext, | ||
| timeout: Duration, | ||
| _ operation: @escaping @Sendable () async throws -> Void | ||
| ) async throws { | ||
| let exchangeID = UUID() | ||
| guard await phaseTimeoutRegistry.begin(phase, id: exchangeID) else { | ||
| log.log("auth.phase=\(phase.rawValue) previous timed-out exchange still active") | ||
| throw AuthError.timedOut | ||
| } | ||
| let registry = phaseTimeoutRegistry | ||
| let client = self.client | ||
| let exchange = Task { | ||
| try await operation() | ||
| let refreshToken = await client.refreshToken() | ||
| if Task.isCancelled { | ||
| if let refreshToken { | ||
| await waitForSignOutCredentialCapture() | ||
| await client.clearLocalSession(ifRefreshTokenMatches: refreshToken) | ||
| } | ||
| throw CancellationError() | ||
| } | ||
| return refreshToken | ||
| } | ||
| let completion = Task { | ||
| _ = await exchange.result | ||
| } | ||
| activeSignInExchanges[flow.attempt] = AuthTrackedSignInExchange( | ||
| id: exchangeID, | ||
| task: exchange, | ||
| completion: completion | ||
| ) | ||
| Task { [weak self, registry, attempt = flow.attempt, exchangeID, phase] in | ||
| await completion.value | ||
| await registry.end(phase, id: exchangeID) | ||
| await MainActor.run { | ||
| guard self?.activeSignInExchanges[attempt]?.id == exchangeID else { return } | ||
| self?.activeSignInExchanges[attempt] = nil | ||
| } | ||
| } | ||
| try await withTaskCancellationHandler { | ||
| try await waitForExchange(exchange, id: exchangeID, attempt: flow.attempt, phase: phase, timeout: timeout) | ||
| } onCancel: { | ||
| Task { await registry.markTimedOut(phase, id: exchangeID) } | ||
| exchange.cancel() | ||
| } | ||
| } | ||
|
|
||
| private func waitForExchange( | ||
| _ exchange: Task<String?, any Error>, | ||
| id: UUID, | ||
| attempt: UInt64, | ||
| phase: AuthPhase, | ||
| timeout: Duration | ||
| ) async throws { | ||
| try Task.checkCancellation() | ||
| let race = AuthPhaseTimeoutRace() | ||
| let stream = AsyncThrowingStream<Void, any Error> { continuation in | ||
| let exchangeWaiter = Task { | ||
| do { | ||
| let refreshToken = try await exchange.value | ||
| guard await race.winOperation() else { | ||
| if let refreshToken { | ||
| await waitForSignOutCredentialCapture() | ||
| await self.client.clearLocalSession(ifRefreshTokenMatches: refreshToken) | ||
| } | ||
| return | ||
| } | ||
| await phaseTimeoutRegistry.end(phase, id: id) | ||
| tokenStoreWriteHighWater = max(tokenStoreWriteHighWater, attempt) | ||
| latestSignInRefreshToken = refreshToken | ||
| continuation.yield(()) | ||
| continuation.finish() | ||
| } catch { | ||
| await phaseTimeoutRegistry.end(phase, id: id) | ||
| guard await race.winOperation() else { return } | ||
| continuation.finish(throwing: error) | ||
| } | ||
| } | ||
| let deadline = Task { [clock, log] in | ||
| do { | ||
| try await clock.sleep(for: timeout, tolerance: nil) | ||
| try Task.checkCancellation() | ||
| } catch { | ||
| return | ||
| } | ||
| guard await race.winTimeout() else { return } | ||
| log.log("auth.phase=\(phase.rawValue) timed out after \(timeout)") | ||
| await phaseTimeoutRegistry.markTimedOut(phase, id: id) | ||
| exchange.cancel() | ||
| continuation.finish(throwing: AuthError.timedOut) | ||
| } | ||
| continuation.onTermination = { _ in | ||
| exchangeWaiter.cancel() | ||
| deadline.cancel() | ||
| } | ||
| } | ||
| do { | ||
| for try await _ in stream { | ||
| return | ||
| } | ||
| } catch AuthError.timedOut { | ||
| throw AuthError.timedOut | ||
| } catch { | ||
| throw error | ||
| } | ||
| if Task.isCancelled { | ||
| throw CancellationError() | ||
| } | ||
| throw AuthError.timedOut | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.