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
Changes from 2 commits
8c0e056
5686dec
d33c2ad
d5902c4
333157f
0b0b726
e38879b
df6ad97
df9d816
c2ec2a1
e8a2711
a2666ae
d6c3a2b
a674753
c3b3453
dffd30f
690aba5
3560472
66cd6fe
306fa38
055e961
547a53d
bc3fdcb
451a73a
e592ac7
ad288e2
7ef841e
686b390
c79ed0b
2d475b6
5528388
624fdab
99d0ec6
ad0fa68
810f2f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1192,6 +1192,7 @@ public final class MobileShellComposite: MobileTerminalOutputSinking { | |
| } | ||
|
|
||
| let directRoute = try? Self.manualHostRoute(host: normalizedHost, port: port) | ||
| activeRoute = directRoute | ||
| let attemptID = beginPairingAttempt(method: "manual") | ||
| // Fast offline preflight: fail immediately instead of stacking | ||
| // per-route timeouts into the opaque ~60s blob. | ||
|
|
@@ -3324,6 +3325,7 @@ public final class MobileShellComposite: MobileTerminalOutputSinking { | |
| // therefore cannot authorize, which is intended. | ||
| let routeAllowsStackAuthFallback = allowsStackAuthFallback | ||
| ?? supportedRoutes.allSatisfy(MobileShellRouteAuthPolicy.routeAllowsStackAuth) | ||
| let pairingAttemptStartedAt = Date() | ||
| var lastError: (any Error)? | ||
| for route in supportedRoutes { | ||
| activeRoute = route | ||
|
|
@@ -3336,9 +3338,16 @@ public final class MobileShellComposite: MobileTerminalOutputSinking { | |
| ) | ||
| for workspaceListRequest in workspaceListRequests { | ||
| do { | ||
| let requestTimeoutNanoseconds = Self.boundedPairingRequestTimeoutNanoseconds( | ||
| runtime: runtime, | ||
| attemptStartedAt: pairingAttemptStartedAt | ||
| ) | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| guard requestTimeoutNanoseconds > 0 else { | ||
| throw MobileShellConnectionError.requestTimedOut | ||
| } | ||
| let resultData = try await client.sendRequest( | ||
| workspaceListRequest.data, | ||
| timeoutNanoseconds: runtime.pairingRequestTimeoutNanoseconds | ||
| timeoutNanoseconds: requestTimeoutNanoseconds | ||
| ) | ||
| let response = try MobileSyncWorkspaceListResponse.decode(resultData) | ||
| guard isCurrentConnectionAttempt(generation) else { return nil } | ||
|
|
@@ -3381,11 +3390,28 @@ public final class MobileShellComposite: MobileTerminalOutputSinking { | |
| } | ||
| } | ||
|
|
||
| clearRemoteConnectionContext() | ||
| diagnosticLog?.record(DiagnosticEvent(.pairFail)) | ||
| throw lastError ?? MobileShellConnectionError.connectionClosed | ||
| } | ||
|
|
||
| private static func boundedPairingRequestTimeoutNanoseconds( | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| runtime: any MobileSyncRuntime, | ||
| attemptStartedAt: Date | ||
| ) -> UInt64 { | ||
| let requestTimeout = runtime.pairingRequestTimeoutNanoseconds | ||
| let attemptTimeout = runtime.pairingAttemptTimeoutNanoseconds | ||
| guard attemptTimeout > 0 else { | ||
| return requestTimeout | ||
| } | ||
|
|
||
| let elapsedSeconds = max(0, Date().timeIntervalSince(attemptStartedAt)) | ||
| let elapsedNanoseconds = UInt64((elapsedSeconds * 1_000_000_000).rounded(.up)) | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| guard elapsedNanoseconds < attemptTimeout else { | ||
| return 0 | ||
| } | ||
| return min(requestTimeout, attemptTimeout - elapsedNanoseconds) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because both reads use the system clock, the new deadline math cannot be controlled in tests — the test in |
||
|
|
||
| private struct WorkspaceListRequest { | ||
| var data: Data | ||
| var isScoped: Bool | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import CMUXMobileCore | ||
| import CmuxMobileRPC | ||
| import CmuxMobileShellModel | ||
| import CmuxMobileTransport | ||
| import Foundation | ||
| import Testing | ||
| @testable import CmuxMobileShell | ||
|
|
||
| @MainActor | ||
| @Suite struct MobilePairingAttemptDeadlineTests { | ||
| @Test func qrPairingURLTimesOutWithoutWaitingForStuckTransport() async throws { | ||
| let store = makeStore() | ||
| let startedAt = Date() | ||
|
|
||
| let result = await store.connectPairingURLResult(Self.qrURL) | ||
|
|
||
| #expect(result == .failed) | ||
| #expect(store.connectionState == .disconnected) | ||
| #expect(store.connectionError?.isEmpty == false) | ||
| #expect(store.connectionError?.contains("100.64.0.5") == true) | ||
| #expect(Date().timeIntervalSince(startedAt) < 0.05) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| @Test func scannedOrPastedPairingInputUsesSameDeadline() async throws { | ||
| let store = makeStore(pairingCode: Self.qrURL) | ||
| let startedAt = Date() | ||
|
|
||
| await store.connectPairingInput() | ||
|
|
||
| #expect(store.connectionState == .disconnected) | ||
| #expect(store.connectionError?.isEmpty == false) | ||
| #expect(store.connectionError?.contains("100.64.0.5") == true) | ||
| #expect(Date().timeIntervalSince(startedAt) < 0.05) | ||
| } | ||
|
|
||
| private static let qrURL = "cmux-ios://attach?v=2&pc=1&r=100.64.0.5:58465" | ||
|
|
||
| private func makeStore(pairingCode: String = "") -> MobileShellComposite { | ||
| MobileShellComposite( | ||
| runtime: PairingDeadlineRuntime(), | ||
| isSignedIn: true, | ||
| pairingCode: pairingCode, | ||
| reachability: AlwaysOnlineReachability(), | ||
| pairingHintDefaults: UserDefaults(suiteName: "pairing-deadline-\(UUID().uuidString)")! | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private struct PairingDeadlineRuntime: MobileSyncRuntime { | ||
| var transportFactory: any CmxByteTransportFactory = SlowIgnoringCancellationTransportFactory() | ||
| var stackAccessTokenProvider: @Sendable () async throws -> String = { "test-stack-token" } | ||
| var stackAccessTokenForceRefresher: @Sendable () async throws -> String = { "test-stack-token" } | ||
| var rpcRequestTimeoutNanoseconds: UInt64 = 30 * 1_000_000_000 | ||
| var now: @Sendable () -> Date = { Date() } | ||
| var supportedRouteKinds: [CmxAttachTransportKind] = [.tailscale] | ||
| var pairingRequestTimeoutNanoseconds: UInt64 = 30 * 1_000_000_000 | ||
| var pairingAttemptTimeoutNanoseconds: UInt64 = 1_000_000 | ||
| var supportsServerPushEvents: Bool = false | ||
| } | ||
|
|
||
| private struct AlwaysOnlineReachability: ReachabilityProviding { | ||
| var isOnline: Bool { get async { true } } | ||
|
|
||
| func pathChanges() -> AsyncStream<Void> { | ||
| AsyncStream { continuation in | ||
| continuation.finish() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private struct SlowIgnoringCancellationTransportFactory: CmxByteTransportFactory { | ||
| func makeTransport(for route: CmxAttachRoute) throws -> any CmxByteTransport { | ||
| SlowIgnoringCancellationTransport() | ||
| } | ||
| } | ||
|
|
||
| private actor SlowIgnoringCancellationTransport: CmxByteTransport { | ||
| func connect() async throws { | ||
| let startedAt = Date() | ||
| while Date().timeIntervalSince(startedAt) < 0.2 { | ||
| try? await Task.sleep(nanoseconds: 10_000_000) | ||
| } | ||
| throw CmxNetworkByteTransportError.connectionTimedOut | ||
| } | ||
|
|
||
| func receive() async throws -> Data? { | ||
| nil | ||
| } | ||
|
|
||
| func send(_ data: Data) async throws {} | ||
|
|
||
| func close() async {} | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RPCRequestTimeoutStateis missing thefinishedflag, risking a leakedCheckedContinuationAuthPhaseTimeoutState(the direct model for this class) guards bothinstallandsetCancelHandleragainst a priorcancel()call via afinished: Boolflag.RPCRequestTimeoutStatedoes not: itsinstallunconditionally stores the continuation, and itssetCancelHandlernever calls the handler eagerly.Concrete failure:
withTaskCancellationHandlerguarantees that if the task is already cancelled when the function is called,onCancelfires before the body runs. If that happens,state.cancel()reads a nil continuation and returns without resuming anything; theninstallstores the continuation that nothing will ever resume. The result is a permanently hungCheckedContinuation— a logged runtime warning in release and a crash on dealloc in debug builds — leaving the pairing spinner active indefinitely.Second gap: if
cancel()fires afterinstallbut beforesetCancelHandler, the continuation is correctly resumed withCancellationError, butoperationTaskandtimeoutTaskare never cancelled becausesetCancelHandlerdoes not call the handler eagerly when already finished. TheAuthPhaseTimeoutStateaddresses this withif finished { handler() }insetCancelHandler. The same two-line pattern needs to be added here.