Skip to content

Commit d50e253

Browse files
committed
Add context cancel for epoll
Signed-off-by: Michael Crosby <[email protected]>
1 parent d89ba5e commit d50e253

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

runtime/v2/runc/epoll.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,21 @@ func (e *epoller) Close() error {
6262
func (e *epoller) run(ctx context.Context) {
6363
var events [128]unix.EpollEvent
6464
for {
65-
n, err := unix.EpollWait(e.fd, events[:], -1)
66-
if err != nil {
67-
if err == unix.EINTR {
68-
continue
65+
select {
66+
case <-ctx.Done():
67+
e.Close()
68+
return
69+
default:
70+
n, err := unix.EpollWait(e.fd, events[:], -1)
71+
if err != nil {
72+
if err == unix.EINTR {
73+
continue
74+
}
75+
logrus.WithError(err).Error("cgroups: epoll wait")
76+
}
77+
for i := 0; i < n; i++ {
78+
e.process(ctx, uintptr(events[i].Fd))
6979
}
70-
logrus.WithError(err).Error("cgroups: epoll wait")
71-
}
72-
for i := 0; i < n; i++ {
73-
e.process(ctx, uintptr(events[i].Fd))
7480
}
7581
}
7682
}

runtime/v2/runc/service.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func New(ctx context.Context, id string, publisher events.Publisher) (shim.Shim,
6868
if err != nil {
6969
return nil, err
7070
}
71+
ctx, cancel := context.WithCancel(ctx)
7172
go ep.run(ctx)
7273
s := &service{
7374
id: id,
@@ -76,10 +77,12 @@ func New(ctx context.Context, id string, publisher events.Publisher) (shim.Shim,
7677
events: make(chan interface{}, 128),
7778
ec: shim.Default.Subscribe(),
7879
ep: ep,
80+
cancel: cancel,
7981
}
8082
go s.processExits()
8183
runcC.Monitor = shim.Default
8284
if err := s.initPlatform(); err != nil {
85+
cancel()
8386
return nil, errors.Wrap(err, "failed to initialized platform behavior")
8487
}
8588
go s.forward(publisher)
@@ -101,6 +104,7 @@ type service struct {
101104
id string
102105
bundle string
103106
cg cgroups.Cgroup
107+
cancel func()
104108
}
105109

106110
func newCommand(ctx context.Context, containerdBinary, containerdAddress string) (*exec.Cmd, error) {
@@ -579,6 +583,7 @@ func (s *service) Connect(ctx context.Context, r *taskAPI.ConnectRequest) (*task
579583
}
580584

581585
func (s *service) Shutdown(ctx context.Context, r *taskAPI.ShutdownRequest) (*ptypes.Empty, error) {
586+
s.cancel()
582587
os.Exit(0)
583588
return empty, nil
584589
}
@@ -698,7 +703,10 @@ func (s *service) getContainerPids(ctx context.Context, id string) ([]uint32, er
698703

699704
func (s *service) forward(publisher events.Publisher) {
700705
for e := range s.events {
701-
if err := publisher.Publish(s.context, getTopic(s.context, e), e); err != nil {
706+
ctx, cancel := context.WithTimeout(s.context, 5*time.Second)
707+
err := publisher.Publish(ctx, getTopic(e), e)
708+
cancel()
709+
if err != nil {
702710
logrus.WithError(err).Error("post event")
703711
}
704712
}
@@ -722,7 +730,7 @@ func (s *service) setCgroup(cg cgroups.Cgroup) {
722730
}
723731
}
724732

725-
func getTopic(ctx context.Context, e interface{}) string {
733+
func getTopic(e interface{}) string {
726734
switch e.(type) {
727735
case *eventstypes.TaskCreate:
728736
return runtime.TaskCreateEventTopic

0 commit comments

Comments
 (0)