Skip to content

Introduce process file descriptor (pidfd) based process monitoring for Linux #125

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 2 commits into
base: main
Choose a base branch
from

Conversation

iCharlesHu
Copy link
Contributor

The current process monitoring code for Linux has a flaw that makes it susceptible to infinite hangs under specific conditions:

  • The parent process uses any other method (other than Subprocess itself) to spawn new processes in addition to spawning with Subprocess.
  • The parent process fails to properly reap the non-Subprocess-spawned process, leaving it as a zombie in the process table.

This is because currently, we rely on running waitid() with P_ALL and WNOWAIT in an infinite loop to detect possible child process state transitions. However, we don’t reap the child process (by specifying WNOWAIT) unless we (Subprocess) actually spawned the process.

Here’s a simplified pseudo-code to illustrate the issue:

while true {
    var siginfo = siginfo_t()
    // We’re not reaping the child process
    if waitid(P_ALL, id_t(0), &siginfo, WEXITED | WNOWAIT) == 0 {
        guard let c = savedContinuation else {
            // If there’s no saved continuation, we didn’t spawn the process
            // In this case, we don’t reap the child process
            continue
        }

        siginfo = siginfo_t()
        waitid(P_PID, numericCast(pid), &siginfo, WEXITED) // We’re actually reaping the child process
    }
}

With this setup, if there are zombie children in the process table without reaping, waitid(P_ALL) will repeatedly return the same (non-Subprocess-spawned) PID with every call, causing an infinite loop.

You can observe this behavior with the following sample code:

let arguments = "\"\""

let pid = arguments.withCString { args in
    var pid: pid_t = -1
    let status = posix_spawn(&pid, "/bin/echo", nil, nil, [strdup(args)] + [nil], environ)
    guard status == 0 else {
        fatalError("posix_spawn: \(status), errno: \(errno)")
    }
    return pid
}
print("echo pid: \(pid)")

let result = try await Subprocess.run(
    .path("/bin/cat"),
    arguments: ["Package.swift"],
    output: .string(limit: .max, encoding: UTF8.self),
    error: .discarded
)
print("cat finished: \(result.terminationStatus)")
print("cat output: \(result.standardOutput ?? "")")

After running this example, you’ll notice that the parent process seems to be stuck, and the “cat finished” message is never printed. This is because the parent process never calls waitid on the echo call, leaving it in the process table. Consequently, the monitor thread runs in an infinite loop.

While some may argue that this is not a bug in Subprocess, but rather an issue with the parent code, since the POSIX standard mandates that the process spawning child process must reap the child process via waitid. However, Subprocess should still not hang due to someone else’s bug.

To resolve this issue, switch to a Linux-specific process monitoring method by creating and observing the process file descriptor (pidfd) using epoll. This approach is similar to the epoll implementation introduced in #117, with the only difference being that we’re polling pidfd instead of a regular file descriptor.

