Skip to content

Commit b85799b

Browse files
authored
Merge pull request moby#36874 from kolyshkin/stop-timeout
daemon.ContainerStop(): fix for a negative timeout
2 parents cbbe0ff + 69b4fe4 commit b85799b

File tree

5 files changed

+86
-23
lines changed

5 files changed

+86
-23
lines changed

client/container_stop.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import (
88
timetypes "github.com/docker/docker/api/types/time"
99
)
1010

11-
// ContainerStop stops a container without terminating the process.
12-
// The process is blocked until the container stops or the timeout expires.
11+
// ContainerStop stops a container. In case the container fails to stop
12+
// gracefully within a time frame specified by the timeout argument,
13+
// it is forcefully terminated (killed).
14+
//
15+
// If the timeout is nil, the container's StopTimeout value is used, if set,
16+
// otherwise the engine default. A negative timeout value can be specified,
17+
// meaning no timeout, i.e. no forceful termination is performed.
1318
func (cli *Client) ContainerStop(ctx context.Context, containerID string, timeout *time.Duration) error {
1419
query := url.Values{}
1520
if timeout != nil {

container/container_unit_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ func TestContainerStopTimeout(t *testing.T) {
5252
c = &Container{
5353
Config: &container.Config{StopTimeout: &stopTimeout},
5454
}
55-
s = c.StopSignal()
56-
if s != 15 {
57-
t.Fatalf("Expected 15, got %v", s)
55+
s = c.StopTimeout()
56+
if s != stopTimeout {
57+
t.Fatalf("Expected %v, got %v", stopTimeout, s)
5858
}
5959
}
6060

container/container_unix.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
)
2424

2525
const (
26-
// DefaultStopTimeout is the timeout (in seconds) for the syscall signal used to stop a container.
26+
// DefaultStopTimeout sets the default time, in seconds, to wait
27+
// for the graceful container stop before forcefully terminating it.
2728
DefaultStopTimeout = 10
2829

2930
containerSecretMountPath = "/run/secrets"

daemon/stop.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,33 @@ import (
1010
"github.com/sirupsen/logrus"
1111
)
1212

13-
// ContainerStop looks for the given container and terminates it,
14-
// waiting the given number of seconds before forcefully killing the
15-
// container. If a negative number of seconds is given, ContainerStop
16-
// will wait for a graceful termination. An error is returned if the
17-
// container is not found, is already stopped, or if there is a
18-
// problem stopping the container.
19-
func (daemon *Daemon) ContainerStop(name string, seconds *int) error {
13+
// ContainerStop looks for the given container and stops it.
14+
// In case the container fails to stop gracefully within a time duration
15+
// specified by the timeout argument, in seconds, it is forcefully
16+
// terminated (killed).
17+
//
18+
// If the timeout is nil, the container's StopTimeout value is used, if set,
19+
// otherwise the engine default. A negative timeout value can be specified,
20+
// meaning no timeout, i.e. no forceful termination is performed.
21+
func (daemon *Daemon) ContainerStop(name string, timeout *int) error {
2022
container, err := daemon.GetContainer(name)
2123
if err != nil {
2224
return err
2325
}
2426
if !container.IsRunning() {
2527
return containerNotModifiedError{running: false}
2628
}
27-
if seconds == nil {
29+
if timeout == nil {
2830
stopTimeout := container.StopTimeout()
29-
seconds = &stopTimeout
31+
timeout = &stopTimeout
3032
}
31-
if err := daemon.containerStop(container, *seconds); err != nil {
33+
if err := daemon.containerStop(container, *timeout); err != nil {
3234
return errdefs.System(errors.Wrapf(err, "cannot stop container: %s", name))
3335
}
3436
return nil
3537
}
3638

37-
// containerStop halts a container by sending a stop signal, waiting for the given
38-
// duration in seconds, and then calling SIGKILL and waiting for the
39-
// process to exit. If a negative duration is given, Stop will wait
40-
// for the initial signal forever. If the container is not running Stop returns
41-
// immediately.
39+
// containerStop sends a stop signal, waits, sends a kill signal.
4240
func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds int) error {
4341
if !container.IsRunning() {
4442
return nil
@@ -69,8 +67,12 @@ func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds i
6967
}
7068

7169
// 2. Wait for the process to exit on its own
72-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(seconds)*time.Second)
73-
defer cancel()
70+
ctx := context.Background()
71+
if seconds >= 0 {
72+
var cancel context.CancelFunc
73+
ctx, cancel = context.WithTimeout(ctx, time.Duration(seconds)*time.Second)
74+
defer cancel()
75+
}
7476

7577
if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
7678
logrus.Infof("Container %v failed to exit within %d seconds of signal %d - using the force", container.ID, seconds, stopSignal)

integration/container/stop_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package container // import "github.com/docker/docker/integration/container"
33
import (
44
"context"
55
"fmt"
6+
"strconv"
67
"strings"
78
"testing"
89
"time"
@@ -42,6 +43,60 @@ func TestStopContainerWithRestartPolicyAlways(t *testing.T) {
4243
}
4344
}
4445

46+
// TestStopContainerWithTimeout checks that ContainerStop with
47+
// a timeout works as documented, i.e. in case of negative timeout
48+
// waiting is not limited (issue #35311).
49+
func TestStopContainerWithTimeout(t *testing.T) {
50+
defer setupTest(t)()
51+
client := request.NewAPIClient(t)
52+
ctx := context.Background()
53+
54+
testCmd := container.WithCmd("sh", "-c", "sleep 2 && exit 42")
55+
testData := []struct {
56+
doc string
57+
timeout int
58+
expectedExitCode int
59+
}{
60+
// In case container is forcefully killed, 137 is returned,
61+
// otherwise the exit code from the above script
62+
{
63+
"zero timeout: expect forceful container kill",
64+
0, 137,
65+
},
66+
{
67+
"too small timeout: expect forceful container kill",
68+
1, 137,
69+
},
70+
{
71+
"big enough timeout: expect graceful container stop",
72+
3, 42,
73+
},
74+
{
75+
"unlimited timeout: expect graceful container stop",
76+
-1, 42,
77+
},
78+
}
79+
80+
for _, d := range testData {
81+
d := d
82+
t.Run(strconv.Itoa(d.timeout), func(t *testing.T) {
83+
t.Parallel()
84+
id := container.Run(t, ctx, client, testCmd)
85+
86+
timeout := time.Duration(d.timeout) * time.Second
87+
err := client.ContainerStop(ctx, id, &timeout)
88+
assert.NilError(t, err)
89+
90+
poll.WaitOn(t, container.IsStopped(ctx, client, id),
91+
poll.WithDelay(100*time.Millisecond))
92+
93+
inspect, err := client.ContainerInspect(ctx, id)
94+
assert.NilError(t, err)
95+
assert.Equal(t, inspect.State.ExitCode, d.expectedExitCode)
96+
})
97+
}
98+
}
99+
45100
func TestDeleteDevicemapper(t *testing.T) {
46101
skip.If(t, testEnv.DaemonInfo.Driver != "devicemapper")
47102
skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")

0 commit comments

Comments
 (0)