@@ -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
106110func 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
581585func (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
699704func (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