As part of this change, I also unified the “process handle” design to make it easier to expose process handles to clients later (after the 1.0 release, as requested by #101). We chose to use ProcessIdentifier to host platform-specific process file descriptors and process handles because it perfectly aligns with the original use case. To ensure flexibility, we opted for a concrete ProcessIdentifier type instead of just a number, allowing us to add more information if necessary.

@iCharlesHu
Copy link
Contributor Author

Resolves #111

@iCharlesHu
Copy link
Contributor Author

waitid with P_PIDFD was introduced in Linux kernel 5.4, which focal should have. I'm looking into what's missing

// MARK: - ProcesIdentifier

/// A platform independent identifier for a Subprocess.
public struct ProcessIdentifier: Sendable, Hashable {

Choose a reason for hiding this comment

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

Is it worthwhile to make this a protocol given the repetition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you elaborate on how would we use this protocol? The reason it's repeated is because on different platforms we have different sets of fields. I don't think having a protocol would solve this problem because we'd still need to offer different concrete types.

Choose a reason for hiding this comment

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

File this one under premature optimization but a protocol might help if the shared, non-platform-specific code started relying on the existence of methods or attributes of ProcessIdentifier. I took a look and don't see any at the moment beyond description.

If there are more expectations requiring the various ProcessIdentifier definitions to stay in sync, it might be helpful to introduce a protocol, not because any given platform needs more than one concrete type, but because the protocol will serve as a contract to keep the implementations in sync with the expectations of the shared code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh I see your point. IMO that's not a great use case here because there's no place we want to write some ProcessIdentifierProtocol as opposed to the concrete type. A protocol would help ensure all variants of ProcessIdentifier to have the value field but that's about it. As part of execution we still want to have the concrete version.

Copy link

@cthielen cthielen Jul 23, 2025

Choose a reason for hiding this comment

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

What about combining that with a typealias, e.g.

protocol SomeSubprocessConstruct {
    func doSomething()
    ...
}

// SomeFile+Windows.swift
#if canImport(WinSDK)
struct WindowsSubprocessConstruct: SomeSubprocessConstruct {
    func doSomething()
    ...
}
typealias SubprocessConstruct = WindowsSubprocessConstruct
#endif

// SomeFile+Darwin.swift
#if canImport(Darwin)
struct DarwinSubprocessConstruct: SomeSubprocessConstruct {
    func doSomething()
    ...
}
typealias SubprocessConstruct = DarwinSubprocessConstruct
#endif

// Some shared code
struct Execution {
    private var construct: SubprocessConstruct

    ...
    if construct.doSomething() { ... }
}

You know this domain way better than I do but as a naive reader, it feels to me like there should be a clearer separation between core logic and platform-specific details. Right now there's a bit of a mix: there are platform-specific files with similarly named methods as well as shared code with #if canImport().

Given there's so much platform-specific code, it'd be great to better separate it out and keep platform-specific details more sequestered away.

@@ -35,16 +35,13 @@ public struct Execution: Sendable {
public let processIdentifier: ProcessIdentifier

#if os(Windows)
internal nonisolated(unsafe) let processInformation: PROCESS_INFORMATION
internal let consoleBehavior: PlatformOptions.ConsoleBehavior
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: unrelated, but you could delete consoleBehavior as well as nothing actually uses it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I originally left it there because I wasn't sure if it is useful... I agree with you that it seems not that useful. I'll remove it in a dedicate PR since it's an API change.

-1
)
if eventCount < 0 {
if errno == EINTR || errno == EAGAIN {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: it might be worth introducing a helper function to handle EINTR/EAGAIN since it's such a common pattern throughout this codebase; see https://github.com/apple/swift-system/blob/6ee9a58c36ad98f4bd917a64d153dd211512e65d/Sources/System/Util.swift#L27 for example.

@grynspan
Copy link

This is not a bug in the existing implementation. It is a bug in the POSIX specification (and a bug in the program.)

@@ -664,6 +504,10 @@ int _subprocess_fork_exec(
// If we reached this point, something went wrong
write_error_and_exit;
} else {
int _pidfd = _pidfd_open(childPid);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use clone + CLONE_PIDFD (Linux 5.2) instead of fork + pidfd_open? Like FreeBSD's pdfork, this avoids races since combining the latter two functions is not atomic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. Unfortunately it still needs to be conditionalized due to kernel version requirements.

@jakepetroules
Copy link
Contributor

jakepetroules commented Jul 17, 2025

This is not a bug in the existing implementation. It is a bug in the POSIX specification (and a bug in the program.)

That may be true, but the implementation Charles is proposing here is more defensive against other parts of the program misbehaving, which seems like a good thing.

Including scenarios where zombies are being reaped correctly throughout the entire program, but maybe the body of one particular Subprocess.run call is stuck or otherwise taking an incredibly long time -- this implementation prevents that one process from holding up everything else.

// - musl 1.1.24 (October 2019)
// - FreeBSD 13.1 (May 2022)
// - Android 14 (API level 34) (October 2023)
return posix_spawn_file_actions_addchdir_np(file_actions, path);

Choose a reason for hiding this comment

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

This will emit a deprecation warning as of *OS 26 since the standardized version has been added.

// MARK: - ProcessIdentifier

/// A platform independent identifier for a Subprocess.
public struct ProcessIdentifier: Sendable, Hashable {

Choose a reason for hiding this comment

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

Could you make this type move-only and incorporate the close() operation into deinit?

Copy link
Member

Choose a reason for hiding this comment

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

Closing might involve closing FDs right? Which might be an asynchronous and throwing operation.

Choose a reason for hiding this comment

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

That close() can fail at all is an unfortunate weird corner of POSIX that I personally tend to ignore, because a failure in close() other than EINTR/EAGAIN is basically non-recoverable. What are you even supposed to do? What can a user do to fix the problem? Generally nothing.

So I just about always just drop a close() failure on the floor. </hottake>

(As for asynchronous, it's a blocking operation in userland but it can't fail to make forward progress in this case because there's no network I/O involved unless we're doing something really wonky.)

Copy link
Contributor

Choose a reason for hiding this comment

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

We've had this discussion in another place (here?). We ended up not calling close, but asserting that close has already been called in deinit.

But it does seem like a design people are going to reach for repeatedly. I wonder if we can put our thought process down somewhere.

Copy link
Member

Choose a reason for hiding this comment

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

That close() can fail at all is an unfortunate weird corner of POSIX that I personally tend to ignore, because a failure in close() other than EINTR/EAGAIN is basically non-recoverable. What are you even supposed to do? What can a user do to fix the problem? Generally nothing.

I agree that it is a weirdness; nevertheless, we need to handle it and most likely surface it to the user. We shouldn't just swallow those errors.

(As for asynchronous, it's a blocking operation in userland but it can't fail to make forward progress in this case because there's no network I/O involved unless we're doing something really wonky.)

This is not entirely true. If you are using io_uring you can asynchronously listen for the subprocess termination with pidfd and signalfd via io_uring. We need to account for changes in the underlying I/O system where closing can become asynchronous otherwise we will lock ourselves in a corner API-wise.

The only pattern that keeps us flexible is a with-style based approach.

@iCharlesHu
Copy link
Contributor Author

Added fallback implementation that uses signal handler on Linux 5.4 and below.

@iCharlesHu iCharlesHu force-pushed the charles/linux-pidfd-monitor branch from 1380d11 to 5c17f7b Compare July 23, 2025 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants