Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/container_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
ctx, span := log.StartSpan(ctx)
defer span.End()
if !sb.Stopped() {
if err := s.stopContainer(ctx, c, int64(10)); err != nil {
if err := s.stopContainer(ctx, c, stopTimeoutFromContext(ctx)); err != nil {

Check warning on line 56 in server/container_remove.go

View check run for this annotation

Codecov / codecov/patch

server/container_remove.go#L56

Added line #L56 was not covered by tests
return fmt.Errorf("failed to stop container for removal %w", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/sandbox_run_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (s *Server) runPodSandbox(ctx context.Context, req *types.RunPodSandboxRequ
}
resourceCleaner.Add(ctx, "runSandbox: stopping container "+container.ID(), func() error {
// Clean-up steps from RemovePodSandbox
if err := s.stopContainer(ctx, container, int64(10)); err != nil {
if err := s.stopContainer(ctx, container, stopTimeoutFromContext(ctx)); err != nil {
return fmt.Errorf("failed to stop container for removal")
}

Expand Down
2 changes: 1 addition & 1 deletion server/sandbox_run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@
}
resourceCleaner.Add(ctx, "runSandbox: stopping container "+container.ID(), func() error {
// Clean-up steps from RemovePodSandbox
if err := s.stopContainer(ctx, container, int64(10)); err != nil {
if err := s.stopContainer(ctx, container, stopTimeoutFromContext(ctx)); err != nil {

Check warning on line 1000 in server/sandbox_run_linux.go

View check run for this annotation

Codecov / codecov/patch

server/sandbox_run_linux.go#L1000

Added line #L1000 was not covered by tests
return errors.New("failed to stop container for removal")
}

Expand Down
4 changes: 2 additions & 2 deletions server/sandbox_stop_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *Server) stopPodSandbox(ctx context.Context, sb *sandbox.Sandbox) error
}
c := ctr
waitGroup.Go(func() error {
if err := s.stopContainer(ctx, c, int64(10)); err != nil {
if err := s.stopContainer(ctx, c, stopTimeoutFromContext(ctx)); err != nil {
return fmt.Errorf("failed to stop container for pod sandbox %s: %v", sb.ID(), err)
}
if err := s.nri.stopContainer(ctx, sb, c); err != nil {
Expand All @@ -76,7 +76,7 @@ func (s *Server) stopPodSandbox(ctx context.Context, sb *sandbox.Sandbox) error
}
}

if err := s.stopContainer(ctx, podInfraContainer, int64(10)); err != nil && !errors.Is(err, storage.ErrContainerUnknown) && !errors.Is(err, oci.ErrContainerStopped) {
if err := s.stopContainer(ctx, podInfraContainer, stopTimeoutFromContext(ctx)); err != nil && !errors.Is(err, storage.ErrContainerUnknown) && !errors.Is(err, oci.ErrContainerStopped) {
return fmt.Errorf("failed to stop infra container for pod sandbox %s: %v", sb.ID(), err)
}

Expand Down
4 changes: 2 additions & 2 deletions server/sandbox_stop_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
}
c := ctr
waitGroup.Go(func() error {
if err := s.stopContainer(ctx, c, int64(10)); err != nil {
if err := s.stopContainer(ctx, c, stopTimeoutFromContext(ctx)); err != nil {

Check warning on line 64 in server/sandbox_stop_linux.go

View check run for this annotation

Codecov / codecov/patch

server/sandbox_stop_linux.go#L64

Added line #L64 was not covered by tests
return fmt.Errorf("failed to stop container for pod sandbox %s: %w", sb.ID(), err)
}
return nil
Expand All @@ -73,7 +73,7 @@
}
}

if err := s.stopContainer(ctx, podInfraContainer, int64(10)); err != nil && !errors.Is(err, storage.ErrContainerUnknown) {
if err := s.stopContainer(ctx, podInfraContainer, stopTimeoutFromContext(ctx)); err != nil && !errors.Is(err, storage.ErrContainerUnknown) {

Check warning on line 76 in server/sandbox_stop_linux.go

View check run for this annotation

Codecov / codecov/patch

server/sandbox_stop_linux.go#L76

Added line #L76 was not covered by tests
return fmt.Errorf("failed to stop infra container for pod sandbox %s: %w", sb.ID(), err)
}

Expand Down
17 changes: 17 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

const (
maxLabelSize = 4096

// defaultStopTimeout is the default container stop timeout in seconds.
defaultStopTimeout = 10
)

func validateLabels(labels map[string]string) error {
Expand Down Expand Up @@ -223,3 +226,17 @@

return s.config.Workloads.FilterDisallowedAnnotations(allowed, toFilter)
}

// stopTimeoutFromContext returns the stop timeout in seconds for the provided
// context. If the context has no timeout or deadline set, then it will default

Check warning on line 231 in server/utils.go

View check run for this annotation

Codecov / codecov/patch

server/utils.go#L229-L231

Added lines #L229 - L231 were not covered by tests
// to 10s.
func stopTimeoutFromContext(ctx context.Context) int64 {
timeout := int64(defaultStopTimeout)
deadline, ok := ctx.Deadline()

Check warning on line 235 in server/utils.go

View check run for this annotation

Codecov / codecov/patch

server/utils.go#L234-L235

Added lines #L234 - L235 were not covered by tests
if ok {
timeout = time.Until(deadline).Milliseconds() / 1000
}

log.Debugf(ctx, "Using stop timeout: %v", timeout)
return timeout
}
1 change: 1 addition & 0 deletions test/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ PINNS_BINARY_PATH=${PINNS_BINARY_PATH:-${CRIO_ROOT}/bin/pinns}

# Path of the crictl binary.
CRICTL_BINARY=${CRICTL_BINARY:-$(command -v crictl)}
CRICTL_TIMEOUT=${CRICTL_TIMEOUT:-30s}
# Path of the conmon binary set as a variable to allow overwriting.
CONMON_BINARY=${CONMON_BINARY:-$(command -v conmon)}
# Cgroup for the conmon process
Expand Down
4 changes: 2 additions & 2 deletions test/ctr.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1427,8 +1427,8 @@ EOF
ctr_id=$(crictl create "$pod_id" "$TESTDATA"/container_sleep.json "$TESTDATA"/sandbox_config.json)

crictl start "$ctr_id"
crictl stopp "$pod_id"
crictl rmp "$pod_id"
CRICTL_TIMEOUT=10m crictl stop -t 10 "$ctr_id"
crictl rmp -f "$pod_id"

grep -q "Stopping container ${ctr_id} with stop signal timed out." "$CRIO_LOG"

Expand Down
2 changes: 1 addition & 1 deletion test/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function crio() {

# Run crictl using the binary specified by $CRICTL_BINARY.
function crictl() {
"$CRICTL_BINARY" -t 10m --config "$CRICTL_CONFIG_FILE" -r "unix://$CRIO_SOCKET" -i "unix://$CRIO_SOCKET" "$@"
"$CRICTL_BINARY" -t "$CRICTL_TIMEOUT" --config "$CRICTL_CONFIG_FILE" -r "unix://$CRIO_SOCKET" -i "unix://$CRIO_SOCKET" "$@"
}

# Run the runtime binary with the specified RUNTIME_ROOT
Expand Down
10 changes: 10 additions & 0 deletions test/pod.bats
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ EOF
crictl rmp "$pod_id"
}

@test "pod remove with timeout from context" {
start_crio
pod_id=$(crictl runp "$TESTDATA"/sandbox_config.json)
# the sleep command in the container needs to be killed within the deadline
# of the context passed down from crictl
ctr_id=$(crictl create "$pod_id" "$TESTDATA"/container_sleep.json "$TESTDATA"/sandbox_config.json)
crictl start "$ctr_id"
CRICTL_TIMEOUT=5s crictl rmp -f "$pod_id"
}

@test "pod stop ignores not found sandboxes" {
start_crio
pod_id=$(crictl runp "$TESTDATA"/sandbox_config.json)
Expand Down
Loading