Skip to content

Commit

Permalink
Fix podman stop -t -1 CID
Browse files Browse the repository at this point in the history
Currently if a user specifies a negative time to stop a container the
code ends up specifying the negative time to time.Duration which treats
it as 0. By settine the default to max.Unint32 we end up with a positive
number which indicates > 68 years which is probably close enough to
infinity for our use case.

Fixes: containers#21811

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Feb 26, 2024
1 parent 04f7032 commit 5b28a2b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1332,10 +1332,10 @@ func ParseRestartPolicy(policy string) (string, uint, error) {
return policyType, retriesUint, nil
}

// ConvertTimeout converts negative timeout to MaxInt, which indicates approximately infinity, waiting to stop containers
// ConvertTimeout converts negative timeout to MaxUint32, which indicates approximately infinity, waiting to stop containers
func ConvertTimeout(timeout int) uint {
if timeout < 0 {
return math.MaxInt
return math.MaxUint32
}
return uint(timeout)
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"fmt"
"math"
"testing"
"time"

Expand Down Expand Up @@ -573,3 +574,17 @@ func TestConvertMappings(t *testing.T) {
assert.Equal(t, start[i].Size, convertedBack[i].Size)
}
}

func TestConvertTimeout(t *testing.T) {
timeout := ConvertTimeout(0)
assert.Equal(t, uint(0), timeout)

timeout = ConvertTimeout(100)
assert.Equal(t, uint(100), timeout)

timeout = ConvertTimeout(-1)
assert.Equal(t, uint(math.MaxUint32), timeout)

timeout = ConvertTimeout(-100)
assert.Equal(t, uint(math.MaxUint32), timeout)
}
9 changes: 2 additions & 7 deletions test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1079,20 +1079,15 @@ $IMAGE--c_ok" \
run_podman stop -t 0 $cid

# Actual test for 15895: with --systemd, no ttyN devices are passed through
run_podman run -d --privileged --systemd=always $IMAGE top
run_podman run -d --privileged --stop-signal=TERM --systemd=always $IMAGE top
cid="$output"

run_podman exec $cid sh -c "find /dev -regex '/dev/tty[0-9].*' | wc -w"
assert "$output" = "0" \
"ls /dev/tty[0-9] with --systemd=always: should have no ttyN devices"

# Make sure run_podman stop supports -1 option
# FIXME: do we really really mean to say FFFFFFFFFFFFFFFF here???
run_podman 0+w stop -t -1 $cid
if ! is_remote; then
require_warning "StopSignal \(37\) failed to stop container .* in 18446744073709551615 seconds, resorting to SIGKILL" \
"stop -t -1 (negative 1) issues warning"
fi
run_podman stop -t -1 $cid
run_podman rm -t -1 -f $cid
}

Expand Down

0 comments on commit 5b28a2b

Please sign in to comment.