@@ -10,35 +10,33 @@ import (
10
10
"github.com/sirupsen/logrus"
11
11
)
12
12
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 {
20
22
container , err := daemon .GetContainer (name )
21
23
if err != nil {
22
24
return err
23
25
}
24
26
if ! container .IsRunning () {
25
27
return containerNotModifiedError {running : false }
26
28
}
27
- if seconds == nil {
29
+ if timeout == nil {
28
30
stopTimeout := container .StopTimeout ()
29
- seconds = & stopTimeout
31
+ timeout = & stopTimeout
30
32
}
31
- if err := daemon .containerStop (container , * seconds ); err != nil {
33
+ if err := daemon .containerStop (container , * timeout ); err != nil {
32
34
return errdefs .System (errors .Wrapf (err , "cannot stop container: %s" , name ))
33
35
}
34
36
return nil
35
37
}
36
38
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.
42
40
func (daemon * Daemon ) containerStop (container * containerpkg.Container , seconds int ) error {
43
41
if ! container .IsRunning () {
44
42
return nil
@@ -69,8 +67,12 @@ func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds i
69
67
}
70
68
71
69
// 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
+ }
74
76
75
77
if status := <- container .Wait (ctx , containerpkg .WaitConditionNotRunning ); status .Err () != nil {
76
78
logrus .Infof ("Container %v failed to exit within %d seconds of signal %d - using the force" , container .ID , seconds , stopSignal )
0 commit comments