Repository navigation
fix(cloud): keep the link client's last stderr lines in its exit error #16057
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
6 commits
Select commit
Hold shift + click to select a range
13d4a4c
test(cloud): an exit error carries the link client's last stderr
austinywang d6a02a2
fix(cloud): wait for the link client's stderr before reporting its exit
austinywang 810d2f5
test(cloud): leave the late stderr line most of the drain bound
austinywang e938a16
test(cloud): cover the link client exiting after it names its socket
austinywang 038bbed
test(cloud): keep the late stderr writer alive past the client's exit
austinywang 52a1eac
Merge main (e709b69d8c8c) into 15488-cloud-link-exit-stderr
austinywang 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
20 changes: 20 additions & 0 deletions
20
Packages/macOS/CmuxCloud/Sources/CmuxCloud/Link/CloudMachineLink+StderrDrain.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,20 @@ | ||
| import Foundation | ||
|
|
||
| extension CloudMachineLink { | ||
| /// Waits for the link client's stderr reader to reach EOF, so an exit error | ||
| /// reads its last lines: they can still be in flight when the process | ||
| /// exits. A child the client started can hold the pipe open, so the wait | ||
| /// is bounded. | ||
| nonisolated static func awaitStderrDrain(_ drain: Task<Void, Never>, upTo limit: Duration = .seconds(1)) async { | ||
| let drained = CloudLinkFirstValue<Bool>() | ||
| Task.detached { | ||
| await drain.value | ||
| drained.resolve(true) | ||
| } | ||
| Task.detached { | ||
| try? await Task.sleep(for: limit) | ||
| drained.resolve(false) | ||
| } | ||
| _ = await drained.result | ||
| } | ||
| } |
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
72 changes: 72 additions & 0 deletions
72
Packages/macOS/CmuxCloud/Tests/CmuxCloudTests/CloudMachineLinkExitDiagnosticsTests.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,72 @@ | ||
| @testable import CmuxCloud | ||
| import CmuxCloudTui | ||
| import Foundation | ||
| import Testing | ||
|
|
||
| /// A link client that exits before naming its socket fails the connect with its | ||
| /// own exit status and stderr. The stderr reader runs in its own task, so the | ||
| /// last lines can still be in flight when the process exits. | ||
| @Suite("Cloud machine link exit diagnostics") | ||
| struct CloudMachineLinkExitDiagnosticsTests { | ||
| @Test("An exit error carries stderr written until the pipe closes") | ||
| func exitErrorWaitsForStderrToClose() async throws { | ||
| let root = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent("cmux-link-exit-stderr-\(UUID().uuidString.lowercased())", isDirectory: true) | ||
| try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) | ||
| defer { try? FileManager.default.removeItem(at: root) } | ||
| let client = root.appendingPathComponent("fake-cmux-tui") | ||
| // The client exits at once. Once it is reaped, a child it started closes | ||
| // stdout and writes the last stderr line a moment later. Closing stdout | ||
| // earlier would let connect terminate the unreaped client's process | ||
| // group, child included, before the line is written. Without the wait, | ||
| // connect builds the error within milliseconds of stdout closing. | ||
| try """ | ||
| #!/bin/sh | ||
| (while kill -0 $$ 2>/dev/null; do sleep 0.01; done | ||
| exec >/dev/null; sleep 0.1; echo 'cmux-tui: route refused' >&2) & | ||
| exit 2 | ||
| """.write(to: client, atomically: true, encoding: .utf8) | ||
| try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: client.path) | ||
| let link = CloudMachineLink(machineID: "test-machine", clientURL: client, paths: CloudTuiClientPaths(home: root)) | ||
|
|
||
| do { | ||
| _ = try await link.connect(route: "ws://10.0.0.1:1337/v1/link", session: "main", carrier: true) | ||
| Issue.record("a client that exits before its socket line must fail the connect") | ||
| } catch CloudMachineLink.LinkError.exited(let status, let output) { | ||
| #expect(status == 2) | ||
| #expect(output.contains("route refused"), "stderr written before the pipe closed must reach the error: \(output)") | ||
| } catch { | ||
| Issue.record("expected LinkError.exited, got \(error)") | ||
| } | ||
| } | ||
|
|
||
| @Test("An exit after the socket line carries stderr written until the pipe closes") | ||
| func exitAfterSocketLineWaitsForStderrToClose() async throws { | ||
| let root = FileManager.default.temporaryDirectory | ||
| .appendingPathComponent("cmux-link-exit-after-socket-\(UUID().uuidString.lowercased())", isDirectory: true) | ||
| try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) | ||
| defer { try? FileManager.default.removeItem(at: root) } | ||
| let client = root.appendingPathComponent("fake-cmux-tui") | ||
| // The client exits at once. Once it is reaped, a child it started names | ||
| // the socket, closes stdout and writes the last stderr line a moment later. | ||
| try """ | ||
| #!/bin/sh | ||
| (while kill -0 $$ 2>/dev/null; do sleep 0.01; done | ||
| printf '%s\\n' '{"event":"connection-snapshot","local_socket":"/tmp/cmux-link-exit-test.sock"}' | ||
| exec >/dev/null; sleep 0.1; echo 'cmux-tui: route refused' >&2) & | ||
| exit 2 | ||
| """.write(to: client, atomically: true, encoding: .utf8) | ||
| try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: client.path) | ||
| let link = CloudMachineLink(machineID: "test-machine", clientURL: client, paths: CloudTuiClientPaths(home: root)) | ||
|
|
||
| do { | ||
| _ = try await link.connect(route: "ws://10.0.0.1:1337/v1/link", session: "main", carrier: true) | ||
| Issue.record("a client that exits after its socket line must fail the connect") | ||
| } catch CloudMachineLink.LinkError.exited(let status, let output) { | ||
| #expect(status == 2) | ||
| #expect(output.contains("route refused"), "stderr written before the pipe closed must reach the error: \(output)") | ||
| } catch { | ||
| Issue.record("expected LinkError.exited, got \(error)") | ||
| } | ||
| } | ||
| } | ||
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.