Skip to content

Commit dc6f5ef

Browse files
committed
Fix repeated sending signal
Signed-off-by: Shiming Zhang <[email protected]>
1 parent ddcc431 commit dc6f5ef

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

pkg/cri/server/container_stop.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package server
1818

1919
import (
20+
"sync/atomic"
2021
"syscall"
2122
"time"
2223

@@ -129,9 +130,22 @@ func (c *criService) stopContainer(ctx context.Context, container containerstore
129130
if err != nil {
130131
return errors.Wrapf(err, "failed to parse stop signal %q", stopSignal)
131132
}
132-
log.G(ctx).Infof("Stop container %q with signal %v", id, sig)
133-
if err = task.Kill(ctx, sig); err != nil && !errdefs.IsNotFound(err) {
134-
return errors.Wrapf(err, "failed to stop container %q", id)
133+
134+
var sswt bool
135+
if container.IsStopSignaledWithTimeout == nil {
136+
log.G(ctx).Infof("unable to ensure stop signal %v was not sent twice to container %v", sig, id)
137+
sswt = true
138+
} else {
139+
sswt = atomic.CompareAndSwapUint32(container.IsStopSignaledWithTimeout, 0, 1)
140+
}
141+
142+
if sswt {
143+
log.G(ctx).Infof("Stop container %q with signal %v", id, sig)
144+
if err = task.Kill(ctx, sig); err != nil && !errdefs.IsNotFound(err) {
145+
return errors.Wrapf(err, "failed to stop container %q", id)
146+
}
147+
} else {
148+
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)
135149
}
136150

137151
sigTermCtx, sigTermCtxCancel := context.WithTimeout(ctx, timeout)

pkg/cri/store/container/container.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type Container struct {
4242
IO *cio.ContainerIO
4343
// StopCh is used to propagate the stop information of the container.
4444
*store.StopCh
45+
// IsStopSignaledWithTimeout the default is 0, and it is set to 1 after sending
46+
// the signal once to avoid repeated sending of the signal.
47+
IsStopSignaledWithTimeout *uint32
4548
}
4649

4750
// Opts sets specific information to newly created Container.
@@ -81,8 +84,9 @@ func WithStatus(status Status, root string) Opts {
8184
// NewContainer creates an internally used container type.
8285
func NewContainer(metadata Metadata, opts ...Opts) (Container, error) {
8386
c := Container{
84-
Metadata: metadata,
85-
StopCh: store.NewStopCh(),
87+
Metadata: metadata,
88+
StopCh: store.NewStopCh(),
89+
IsStopSignaledWithTimeout: new(uint32),
8690
}
8791
for _, o := range opts {
8892
if err := o(&c); err != nil {

0 commit comments

Comments
 (0)