summaryrefslogtreecommitdiff
path: root/libpod/container_internal.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2022-05-02 11:53:30 -0400
committerGitHub <noreply@github.com>2022-05-02 11:53:30 -0400
commitc3d871a3f6cc7a94c5e86782ba63e05cd1d2faeb (patch)
tree589ce895fa6ab9ab7a605ea5016307d9915c74a2 /libpod/container_internal.go
parentadf6ee671ff8111b3b1d1819a65fcc05e44589ef (diff)
parent4eff0c8cf284a6007122aec731e4d97059750166 (diff)
downloadpodman-c3d871a3f6cc7a94c5e86782ba63e05cd1d2faeb.tar.gz
podman-c3d871a3f6cc7a94c5e86782ba63e05cd1d2faeb.tar.bz2
podman-c3d871a3f6cc7a94c5e86782ba63e05cd1d2faeb.zip
Merge pull request #13859 from vrothberg/fix-13464
pod: add exit policies
Diffstat (limited to 'libpod/container_internal.go')
-rw-r--r--libpod/container_internal.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/libpod/container_internal.go b/libpod/container_internal.go
index 5c6719bdf..7494eb3ec 100644
--- a/libpod/container_internal.go
+++ b/libpod/container_internal.go
@@ -1939,9 +1939,51 @@ func (c *Container) cleanup(ctx context.Context) error {
}
}
+ if err := c.stopPodIfNeeded(context.Background()); err != nil {
+ if lastError == nil {
+ lastError = err
+ } else {
+ logrus.Errorf("Stopping pod of container %s: %v", c.ID(), err)
+ }
+ }
+
return lastError
}
+// If the container is part of a pod where only the infra container remains
+// running, attempt to stop the pod.
+func (c *Container) stopPodIfNeeded(ctx context.Context) error {
+ if c.config.Pod == "" {
+ return nil
+ }
+
+ pod, err := c.runtime.state.Pod(c.config.Pod)
+ if err != nil {
+ return fmt.Errorf("container %s is in pod %s, but pod cannot be retrieved: %w", c.ID(), c.config.Pod, err)
+ }
+
+ switch pod.config.ExitPolicy {
+ case config.PodExitPolicyContinue:
+ return nil
+
+ case config.PodExitPolicyStop:
+ // Use the runtime's work queue to stop the pod. This resolves
+ // a number of scenarios where we'd otherwise run into
+ // deadlocks. For instance, during `pod stop`, the pod has
+ // already been locked.
+ // The work queue is a simple means without having to worry about
+ // future changes that may introduce more deadlock scenarios.
+ c.runtime.queueWork(func() {
+ if err := pod.stopIfOnlyInfraRemains(ctx, c.ID()); err != nil {
+ if !errors.Is(err, define.ErrNoSuchPod) {
+ logrus.Errorf("Checking if infra needs to be stopped: %v", err)
+ }
+ }
+ })
+ }
+ return nil
+}
+
// delete deletes the container and runs any configured poststop
// hooks.
func (c *Container) delete(ctx context.Context) error {