Skip to content

Fix unless-stopped containers not restarting after podman-restart-service stop them#28236

Open
Honny1 wants to merge 1 commit intocontainers:mainfrom
Honny1:fix-unless-stopped
Open

Fix unless-stopped containers not restarting after podman-restart-service stop them#28236
Honny1 wants to merge 1 commit intocontainers:mainfrom
Honny1:fix-unless-stopped

Conversation

@Honny1
Copy link
Member

@Honny1 Honny1 commented Mar 10, 2026

When podman-restart.service stops containers, it marks them as "stopped by user" which breaks the unless-stopped restart policy. Add hidden --not-stopped-by-user flag to prevent this, allowing unless-stopped containers to restart on next boot.

Fixes: #28152
Fixes: https://issues.redhat.com/browse/RUN-4357

Checklist

Ensure you have completed the following checklist for your pull request to be reviewed:

  • 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). (If needed, use git commit -s --amend). The author email must match
    the sign-off email address. See CONTRIBUTING.md
    for more information.
  • 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?

Fixed an issue where containers with `unless-stopped` were incorrectly marked as `user-stopped` by `podman-restart.service`, preventing restart on next boot, by introducing an internal `--not-stopped-by-user` path and adding supporting tests.

@Honny1
Copy link
Member Author

Honny1 commented Mar 10, 2026

Note: This fix must be backported into the v5.8 branch.

@Honny1 Honny1 force-pushed the fix-unless-stopped branch from a17cb8d to e7f7ead Compare March 10, 2026 11:05
@Honny1 Honny1 marked this pull request as ready for review March 10, 2026 11:34
@Honny1 Honny1 marked this pull request as draft March 10, 2026 11:59
@Honny1 Honny1 force-pushed the fix-unless-stopped branch from e7f7ead to faab7cb Compare March 10, 2026 13:30
if registry.IsRemote() {
_ = flags.MarkHidden("cidfile")
_ = flags.MarkHidden("ignore")
_ = flags.MarkHidden(notStoppedByUserFlagName)
Copy link
Member

Choose a reason for hiding this comment

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

if it is already hidden no point to do it here again

Comment on lines +1399 to +1401
if !notStoppedByUser {
c.state.StoppedByUser = true
}
Copy link
Member

Choose a reason for hiding this comment

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

Does this work? If I have restart always does this not mean that with the new flag the cleanup process will keep restarting the container even if that is not what we want, i.e. see logic in shouldRestart() will be triggered in podman container cleanup I think

Copy link
Member Author

Choose a reason for hiding this comment

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

No, adding --not-stopped-by-user does not cause cleanup to keep restarting containers in the normal stop flow, even with --restart=always, because the stop path transitions through Stopping state and therefore does not set RestartPolicyMatch=true.

I am not sure whether this is a bug, but when Podman calls stopInternal, the container will not restart, even if the restart policy is always. I will add comments to clarify this.

Copy link
Member

Choose a reason for hiding this comment

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

We might want a stronger protection here because I can see this getting broken by future changes to how stop works?

@Honny1 Honny1 force-pushed the fix-unless-stopped branch from faab7cb to 0ad1c8a Compare March 10, 2026 18:49
…service` stop them

When `podman-restart.service` stops containers, it marks them as "stopped by
user" which breaks the `unless-stopped` restart policy. Add hidden
`--not-stopped-by-user` flag to prevent this, allowing `unless-stopped`
containers to restart on next boot.

Fixes: containers#28152
Fixes: https://issues.redhat.com/browse/RUN-4357

Signed-off-by: Jan Rodák <hony.com@seznam.cz>
@Honny1 Honny1 force-pushed the fix-unless-stopped branch from 0ad1c8a to 1841881 Compare March 10, 2026 19:41
@packit-as-a-service
Copy link

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

@Honny1 Honny1 marked this pull request as ready for review March 10, 2026 22:04
@TomSweeneyRedHat
Copy link
Member

LGTM
but I'd like a final head nod from @Luap99

@Honny1
Copy link
Member Author

Honny1 commented Mar 13, 2026

PTAL @containers/podman-maintainers

@mheon
Copy link
Member

mheon commented Mar 13, 2026

My first reaction is that I really hate the API change; StoppedByUser isn't really something we should be exposing, even as a hidden flag... I don't know if I have a better way, though.

// An explicit stop is treated as a user-driven lifecycle action. Because of
// that, this path may not trigger automatic restart-policy handling in cleanup,
// even when notStoppedByUser is true.
func (c *Container) StopWithArgs(timeout uint, notStoppedByUser bool) (finalErr error) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think there's much value in exposing notStoppedByUser as an argument. If we do this, I think we have a StopService() that calls stopInternal with stoppedByUser = false


// Internal, non-locking function to stop container
// notStoppedByUser controls whether to set the StoppedByUser state field.
func (c *Container) stopInternal(timeout uint, notStoppedByUser bool) error {
Copy link
Member

Choose a reason for hiding this comment

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

Don't do notStoppedByUser - confusing to negate. stoppedByUser is less confusing.

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.

podman-restart.service stop containers when systemd service is running

4 participants