@@ -34,7 +34,7 @@ func (daemon *Daemon) ContainerStop(name string, timeout *int) error {
3434}
3535
3636// containerStop sends a stop signal, waits, sends a kill signal.
37- func (daemon * Daemon ) containerStop (ctr * container.Container , seconds * int ) error {
37+ func (daemon * Daemon ) containerStop (ctr * container.Container , seconds * int ) ( retErr error ) {
3838 // TODO propagate a context down to this function
3939 ctx := context .TODO ()
4040 if ! ctr .IsRunning () {
@@ -53,10 +53,11 @@ func (daemon *Daemon) containerStop(ctr *container.Container, seconds *int) erro
5353 if stopTimeout >= 0 {
5454 wait = time .Duration (stopTimeout ) * time .Second
5555 }
56- success := func () error {
57- daemon .LogContainerEvent (ctr , "stop" )
58- return nil
59- }
56+ defer func () {
57+ if retErr == nil {
58+ daemon .LogContainerEvent (ctr , "stop" )
59+ }
60+ }()
6061
6162 // 1. Send a stop signal
6263 err := daemon .killPossiblyDeadProcess (ctr , stopSignal )
@@ -75,7 +76,7 @@ func (daemon *Daemon) containerStop(ctr *container.Container, seconds *int) erro
7576
7677 if status := <- ctr .Wait (subCtx , container .WaitConditionNotRunning ); status .Err () == nil {
7778 // container did exit, so ignore any previous errors and return
78- return success ()
79+ return nil
7980 }
8081
8182 if err != nil {
@@ -89,18 +90,19 @@ func (daemon *Daemon) containerStop(ctr *container.Container, seconds *int) erro
8990 }
9091
9192 logrus .WithField ("container" , ctr .ID ).Infof ("Container failed to exit within %s of signal %d - using the force" , wait , stopSignal )
92- // Stop either failed or container didnt exit, so fallback to kill.
93+
94+ // Stop either failed or container didn't exit, so fallback to kill.
9395 if err := daemon .Kill (ctr ); err != nil {
9496 // got a kill error, but give container 2 more seconds to exit just in case
9597 subCtx , cancel := context .WithTimeout (ctx , 2 * time .Second )
9698 defer cancel ()
97- if status := <- ctr .Wait (subCtx , container .WaitConditionNotRunning ); status .Err () == nil {
98- // container did exit, so ignore error and return
99- return success ()
99+ status := <- ctr .Wait (subCtx , container .WaitConditionNotRunning )
100+ if status .Err () != nil {
101+ logrus .WithError (err ).WithField ("container" , ctr .ID ).Errorf ("error killing container: %v" , status .Err ())
102+ return err
100103 }
101- logrus .WithError (err ).WithField ("container" , ctr .ID ).Error ("Error killing the container" )
102- return err
104+ // container did exit, so ignore previous errors and continue
103105 }
104106
105- return success ()
107+ return nil
106108}
0 commit comments