From 8ca92a2aa8154fca60bd947a5adb7eb11cf1fd68 Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Fri, 19 May 2017 15:31:18 -0700 Subject: [PATCH] Ensure shim start & exit events are sent in right order Signed-off-by: Kenfe-Mickael Laventure --- linux/shim/init.go | 9 +++++++++ linux/shim/service.go | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/linux/shim/init.go b/linux/shim/init.go index a5a9a6498..6b07a4c59 100644 --- a/linux/shim/init.go +++ b/linux/shim/init.go @@ -23,6 +23,11 @@ import ( type initProcess struct { sync.WaitGroup + // mu is used to ensure that `Start()` and `Exited()` calls return in + // the right order when invoked in separate go routines. + // This is the case within the shim implementation as it makes use of + // the reaper interface. + mu sync.Mutex id string bundle string console console.Console @@ -139,12 +144,16 @@ func (p *initProcess) ContainerStatus(ctx context.Context) (string, error) { } func (p *initProcess) Start(context context.Context) error { + p.mu.Lock() + defer p.mu.Unlock() return p.runc.Start(context, p.id) } func (p *initProcess) Exited(status int) { + p.mu.Lock() p.status = status p.exited = time.Now() + p.mu.Unlock() } func (p *initProcess) Delete(context context.Context) error { diff --git a/linux/shim/service.go b/linux/shim/service.go index a8a857f33..5a3eb40a1 100644 --- a/linux/shim/service.go +++ b/linux/shim/service.go @@ -56,12 +56,12 @@ func (s *Service) Create(ctx context.Context, r *shimapi.CreateRequest) (*shimap ExitCh: make(chan int, 1), } reaper.Default.Register(pid, cmd) - go s.waitExit(process, pid, cmd) s.events <- &container.Event{ Type: container.Event_CREATE, ID: r.ID, Pid: uint32(pid), } + go s.waitExit(process, pid, cmd) return &shimapi.CreateResponse{ Pid: uint32(pid), }, nil @@ -115,12 +115,12 @@ func (s *Service) Exec(ctx context.Context, r *shimapi.ExecRequest) (*shimapi.Ex } reaper.Default.RegisterNL(pid, cmd) reaper.Default.Unlock() - go s.waitExit(process, pid, cmd) s.events <- &container.Event{ Type: container.Event_EXEC_ADDED, ID: s.id, Pid: uint32(pid), } + go s.waitExit(process, pid, cmd) return &shimapi.ExecResponse{ Pid: uint32(pid), }, nil