Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(lume): prevent SSH promise lifecycle crash (#2640)
Francesco Bonacci21e352498b78
diff --git a/libs/lume/Package.swift b/libs/lume/Package.swiftindex 1b443ce140..abfb86c673 100644--- a/libs/lume/Package.swift+++ b/libs/lume/Package.swift@@ -47,6 +47,7 @@ let package = Package(name: "lumeTests",dependencies: ["lume",+ .product(name: "NIOEmbedded", package: "swift-nio"),.product(name: "Testing", package: "swift-testing")],path: "tests")diff --git a/libs/lume/src/SSH/SSHClient.swift b/libs/lume/src/SSH/SSHClient.swiftindex cb6b4b9daf..ec9c6f6cc3 100644--- a/libs/lume/src/SSH/SSHClient.swift+++ b/libs/lume/src/SSH/SSHClient.swift@@ -48,29 +48,40 @@ public actor SSHClient {let resultPromise = channel.eventLoop.makePromise(of: SSHResult.self)let resultState = CommandResultState(promise: resultPromise)- // Create the SSH child channel for command execution.- // We keep handler access + createChannel on the event loop to avoid- // crossing a Sendable boundary with NIOSSHHandler.- let childChannelPromise = channel.eventLoop.makePromise(of: Channel.self)- let childChannelFuture = channel.pipeline.handler(type: NIOSSHHandler.self).flatMap { sshHandler -> EventLoopFuture<Channel> in- sshHandler.createChannel(childChannelPromise) { childChannel, channelType in- guard channelType == .session else {- return channel.eventLoop.makeFailedFuture(SSHError.connectionFailed("Invalid channel type"))- }-- return childChannel.eventLoop.makeCompletedFuture {- let execHandler = CommandExecHandler(- command: command,- resultState: resultState- )- try childChannel.pipeline.syncOperations.addHandler(execHandler)+ // Create the child promise only after locating NIOSSHHandler. If the+ // connection closes first, flatMap is skipped; a promise created outside+ // this closure would then be released unfulfilled and SwiftNIO traps in+ // EventLoopFuture.deinit.+ let childChannel: Channel+ do {+ childChannel = try await Self.makeChildChannelFuture(+ handlerFuture: channel.pipeline.handler(type: NIOSSHHandler.self),+ eventLoop: channel.eventLoop+ ) { sshHandler, childChannelPromise in+ sshHandler.createChannel(childChannelPromise) { childChannel, channelType in+ guard channelType == .session else {+ return channel.eventLoop.makeFailedFuture(+ SSHError.connectionFailed("Invalid channel type")+ )+ }++ return childChannel.eventLoop.makeCompletedFuture {+ let execHandler = CommandExecHandler(+ command: command,+ resultState: resultState+ )+ try childChannel.pipeline.syncOperations.addHandler(execHandler)+ }}- }- return childChannelPromise.futureResult+ }.get()+ } catch {+ // The command handler never took ownership of resultState, so resolve+ // its promise before unwinding this operation.+ resultState.fail(error)+ try? await channel.close().get()+ throw error}- let childChannel = try await childChannelFuture.get()-// Set up timeout if specifiedif timeout > 0 {let timeoutTask = channel.eventLoop.scheduleTask(in: .seconds(Int64(timeout))) {@@ -100,28 +111,36 @@ public actor SSHClient {public func interactive() async throws {let channel = try await connect()- // Create the SSH child channel for interactive session.- // We keep handler access + createChannel on the event loop to avoid- // crossing a Sendable boundary with NIOSSHHandler.- let childChannelPromise = channel.eventLoop.makePromise(of: Channel.self)+ // As in execute(), do not allocate the child promise until handler+ // lookup succeeds or a disconnect race can leak an unfulfilled promise.let sessionCompletePromise = channel.eventLoop.makePromise(of: Void.self)-- let childChannelFuture = channel.pipeline.handler(type: NIOSSHHandler.self).flatMap { sshHandler -> EventLoopFuture<Channel> in- sshHandler.createChannel(childChannelPromise) { childChannel, channelType in- guard channelType == .session else {- return channel.eventLoop.makeFailedFuture(SSHError.connectionFailed("Invalid channel type"))- }-- return childChannel.eventLoop.makeCompletedFuture {- let interactiveHandler = InteractiveSessionHandler(completePromise: sessionCompletePromise)- try childChannel.pipeline.syncOperations.addHandler(interactiveHandler)+ let childChannel: Channel+ do {+ childChannel = try await Self.makeChildChannelFuture(+ handlerFuture: channel.pipeline.handler(type: NIOSSHHandler.self),+ eventLoop: channel.eventLoop+ ) { sshHandler, childChannelPromise in+ sshHandler.createChannel(childChannelPromise) { childChannel, channelType in+ guard channelType == .session else {+ return channel.eventLoop.makeFailedFuture(+ SSHError.connectionFailed("Invalid channel type")+ )+ }++ return childChannel.eventLoop.makeCompletedFuture {+ let interactiveHandler = InteractiveSessionHandler(+ completePromise: sessionCompletePromise+ )+ try childChannel.pipeline.syncOperations.addHandler(interactiveHandler)+ }}- }- return childChannelPromise.futureResult+ }.get()+ } catch {+ sessionCompletePromise.fail(error)+ try? await channel.close().get()+ throw error}- let childChannel = try await childChannelFuture.get()-// Wait for the session to completetry await sessionCompletePromise.futureResult.get()@@ -130,6 +149,21 @@ public actor SSHClient {try? await channel.close().get()}+ /// Creates the child promise only after handler lookup succeeds. SwiftNIO+ /// deliberately traps when an unfulfilled promise is deallocated, so callers+ /// must not allocate this promise before the prerequisite future completes.+ static func makeChildChannelFuture(+ handlerFuture: EventLoopFuture<NIOSSHHandler>,+ eventLoop: EventLoop,+ initialize: @escaping (NIOSSHHandler, EventLoopPromise<Channel>) -> Void+ ) -> EventLoopFuture<Channel> {+ handlerFuture.flatMap { sshHandler in+ let childChannelPromise = eventLoop.makePromise(of: Channel.self)+ initialize(sshHandler, childChannelPromise)+ return childChannelPromise.futureResult+ }+ }+/// Connect to the SSH server and return the channelprivate func connect() async throws -> Channel {let bootstrap = ClientBootstrap(group: eventLoopGroup)diff --git a/libs/lume/tests/SSHClientTests.swift b/libs/lume/tests/SSHClientTests.swiftnew file mode 100644index 0000000000..bbd9c0cdff--- /dev/null+++ b/libs/lume/tests/SSHClientTests.swift@@ -0,0 +1,32 @@+@preconcurrency import NIOCore+@preconcurrency import NIOEmbedded+@preconcurrency import NIOSSH+import Testing++@testable import lume++private enum SSHClientFutureTestError: Error {+ case handlerLookupFailed+}++@Test("SSH child promise is not created when handler lookup fails")+func childPromiseIsCreatedAfterHandlerLookup() throws {+ let eventLoop = EmbeddedEventLoop()+ let handlerFuture = eventLoop.makeFailedFuture(+ SSHClientFutureTestError.handlerLookupFailed+ ) as EventLoopFuture<NIOSSHHandler>+ var initializerCalled = false++ let childFuture = SSHClient.makeChildChannelFuture(+ handlerFuture: handlerFuture,+ eventLoop: eventLoop+ ) { _, promise in+ initializerCalled = true+ promise.fail(SSHClientFutureTestError.handlerLookupFailed)+ }++ #expect(throws: SSHClientFutureTestError.self) {+ try childFuture.wait()+ }+ #expect(!initializerCalled)+}