diff --git a/Sources/Containerization/CHProcess.swift b/Sources/Containerization/CHProcess.swift index fd1c6b180..44bbf72b5 100644 --- a/Sources/Containerization/CHProcess.swift +++ b/Sources/Containerization/CHProcess.swift @@ -104,7 +104,7 @@ final class CHProcess: Sendable { let exitTask = Task.detached { [command, logger] in do { - let status = try command.wait() + let status = try await command.waitAsync() if status >= 128 { return .signalled(status - 128) } diff --git a/Sources/Containerization/VirtiofsdProcess.swift b/Sources/Containerization/VirtiofsdProcess.swift index 4a2460d77..894ce554b 100644 --- a/Sources/Containerization/VirtiofsdProcess.swift +++ b/Sources/Containerization/VirtiofsdProcess.swift @@ -113,7 +113,7 @@ final class VirtiofsdProcess: Sendable { let exitTask = Task.detached { [command, logger] in do { - _ = try command.wait() + _ = try await command.waitAsync() } catch { logger?.error("virtiofsd wait failed: \(error)") } diff --git a/Sources/ContainerizationOS/Command.swift b/Sources/ContainerizationOS/Command.swift index 63fbe7b4f..9a1aee1e8 100644 --- a/Sources/ContainerizationOS/Command.swift +++ b/Sources/ContainerizationOS/Command.swift @@ -163,6 +163,17 @@ extension Command { self.state.pid.store(child, ordering: .releasing) } + /// Wait for the process to exit on a dedicated thread and return the exit status. + /// `wait4` blocks the calling thread; on the cooperative pool enough concurrent + /// waits starve async scheduling. + public func waitAsync() async throws -> Int32 { + try await withCheckedThrowingContinuation { (cc: CheckedContinuation) in + Thread.detachNewThread { + cc.resume(with: Result { try self.wait() }) + } + } + } + /// Wait for the process to exit and return the exit status. @discardableResult public func wait() throws -> Int32 { diff --git a/Tests/ContainerizationOSTests/CommandWaitAsyncTests.swift b/Tests/ContainerizationOSTests/CommandWaitAsyncTests.swift new file mode 100644 index 000000000..dfc9206a8 --- /dev/null +++ b/Tests/ContainerizationOSTests/CommandWaitAsyncTests.swift @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// Copyright © 2026 Apple Inc. and the Containerization project authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//===----------------------------------------------------------------------===// + +import ContainerizationOS +import Foundation +import Testing + +#if canImport(Darwin) +import Darwin +#elseif canImport(Glibc) +import Glibc +#elseif canImport(Musl) +import Musl +#endif + +@Suite("Command waitAsync tests") +final class CommandWaitAsyncTests { + /// Parks `waitAsync` on far more children than there are CPUs; the Swift + /// cooperative pool must keep scheduling. The blocking `wait4` variant + /// held a pool thread per child and starved it. + @Test func poolStaysResponsiveWithManyWaiters() async throws { + let childCount = max(50, 4 * ProcessInfo.processInfo.processorCount) + var commands: [Command] = [] + var waits: [Task] = [] + for _ in 0..