Merge pull request #5021 from wzshiming/fix/signal_repeatedly

Fix repeated sending signal
This commit is contained in:
Mike Brown 2021-02-22 09:45:56 -06:00 committed by GitHub
commit 9173d3e929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View File

@ -17,6 +17,7 @@
package server package server
import ( import (
"sync/atomic"
"syscall" "syscall"
"time" "time"
@ -129,10 +130,23 @@ func (c *criService) stopContainer(ctx context.Context, container containerstore
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to parse stop signal %q", stopSignal) return errors.Wrapf(err, "failed to parse stop signal %q", stopSignal)
} }
var sswt bool
if container.IsStopSignaledWithTimeout == nil {
log.G(ctx).Infof("unable to ensure stop signal %v was not sent twice to container %v", sig, id)
sswt = true
} else {
sswt = atomic.CompareAndSwapUint32(container.IsStopSignaledWithTimeout, 0, 1)
}
if sswt {
log.G(ctx).Infof("Stop container %q with signal %v", id, sig) log.G(ctx).Infof("Stop container %q with signal %v", id, sig)
if err = task.Kill(ctx, sig); err != nil && !errdefs.IsNotFound(err) { if err = task.Kill(ctx, sig); err != nil && !errdefs.IsNotFound(err) {
return errors.Wrapf(err, "failed to stop container %q", id) return errors.Wrapf(err, "failed to stop container %q", id)
} }
} else {
log.G(ctx).Infof("Skipping the sending of signal %v to container %q because a prior stop with timeout>0 request already sent the signal", sig, id)
}
sigTermCtx, sigTermCtxCancel := context.WithTimeout(ctx, timeout) sigTermCtx, sigTermCtxCancel := context.WithTimeout(ctx, timeout)
defer sigTermCtxCancel() defer sigTermCtxCancel()

View File

@ -42,6 +42,9 @@ type Container struct {
IO *cio.ContainerIO IO *cio.ContainerIO
// StopCh is used to propagate the stop information of the container. // StopCh is used to propagate the stop information of the container.
*store.StopCh *store.StopCh
// IsStopSignaledWithTimeout the default is 0, and it is set to 1 after sending
// the signal once to avoid repeated sending of the signal.
IsStopSignaledWithTimeout *uint32
} }
// Opts sets specific information to newly created Container. // Opts sets specific information to newly created Container.
@ -83,6 +86,7 @@ func NewContainer(metadata Metadata, opts ...Opts) (Container, error) {
c := Container{ c := Container{
Metadata: metadata, Metadata: metadata,
StopCh: store.NewStopCh(), StopCh: store.NewStopCh(),
IsStopSignaledWithTimeout: new(uint32),
} }
for _, o := range opts { for _, o := range opts {
if err := o(&c); err != nil { if err := o(&c); err != nil {