Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Containerization/CHProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ final class CHProcess: Sendable {

let exitTask = Task<ExitReason, Never>.detached { [command, logger] in
do {
let status = try command.wait()
let status = try await command.waitAsync()
if status >= 128 {
return .signalled(status - 128)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Containerization/VirtiofsdProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ final class VirtiofsdProcess: Sendable {

let exitTask = Task<Void, Never>.detached { [command, logger] in
do {
_ = try command.wait()
_ = try await command.waitAsync()
} catch {
logger?.error("virtiofsd wait failed: \(error)")
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/ContainerizationOS/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int32, Swift.Error>) 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 {
Expand Down
69 changes: 69 additions & 0 deletions Tests/ContainerizationOSTests/CommandWaitAsyncTests.swift
Original file line number Diff line number Diff line change
@@ -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<Int32, any Error>] = []
for _ in 0..<childCount {
let 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 {
let command = Command("/bin/sh", arguments: ["-c", "exit 42"])
try command.start()
#expect(try await command.waitAsync() == 42)
}
}
Loading