Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(lume): share SSH event loop infrastructure (#3272)

injaneityea4abe672afc
diff --git a/libs/lume/Package.swift b/libs/lume/Package.swift
index abfb86c673..e3894f8088 100644
--- a/libs/lume/Package.swift
+++ b/libs/lume/Package.swift
@@ -48,6 +48,7 @@ let package = Package(
dependencies: [
"lume",
.product(name: "NIOEmbedded", package: "swift-nio"),
+ .product(name: "NIOPosix", 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.swift
index 8b08accbd1..7860d8cad3 100644
--- a/libs/lume/src/SSH/SSHClient.swift
+++ b/libs/lume/src/SSH/SSHClient.swift
@@ -22,7 +22,9 @@ public actor SSHClient {
private let user: String
private let password: String
private let connectTimeout: TimeInterval
- private let eventLoopGroup: MultiThreadedEventLoopGroup
+
+ /// Networking infrastructure has process lifetime, not client lifetime.
+ private static let sharedEventLoopGroup = MultiThreadedEventLoopGroup.singleton
public init(
host: String,
@@ -36,11 +38,6 @@ public actor SSHClient {
self.user = user
self.password = password
self.connectTimeout = connectTimeout
- self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
- }
-
- deinit {
- try? eventLoopGroup.syncShutdownGracefully()
}
/// Execute a command on the remote host
@@ -169,15 +166,17 @@ public actor SSHClient {
/// Connect to the SSH server and return the channel
private func connect() async throws -> Channel {
- let bootstrap = ClientBootstrap(group: eventLoopGroup)
+ let user = self.user
+ let password = self.password
+ let bootstrap = ClientBootstrap(group: Self.sharedEventLoopGroup)
.channelInitializer { channel in
channel.eventLoop.makeCompletedFuture {
let sshHandler = NIOSSHHandler(
role: .client(
.init(
userAuthDelegate: PasswordAuthDelegate(
- username: self.user,
- password: self.password
+ username: user,
+ password: password
),
serverAuthDelegate: AcceptAllHostKeysDelegate()
)
diff --git a/libs/lume/tests/SSHClientTests.swift b/libs/lume/tests/SSHClientTests.swift
index e0685d635a..76a6a5d217 100644
--- a/libs/lume/tests/SSHClientTests.swift
+++ b/libs/lume/tests/SSHClientTests.swift
@@ -1,6 +1,7 @@
@preconcurrency import NIOCore
@preconcurrency import NIOConcurrencyHelpers
@preconcurrency import NIOEmbedded
+@preconcurrency import NIOPosix
@preconcurrency import NIOSSH
import Testing
@@ -10,6 +11,17 @@ private enum SSHClientFutureTestError: Error {
case handlerLookupFailed
}
+@Test("SSH client teardown is safe on a NIO event loop")
+func clientTeardownIsSafeOnEventLoop() async throws {
+ let client = NIOLockedValueBox<SSHClient?>(SSHClient(host: "127.0.0.1"))
+
+ try await MultiThreadedEventLoopGroup.singleton.next().submit {
+ client.withLockedValue { $0 = nil }
+ }.get()
+
+ #expect(client.withLockedValue { $0 == nil })
+}
+
@Test("SSH child promise is not created when handler lookup fails")
func childPromiseIsCreatedAfterHandlerLookup() throws {
let eventLoop = EmbeddedEventLoop()