Repository navigation
Fix persistent Cloud command deadline and cancellation races #12631
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
27 commits
Select commit
Hold shift + click to select a range
ccf8cec
test: reproduce Cloud command deadline and pipe teardown stalls
austinywang eb14189
fix: own Cloud command process groups through deadline and capture cl…
austinywang ebdae07
fix: open injected command clocks at the generic call boundary
austinywang 8643561
test: verify command cleanup, resume ordering and durable timeout state
austinywang dc6d386
fix: reap an exited leader on the cleanup deadline path
austinywang df225e6
test: reproduce inherited cold identity initialization deadlock
austinywang 2739cf5
test: make cold identity observer captures explicit
austinywang 0384850
test: validate canonical identity fixture home boundaries
austinywang fae8cbf
fix: publish cold host identity before defaults notifications
austinywang ae6c75c
test: verify actual capture descriptor closure
austinywang 4534661
Merge remote-tracking branch 'origin/main' into issue-11008-cloud-com…
austinywang 1f0c466
Merge main into Cloud command deadlines branch
lawrencecchen bda560a
Merge current main into Cloud command deadlines branch
lawrencecchen fd03627
Merge remote-tracking branch 'origin/issue-11008-cloud-command-deadli…
austinywang fe24caa
Merge main and port deadline regressions to persistent Cloud requests
austinywang 0ae1a1b
fix: enforce absolute deadlines on persistent Cloud replies
austinywang 942f640
test: keep timed out Cloud operations visible
austinywang 9538d27
ci: allow isolated deadline checks on either hosted macOS lane
austinywang 8243c86
ci: track the notification-extension bundle identity in the App Store…
austinywang 13e7df7
fix: compile the persistent Cloud transport in the isolated deadline …
austinywang 2503185
Merge origin/main into Cloud command deadlines branch
austinywang d5f2fbf
Merge branch 'main' into issue-11008-cloud-command-deadlines
lawrencecchen 3bb1b19
Merge origin/main into Cloud command deadlines branch
austinywang 522eb6d
Merge branch 'main' of https://github.com/manaflow-ai/cmux into issue…
austinywang 9bc8ef8
Merge remote-tracking branch 'origin/main' into issue-11008-cloud-com…
austinywang bc5467c
fix: keep authoritative cloud names on accepted graph
austinywang 7ce5b12
Merge branch 'main' into issue-11008-cloud-command-deadlines
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| name: Cloud command deadlines | ||
| on: | ||
| pull_request: | ||
| paths: | ||
| - Sources/Cloud/CloudTuiPersistent*.swift | ||
| - Sources/Cloud/CloudTuiManualIO*.swift | ||
| - Sources/Cloud/CloudTuiTerminalProjectionTarget.swift | ||
| - Sources/Cloud/CloudTuiRemoteColors.swift | ||
| - cmuxTests/CloudCommand*.swift | ||
| - cmuxTests/CloudTuiManualIOConnectionTests.swift | ||
| - tests/fixtures/cloud-command-deadlines/** | ||
| - tests/run_cloud_command_deadline_tests.sh | ||
| - Sources/Mobile/MobileHostIdentity.swift | ||
| - Packages/Shared/CMUXMobileCore/Sources/CMUXMobileCore/CmxDeviceIDCanonicalization.swift | ||
| - Packages/macOS/CmuxFoundation/Sources/CmuxFoundation/Concurrency/AtomicBooleanGate.swift | ||
| - Packages/macOS/CmuxFoundation/Sources/CmuxFoundationAtomicsC/** | ||
| - tests/fixtures/mobile-host-identity-cold-start/** | ||
| - tests/run_mobile_host_identity_cold_start_tests.sh | ||
| - tests/test_mobile_host_identity_cold_start.py | ||
| - .github/workflows/cloud-command-deadlines.yml | ||
| workflow_dispatch: | ||
| inputs: | ||
| source_ref: | ||
| description: Source commit to exercise (empty uses the dispatch ref) | ||
| default: "" | ||
| type: string | ||
| runner: | ||
| description: Hosted macOS runner | ||
| default: blacksmith-6vcpu-macos-15 | ||
| type: choice | ||
| options: | ||
| - blacksmith-6vcpu-macos-15 | ||
| - blacksmith-6vcpu-macos-26 | ||
| permissions: | ||
| contents: read | ||
| concurrency: | ||
| group: cloud-command-deadlines-${{ github.ref }}-${{ inputs.source_ref }}-${{ inputs.runner }} | ||
| cancel-in-progress: true | ||
| jobs: | ||
| command-regressions: | ||
| runs-on: ${{ inputs.runner || vars.MACOS_RUNNER_15 || 'blacksmith-6vcpu-macos-15' }} | ||
| timeout-minutes: 5 | ||
| steps: | ||
| - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd | ||
| with: | ||
| persist-credentials: false | ||
| ref: ${{ inputs.source_ref || github.ref }} | ||
| - name: Exercise the persistent Cloud command transport in isolation | ||
| run: tests/run_cloud_command_deadline_tests.sh "$RUNNER_TEMP/issue-11008-${{ github.run_id }}" | ||
| - name: Prove cold identity publication before defaults notification | ||
| run: tests/run_mobile_host_identity_cold_start_tests.sh "$RUNNER_TEMP/issue-11008-identity-${{ github.run_id }}" | ||
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
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,49 @@ | ||
| import Foundation | ||
|
|
||
| /// A virtual command clock that can hold timer delivery while time advances. | ||
| /// Safety: Clock.now is synchronous; the lock protects only the test clock's instant and sleepers. | ||
| final class CloudCommandDeadlineClock: Clock, @unchecked Sendable { | ||
| typealias Instant = ContinuousClock.Instant | ||
| private let lock = NSLock() | ||
| private var instant = ContinuousClock.now | ||
| private var sleepers: [UUID: (Instant, AsyncStream<Void>.Continuation)] = [:] | ||
| private let registrations = AsyncStream<Void>.makeStream() | ||
|
|
||
| var now: Instant { lock.withLock { instant } } | ||
| var minimumResolution: Duration { .nanoseconds(1) } | ||
|
|
||
| func advance(by duration: Duration, wakingTimers: Bool = true) { | ||
| let ready = lock.withLock { | ||
| instant = instant.advanced(by: duration) | ||
| return wakingTimers ? sleepers.values.filter { $0.0 <= instant }.map { $0.1 } : [] | ||
| } | ||
| for continuation in ready { continuation.finish() } | ||
| } | ||
|
|
||
| func waitUntilSleeping() async { | ||
| var iterator = registrations.stream.makeAsyncIterator() | ||
| _ = await iterator.next() | ||
| } | ||
|
|
||
| func sleep(until deadline: Instant, tolerance: Duration?) async throws { | ||
| let id = UUID() | ||
| let wakeup = AsyncStream<Void>.makeStream() | ||
| register(id, deadline: deadline, continuation: wakeup.continuation) | ||
| defer { remove(id) } | ||
| registrations.continuation.yield(()) | ||
| // AsyncStream ends on cancellation, releasing even a deliberately held timer. | ||
| for await _ in wakeup.stream {} | ||
| try Task.checkCancellation() | ||
| } | ||
|
|
||
| private func register(_ id: UUID, deadline: Instant, continuation: AsyncStream<Void>.Continuation) { | ||
| lock.withLock { | ||
| if deadline <= instant { continuation.finish() } | ||
| else { sleepers[id] = (deadline, continuation) } | ||
| } | ||
| } | ||
|
|
||
| private func remove(_ id: UUID) { | ||
| _ = lock.withLock { sleepers.removeValue(forKey: id) } | ||
| } | ||
| } |
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,72 @@ | ||
| import Foundation | ||
| import Testing | ||
| #if canImport(cmux_DEV) | ||
| @testable import cmux_DEV | ||
| #elseif canImport(cmux) | ||
| @testable import cmux | ||
| #elseif canImport(CloudCommandFixture) | ||
| @testable import CloudCommandFixture | ||
| #endif | ||
|
|
||
| @Suite(.timeLimit(.minutes(1))) struct CloudCommandDeadlineTests { | ||
| private typealias Socket = CloudTuiManualIOConnectionTests | ||
|
|
||
| @Test(arguments: [false, true]) | ||
| func responseAfterDeadlineFailsEvenWhenTimerHasNotResumed(raw: Bool) async throws { | ||
| let clock = CloudCommandDeadlineClock() | ||
| try await Socket.withResourceConnection(clock: clock) { channel, peer in | ||
| let command = Task { try await channel.request(CloudTuiRequest("session.ping", raw: raw), timeout: .seconds(30)) } | ||
| defer { command.cancel() } | ||
| let bytes = try await Socket.blocking { try Socket.readLine(peer) } | ||
| let request = try Socket.object(bytes) | ||
| let requestID = try #require(request["id"] as? String) | ||
| await clock.waitUntilSleeping() | ||
| clock.advance(by: .seconds(600), wakingTimers: false) | ||
| try Socket.write(peer, Socket.response(request, result: ["expired": true], raw: raw)) | ||
| do { | ||
| _ = try await command.value | ||
| Issue.record("a response delivered after resume cannot turn an expired request into success") | ||
| } catch CloudMachineLink.LinkError.timedOut {} | ||
|
|
||
| // An expired request never closes the machine-owned socket or replays work. | ||
| async let sibling = channel.request(CloudTuiRequest("session.snapshot")) | ||
| try await Socket.blocking { | ||
| var next = try Socket.object(Socket.readLine(peer)) | ||
| if next["operation"] as? String == "request.cancel" { | ||
| #expect(!raw) | ||
| #expect((next["params"] as? [String: Any])?["request_id"] as? String == requestID) | ||
| next = try Socket.object(Socket.readLine(peer)) | ||
| } | ||
| #expect(next["operation"] as? String == "session.snapshot") | ||
| try Socket.write(peer, Socket.response(next, result: ["alive": true])) | ||
| } | ||
| let siblingResult = try await sibling | ||
| #expect(try Socket.object(siblingResult)["alive"] as? Bool == true) | ||
| #expect(!(await channel.isClosed)) | ||
| } | ||
| } | ||
|
|
||
| @Test(arguments: [false, true]) | ||
| func responseBeforeDeadlinePreservesOutput(raw: Bool) async throws { | ||
| let clock = CloudCommandDeadlineClock() | ||
| try await Socket.withResourceConnection(clock: clock) { channel, peer in | ||
| async let command = channel.request(CloudTuiRequest("session.ping", raw: raw), timeout: .seconds(30)) | ||
| let bytes = try await Socket.blocking { try Socket.readLine(peer) } | ||
| await clock.waitUntilSleeping() | ||
| clock.advance(by: .seconds(29), wakingTimers: false) | ||
| try Socket.write(peer, Socket.response(Socket.object(bytes), result: ["value": "unchanged"], raw: raw)) | ||
| let commandResult = try await command | ||
| #expect(try Socket.object(commandResult)["value"] as? String == "unchanged") | ||
| } | ||
| } | ||
|
|
||
| @Test(arguments: [Duration.zero, .seconds(-1)]) | ||
| func expiredBudgetIsRejectedBeforeOpeningSocket(timeout: Duration) async throws { | ||
| let channel = CloudTuiPersistentResourceConnection(socketPath: "/does/not/exist") | ||
| do { | ||
| _ = try await channel.request(CloudTuiRequest("session.ping"), timeout: timeout) | ||
| Issue.record("an expired request must be rejected") | ||
| } catch CloudMachineLink.LinkError.timedOut {} | ||
| await channel.close() | ||
| } | ||
| } |
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.