Skip to content

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
canonical:mainfrom
tomponline:tp-subprocess-wait
Closed

Subprocess: Use pidfd (PIDFD_GET_INFO if supported) to monitor an imported process for exit status#18922
tomponline wants to merge 15 commits into
canonical:mainfrom
tomponline:tp-subprocess-wait

Conversation

@tomponline

Copy link
Copy Markdown
Member

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.

…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>
@tomponline tomponline self-assigned this Aug 25, 2026
@tomponline
tomponline requested a balanced review from Copilot August 25, 2026 15:54
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread lxd/subprocess/proc.go
go func() {
defer close(chExit)

p.exitCode, p.exitErr = wait()
Comment thread lxd/subprocess/proc.go
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
}
Comment thread lxd/subprocess/procwait_linux.go Outdated
// 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 thread lxd/subprocess/bgpm_test.go Outdated
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 thread lxd/subprocess/bgpm_test.go Outdated
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 on exitCode/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_EXIT is populated later when the parent reaps the task (pidfs_exit runs from release_task). Calling the ioctl once at POLLIN therefore races the parent and can return -1 on 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 thread lxd/subprocess/manager.go
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 thread lxd/subprocess/proc.go
Comment on lines +162 to +164
if p.hasMonitor {
return ErrAlreadyRunning
}
Comment thread lxd/subprocess/proc.go
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>
@tomponline tomponline closed this Sep 2, 2026
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.

2 participants