stop.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package daemon
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/docker/docker/api/errors"
  9. containerpkg "github.com/docker/docker/container"
  10. )
  11. // ContainerStop looks for the given container and terminates it,
  12. // waiting the given number of seconds before forcefully killing the
  13. // container. If a negative number of seconds is given, ContainerStop
  14. // will wait for a graceful termination. An error is returned if the
  15. // container is not found, is already stopped, or if there is a
  16. // problem stopping the container.
  17. func (daemon *Daemon) ContainerStop(name string, seconds *int) error {
  18. container, err := daemon.GetContainer(name)
  19. if err != nil {
  20. return err
  21. }
  22. if !container.IsRunning() {
  23. err := fmt.Errorf("Container %s is already stopped", name)
  24. return errors.NewErrorWithStatusCode(err, http.StatusNotModified)
  25. }
  26. if seconds == nil {
  27. stopTimeout := container.StopTimeout()
  28. seconds = &stopTimeout
  29. }
  30. if err := daemon.containerStop(container, *seconds); err != nil {
  31. return fmt.Errorf("Cannot stop container %s: %v", name, err)
  32. }
  33. return nil
  34. }
  35. // containerStop halts a container by sending a stop signal, waiting for the given
  36. // duration in seconds, and then calling SIGKILL and waiting for the
  37. // process to exit. If a negative duration is given, Stop will wait
  38. // for the initial signal forever. If the container is not running Stop returns
  39. // immediately.
  40. func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds int) error {
  41. if !container.IsRunning() {
  42. return nil
  43. }
  44. daemon.stopHealthchecks(container)
  45. stopSignal := container.StopSignal()
  46. // 1. Send a stop signal
  47. if err := daemon.killPossiblyDeadProcess(container, stopSignal); err != nil {
  48. // While normally we might "return err" here we're not going to
  49. // because if we can't stop the container by this point then
  50. // it's probably because it's already stopped. Meaning, between
  51. // the time of the IsRunning() call above and now it stopped.
  52. // Also, since the err return will be environment specific we can't
  53. // look for any particular (common) error that would indicate
  54. // that the process is already dead vs something else going wrong.
  55. // So, instead we'll give it up to 2 more seconds to complete and if
  56. // by that time the container is still running, then the error
  57. // we got is probably valid and so we force kill it.
  58. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  59. defer cancel()
  60. if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
  61. logrus.Infof("Container failed to stop after sending signal %d to the process, force killing", stopSignal)
  62. if err := daemon.killPossiblyDeadProcess(container, 9); err != nil {
  63. return err
  64. }
  65. }
  66. }
  67. // 2. Wait for the process to exit on its own
  68. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(seconds)*time.Second)
  69. defer cancel()
  70. if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
  71. logrus.Infof("Container %v failed to exit within %d seconds of signal %d - using the force", container.ID, seconds, stopSignal)
  72. // 3. If it doesn't, then send SIGKILL
  73. if err := daemon.Kill(container); err != nil {
  74. // Wait without a timeout, ignore result.
  75. _ = <-container.Wait(context.Background(), containerpkg.WaitConditionNotRunning)
  76. logrus.Warn(err) // Don't return error because we only care that container is stopped, not what function stopped it
  77. }
  78. }
  79. daemon.LogContainerEvent(container, "stop")
  80. return nil
  81. }