Skip to content

Add --pidfile flag to podman start#28983

Open
nomarek wants to merge 1 commit into
podman-container-tools:mainfrom
nomarek:start-pidfile
Open

Add --pidfile flag to podman start#28983
nomarek wants to merge 1 commit into
podman-container-tools:mainfrom
nomarek:start-pidfile

Conversation

@nomarek

@nomarek nomarek commented Jun 19, 2026

Copy link
Copy Markdown

podman create and podman run already have --pidfile, but the path is fixed at create time and reused on every start. Toolbx wants a fresh path per start so it can read the PID of the container's main process and wait on it before running podman exec. This adds the same --pidfile flag to podman start.

After the container starts, its PID is read with libpod's Container.PID() and written to the given path. As on create/run, the flag only works for a single container, is rejected with --attach, and is hidden on the remote client.

Tests: new e2e specs check that the PID written to the file matches .State.Pid, both for a freshly started and for an already-running container, plus error cases for starting more than one container (by argument and by --filter) and for --attach.

Checklist

  • Certify you wrote the patch or otherwise have the right to pass it on as an open-source patch by signing all commits. (git commit -s).
  • Referenced issues using Fixes: #00000 in commit message (if applicable)
  • Tests have been added/updated (or no tests are needed)
  • Documentation has been updated (or no documentation changes are needed)
  • All commits pass make validatepr (format/lint checks)
  • Release note entered in the section below (or None if no user-facing changes)

Does this PR introduce a user-facing change?

Added a `--pidfile` option to `podman start` that writes the started container's PID to the given path (not supported with the remote client).

@nomarek nomarek force-pushed the start-pidfile branch 2 times, most recently from 9c49a69 to 5ed9c08 Compare June 19, 2026 10:14

@Honny1 Honny1 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.

Thanks! I have some comments.

Comment on lines +38 to +39
Unlike **podman create** and **podman run**, the path is not stored in the
container configuration and is not reported by `podman inspect`.

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.

I would mention that in pidfile.md.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Moved it into pidfile.md so it sits with the shared option.

Comment thread pkg/domain/infra/abi/containers.go Outdated
return fmt.Errorf("retrieving PID of container %q: %w", c.ID(), err)
}
if pid == 0 {
return fmt.Errorf("container %q is no longer running, cannot write pidfile", c.ID())

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.

Suggested change
return fmt.Errorf("container %q is no longer running, cannot write pidfile", c.ID())
return fmt.Errorf("container %q exited before its PID could be written to the pidfile", c.ID())

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done, went with your wording.

Comment thread test/e2e/start_test.go Outdated

content, err := os.ReadFile(pidfile)
Expect(err).ToNot(HaveOccurred())
filePID, err := strconv.Atoi(strings.Split(string(content), "\n")[0])

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.

CreateIDFile doesn't append a newline.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Dropped the split, just parsing the bare PID now.

Comment thread test/e2e/start_test.go Outdated

It("podman start --pidfile with --attach fails", func() {
pidfile := filepath.Join(tempdir, "start-pidfile")
session := podmanTest.Podman([]string{"create", ALPINE, "top"})

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.

Please use PodmanExitCleanly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Switched the setup calls over to PodmanExitCleanly.


@@option pidfile

Unlike **podman create** and **podman run**, the path is not stored in the

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.

If this is the case, and I do a podman create --pidfile then a podman start pidfile, will podman inspect show the wrong pidfile value?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

start --pidfile doesn't touch the stored config, so inspect keeps showing the create-time path (or the default), not the one passed to start. So it's not a wrong value, it just won't reflect the start path. I spelled that out in pidfile.md now.

@ashley-cui

Copy link
Copy Markdown
Contributor

Please squash your commits once you are ready for merge.

create and run already take --pidfile, but the path is fixed at create
time and reused on every start. Toolbx wants a fresh path per start so it
can read the PID of the container's main process and wait on it before
running podman exec.

Add the same flag to start. Once the container is running we read its PID
with Container.PID() and write it to the requested path. It only works for
a single container, is rejected together with --attach, and is hidden on
the remote client, the same as create and run.

Fixes: podman-container-tools#25849
Signed-off-by: Marek Nogacki <no.marek@gmail.com>
@nomarek

nomarek commented Jun 23, 2026

Copy link
Copy Markdown
Author

Squashed it down to a single commit.

@mheon

mheon commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

@Luap99 I seem to remember you being opposed to this approach at some point in the past - am I misremembering?

@Luap99

Luap99 commented Jun 23, 2026

Copy link
Copy Markdown
Member

see the linked issue #25849

I am not exactly a fan of this, if others see a strong need for this sure but looking at the code this is super racy which I would oppose.

Comment on lines +1044 to +1050
pid, err := c.PID()
if err != nil {
return fmt.Errorf("retrieving PID of container %q: %w", c.ID(), err)
}
if pid == 0 {
return fmt.Errorf("container %q exited before its PID could be written to the pidfile", c.ID())
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well that is to some extend just not good design, we should not expose such issues to user and you do not really prevent the other problem of the container could have been restarted in between meaning the pid is different from the command which actually started the container.

Because this is doing c.PID() means anything between the actual start and that anything can happen. The better design would be to really go in the internals and make the start code return the pid from the time when it was actually started while we gold the proper lock to avoid the concurrent modification situation.

@packit-as-a-service

Copy link
Copy Markdown

[NON-BLOCKING] Packit jobs failed. @containers/packit-build please check. Everyone else, feel free to ignore.

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.

5 participants