From 123a851515a1f0a924a741ea4ce700e7e759b840 Mon Sep 17 00:00:00 2001 From: Aditya Ramani Date: Tue, 17 Nov 2026 09:16:08 -0800 Subject: [PATCH 1/2] ContainerizationOS: add an async wait method Command.wait() blocks the calling thread in wait4(). Once the blocked threads exhaust the pool, any subsequent async step in the same process hang. Add Command.waitAsync(), which runs wait4 on a detached thread and resumes through a CheckedContinuation --- Sources/Containerization/CHProcess.swift | 2 +- .../Containerization/VirtiofsdProcess.swift | 2 +- Sources/ContainerizationOS/Command.swift | 11 +++ .../CommandWaitAsyncTests.swift | 70 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 Tests/ContainerizationOSTests/CommandWaitAsyncTests.swift 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..4f09aa29a --- /dev/null +++ b/Tests/ContainerizationOSTests/CommandWaitAsyncTests.swift @@ -0,0 +1,70 @@ +//===----------------------------------------------------------------------===// +// 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 Foundation +import Testing + +import ContainerizationOS + +#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 ..< childCount { + var command = Command("/bin/sleep", arguments: ["600"]) + try command.start() + commands.append(command) + waits.append(Task { try await command.waitAsync() }) + } + defer { + for command in commands { + _ = command.kill(SIGKILL) + } + } + + let start = ContinuousClock.now + for _ in 0 ..< 20 { + try await Task.sleep(for: .milliseconds(25)) + } + #expect(ContinuousClock.now - start < .seconds(10)) + + for command in commands { + _ = command.kill(SIGKILL) + } + for wait in waits { + #expect(try await wait.value == 128 + Int32(SIGKILL)) + } + } + + @Test func returnsExitStatus() async throws { + var command = Command("/bin/sh", arguments: ["-c", "exit 42"]) + try command.start() + #expect(try await command.waitAsync() == 42) + } +} From c1b5066dfb20382364bc6ae97375c4623b89854a Mon Sep 17 00:00:00 2001 From: Aditya Ramani Date: Tue, 17 Nov 2026 16:20:20 -0800 Subject: [PATCH 2/2] Format the changes --- .../CommandWaitAsyncTests.swift | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Tests/ContainerizationOSTests/CommandWaitAsyncTests.swift b/Tests/ContainerizationOSTests/CommandWaitAsyncTests.swift index 4f09aa29a..dfc9206a8 100644 --- a/Tests/ContainerizationOSTests/CommandWaitAsyncTests.swift +++ b/Tests/ContainerizationOSTests/CommandWaitAsyncTests.swift @@ -14,11 +14,10 @@ // limitations under the License. //===----------------------------------------------------------------------===// +import ContainerizationOS import Foundation import Testing -import ContainerizationOS - #if canImport(Darwin) import Darwin #elseif canImport(Glibc) @@ -36,8 +35,8 @@ final class CommandWaitAsyncTests { let childCount = max(50, 4 * ProcessInfo.processInfo.processorCount) var commands: [Command] = [] var waits: [Task] = [] - for _ in 0 ..< childCount { - var command = Command("/bin/sleep", arguments: ["600"]) + for _ in 0..