From 0ce132eb8ff6c9b1e357a8a153552c4f958378a6 Mon Sep 17 00:00:00 2001 From: Dominic Wong Date: Wed, 10 Jun 2020 09:36:41 +0100 Subject: [PATCH] Fix race condition when updating process being waited on (#1694) --- runtime/service.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/runtime/service.go b/runtime/service.go index 5c6ba621..8e28de1f 100644 --- a/runtime/service.go +++ b/runtime/service.go @@ -197,11 +197,20 @@ func (s *service) Error() error { // Wait waits for the service to finish running func (s *service) Wait() { // wait for process to exit - err := s.Process.Wait(s.PID) + s.RLock() + thisPID := s.PID + s.RUnlock() + err := s.Process.Wait(thisPID) s.Lock() defer s.Unlock() + if s.PID.ID != thisPID.ID { + // trying to update when it's already been switched out, ignore + logger.Warnf("Trying to update a process status but PID doesn't match. Old %s, New %s. Skipping update.", thisPID.ID, s.PID.ID) + return + } + // save the error if err != nil { if logger.V(logger.ErrorLevel, logger.DefaultLogger) {