Subprocess: Use pidfd (PIDFD_GET_INFO if supported) to monitor an imported process for exit status - #18922
Closed
tomponline wants to merge 15 commits into
Closed
Subprocess: Use pidfd (PIDFD_GET_INFO if supported) to monitor an imported process for exit status#18922tomponline wants to merge 15 commits into
tomponline wants to merge 15 commits into
Conversation
…hen a process exits On kernels that support PIDFD_GET_INFO it returns the process exit code, otherwise returns -1. Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
…r monitoring the process' exit status Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds imported-process monitoring so Stop waits for termination, supporting safe virtiofsd cleanup after daemon restarts.
Changes:
- Adds pidfd-based Linux monitoring with portable fallback.
- Shares monitoring between spawned and imported processes.
- Adds imported-process wait and exit-code tests.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
lxd/subprocess/procwait_other.go |
Adds polling fallback. |
lxd/subprocess/procwait_linux.go |
Adds pidfd monitoring and exit-status retrieval. |
lxd/subprocess/proc.go |
Introduces shared monitor lifecycle and safe persistence copy. |
lxd/subprocess/manager.go |
Starts monitors for live imported processes. |
lxd/subprocess/bgpm_test.go |
Adds imported-process monitoring tests. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| go func() { | ||
| defer close(chExit) | ||
|
|
||
| p.exitCode, p.exitErr = wait() |
Comment on lines
+113
to
+116
| code, err := waitProcess(context.Background(), pid, startTime) | ||
| if err != nil { | ||
| return -1, nil | ||
| } |
Comment on lines
+87
to
+89
| if n > 0 { | ||
| return pidfdExitCode(pidFd), nil | ||
| } |
| // PIDFD_GET_INFO ioctl, or -1 if the kernel does not support it or the process was signalled. | ||
| func pidfdExitCode(pidFd int) int64 { | ||
| info := pidfdInfo{mask: pidfdInfoExit} | ||
| req := (uintptr(iocDirWriteRead) << 30) | (unsafe.Sizeof(info) << 16) | (uintptr(pidfsIoctlMagic) << 8) | 11 |
Comment on lines
+448
to
+450
| if time.Since(started) < 300*time.Millisecond { | ||
| t.Error("Wait returned before the process exited; it did not block on the pidfd") | ||
| } |
Comment on lines
+438
to
+443
| // Wait must block on the pidfd until the process exits, then report its exit code. | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
|
|
||
| started := time.Now() | ||
| code, err := imp.Wait(ctx) |
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
To allow starting of a finished process. Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Suppressed comments (4)
Previously missed (3) — in code that hasn't changed since the last review.
lxd/subprocess/proc.go:297
- The monitor-owned fields are all unexported, so YAML already ignores them and
yaml.Marshal(p)cannot race onexitCode/exitErr. Manually duplicating every persisted field instead creates a second schema that can silently omit future exported fields. Keep marshaling the process directly unless a dedicated persisted type is needed for another reason.
// Marshal a copy of only the persisted fields. Marshalling the live process would have yaml
// read the whole struct (via reflect) and race with the monitor goroutine updating the
// exit state. The excluded fields are not persisted anyway.
saved := Process{
Name: p.Name,
Args: p.Args,
Apparmor: p.Apparmor,
PID: p.PID,
BootID: p.BootID,
UID: p.UID,
GID: p.GID,
SetGroups: p.SetGroups,
StartTime: p.StartTime,
SysProcAttr: p.SysProcAttr,
}
lxd/subprocess/procwait_other.go:12
- Use US English spelling: “canceled.”
// waitProcess blocks until the process identified by pid has exited, or ctx is cancelled.
lxd/subprocess/procwait_linux.go:55
- Use US English spelling: “canceled.”
// waitProcess blocks until the process identified by pid has exited, or ctx is cancelled.
lxd/subprocess/procwait_linux.go:116
- A readable pidfd only establishes that the task exited;
PIDFD_INFO_EXITis populated later when the parent reaps the task (pidfs_exitruns fromrelease_task). Calling the ioctl once atPOLLINtherefore races the parent and can return-1on a kernel that supports exit info. Wait for/retry through the reaping transition, with a bounded policy so an unreaping parent cannot hang this monitor.
if revents&unix.POLLIN != 0 {
return pidfdExitCode(pidFd), nil
Comment on lines
+112
to
+115
| // Spawn a monitor goroutine so a running imported process can be waited on like a spawned | ||
| // one. Only do so when the process is actually alive; the monitor records the exit code and | ||
| // starting it for an already-exited process would race with any later reuse of the object. | ||
| proc.monitorImported() |
Comment on lines
+162
to
+164
| if p.hasMonitor { | ||
| return ErrAlreadyRunning | ||
| } |
Comment on lines
80
to
+82
| if p.hasMonitor { | ||
| <-p.chExit | ||
| return | ||
| p.hasMonitor = false |
Comment on lines
+290
to
+292
| _, err = imp.Wait(ctxWait) | ||
| if errors.Is(err, context.DeadlineExceeded) { | ||
| t.Error("Imported Stop returned before the imported process exited") |
Comment on lines
+513
to
+516
| if code == -1 { | ||
| // The kernel lacks PIDFD_GET_INFO exit support; the wait still completed on exit. | ||
| t.Log("Kernel does not support PIDFD_GET_INFO; exit code unavailable") | ||
| return |
Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes
process.Stop()wait for the process to exit when the process has been imported, the same as it does for when the process has been started and stopped from the same parent process.This is related to #18918 which wraps virtiofsd in a userns (always) and requires that LXD waits for virtiofsd to stop (and the userns be cleared up) before the volume can be deleted.