Skip to content

Change runDetached to return a ProcessHandle instead of a ProcessIdentifier #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: eng/PR-platform-handles
Choose a base branch
from
Open
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
7 changes: 3 additions & 4 deletions Sources/Subprocess/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ public func runDetached(
input: FileDescriptor? = nil,
output: FileDescriptor? = nil,
error: FileDescriptor? = nil
) throws -> ProcessIdentifier {
) throws -> ProcessHandle {
let config: Configuration = Configuration(
executable: executable,
arguments: arguments,
Expand Down Expand Up @@ -643,7 +643,7 @@ public func runDetached(
input: FileDescriptor? = nil,
output: FileDescriptor? = nil,
error: FileDescriptor? = nil
) throws -> ProcessIdentifier {
) throws -> ProcessHandle {
let execution: Execution
switch (input, output, error) {
case (.none, .none, .none):
Expand Down Expand Up @@ -753,7 +753,6 @@ public func runDetached(
errorPipe: try processError.createPipe()
).execution
}
execution.release()
return execution.processIdentifier
return ProcessHandle(execution: execution)
}

26 changes: 26 additions & 0 deletions Sources/Subprocess/Execution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ public struct Execution: Sendable {
}
}

/// An object that represents a subprocess that has been
/// executed without monitoring the state of the subprocess
/// nor waiting until it exits. You can use this object to control
/// the process using platform-native APIs.
public struct ProcessHandle: Sendable, ~Copyable {
/// The process identifier of the current execution
public var processIdentifier: ProcessIdentifier {
execution.processIdentifier
}

/// The collection of platform-specific handles of the current execution.
public var platformHandles: PlatformHandles {
execution.platformHandles
}

private let execution: Execution

init(execution: Execution) {
self.execution = execution
}

deinit {
execution.release()
}
}

// MARK: - Output Capture
internal enum OutputCapturingState<Output: Sendable, Error: Sendable>: Sendable {
case standardOutputCaptured(Output)
Expand Down
3 changes: 2 additions & 1 deletion Tests/SubprocessTests/SubprocessTests+Unix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,12 @@ extension SubprocessUnixTests {
extension SubprocessUnixTests {
@Test func testRunDetached() async throws {
let (readFd, writeFd) = try FileDescriptor.pipe()
let pid = try runDetached(
let execution = try runDetached(
.path("/bin/sh"),
arguments: ["-c", "echo $$"],
output: writeFd
)
let pid = execution.processIdentifier
var status: Int32 = 0
waitpid(pid.value, &status, 0)
#expect(_was_process_exited(status) > 0)
Expand Down
17 changes: 3 additions & 14 deletions Tests/SubprocessTests/SubprocessTests+Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -729,35 +729,24 @@ extension SubprocessWindowsTests {
DWORD(HANDLE_FLAG_INHERIT),
0
)
let pid = try Subprocess.runDetached(
let execution = try Subprocess.runDetached(
.path("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"),
arguments: [
"-Command", "Write-Host $PID",
],
output: writeFd
)
// Wait for process to finish
guard
let processHandle = OpenProcess(
DWORD(PROCESS_QUERY_INFORMATION | SYNCHRONIZE),
false,
pid.value
)
else {
Issue.record("Failed to get process handle")
return
}

// Wait for the process to finish
WaitForSingleObject(processHandle, INFINITE)
WaitForSingleObject(execution.platformHandles.processInformation.hProcess, INFINITE)

// Up to 10 characters because Windows process IDs are DWORDs (UInt32), whose max value is 10 digits.
try writeFd.close()
let data = try await readFd.readUntilEOF(upToLength: 10)
let resultPID = try #require(
String(data: data, encoding: .utf8)
).trimmingCharacters(in: .whitespacesAndNewlines)
#expect("\(pid.value)" == resultPID)
#expect("\(execution.processIdentifier.value)" == resultPID)
try readFd.close()
}
}
Expand Down
Loading