Skip to content

Expose platform-specific process file descriptors #101

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

Merged
merged 1 commit into from
Aug 11, 2025
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/Subprocess/Platforms/Subprocess+Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ extension Configuration {
public struct ProcessIdentifier: Sendable, Hashable {
/// The platform specific process identifier value
public let value: pid_t
internal let processDescriptor: PlatformFileDescriptor
public let processDescriptor: CInt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: since we are using it else where might as well keep it here as well

Copy link
Contributor Author

@jakepetroules jakepetroules Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PlatformFileDescriptor is internal (just like ProcessIdentifierProtocol)


internal init(value: pid_t, processDescriptor: PlatformFileDescriptor) {
self.value = value
Expand Down
5 changes: 2 additions & 3 deletions Sources/Subprocess/Platforms/Subprocess+Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -677,9 +677,8 @@ extension Environment {
public struct ProcessIdentifier: Sendable, Hashable {
/// Windows specific process identifier value
public let value: DWORD
internal nonisolated(unsafe) let processDescriptor: HANDLE
internal nonisolated(unsafe) let threadHandle: HANDLE

public nonisolated(unsafe) let processDescriptor: HANDLE
public nonisolated(unsafe) let threadHandle: HANDLE

internal init(value: DWORD, processDescriptor: HANDLE, threadHandle: HANDLE) {
self.value = value
Expand Down
9 changes: 9 additions & 0 deletions Tests/SubprocessTests/PlatformConformance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ protocol ProcessIdentifierProtocol: Sendable, Hashable, CustomStringConvertible,
#else
var value: pid_t { get }
#endif

#if os(Linux) || os(Android)
var processDescriptor: PlatformFileDescriptor { get }
#endif

#if os(Windows)
nonisolated(unsafe) var processDescriptor: PlatformFileDescriptor { get }
nonisolated(unsafe) var threadHandle: PlatformFileDescriptor { get }
#endif
}

extension ProcessIdentifier : ProcessIdentifierProtocol {}
16 changes: 16 additions & 0 deletions Tests/SubprocessTests/SubprocessTests+Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ struct SubprocessLinuxTests {
#expect(result.terminationStatus == .unhandledException(SIGTERM))
}
}

@Test func testUniqueProcessIdentifier() async throws {
_ = try await Subprocess.run(
.path("/bin/echo"),
output: .discarded,
error: .discarded
) { subprocess in
if subprocess.processIdentifier.processDescriptor > 0 {
var statinfo = stat()
try #require(fstat(subprocess.processIdentifier.processDescriptor, &statinfo) == 0)

// In kernel 6.9+, st_ino globally uniquely identifies the process
#expect(statinfo.st_ino > 0)
}
}
}
}

fileprivate enum ProcessState: String {
Expand Down
30 changes: 30 additions & 0 deletions Tests/SubprocessTests/SubprocessTests+Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,36 @@ extension SubprocessWindowsTests {
}
#expect(stuckProcess.terminationStatus.isSuccess)
}

/// Tests a use case for Windows platform handles by assigning the newly created process to a Job Object
/// - see: https://devblogs.microsoft.com/oldnewthing/20131209-00/
@Test func testPlatformHandles() async throws {
let hJob = CreateJobObjectW(nil, nil)
defer { #expect(CloseHandle(hJob)) }
var info = JOBOBJECT_EXTENDED_LIMIT_INFORMATION()
info.BasicLimitInformation.LimitFlags = DWORD(JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
#expect(SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &info, DWORD(MemoryLayout<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>.size)))

var platformOptions = PlatformOptions()
platformOptions.preSpawnProcessConfigurator = { (createProcessFlags, startupInfo) in
createProcessFlags |= DWORD(CREATE_SUSPENDED)
}

let result = try await Subprocess.run(
self.cmdExe,
arguments: ["/c", "echo"],
platformOptions: platformOptions,
output: .discarded
) { execution, _ in
guard AssignProcessToJobObject(hJob, execution.processIdentifier.processDescriptor) else {
throw SubprocessError.UnderlyingError(rawValue: GetLastError())
}
guard ResumeThread(execution.processIdentifier.threadHandle) != DWORD(bitPattern: -1) else {
throw SubprocessError.UnderlyingError(rawValue: GetLastError())
}
}
#expect(result.terminationStatus.isSuccess)
}
}

// MARK: - User Utils
Expand Down