From f1975cbc7c5b551e5a206c15f32aef3d51904408 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Thu, 14 Aug 2014 19:38:06 +0400 Subject: [PATCH] Fix race on state serialization Signed-off-by: Alexandr Morozov --- daemon/state.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/daemon/state.go b/daemon/state.go index ed137b0206..44742b78c9 100644 --- a/daemon/state.go +++ b/daemon/state.go @@ -1,6 +1,7 @@ package daemon import ( + "encoding/json" "fmt" "sync" "time" @@ -49,6 +50,16 @@ func (s *State) String() string { return fmt.Sprintf("Exited (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt))) } +type jState State + +// MarshalJSON for state is needed to avoid race conditions on inspect +func (s *State) MarshalJSON() ([]byte, error) { + s.RLock() + b, err := json.Marshal(jState(*s)) + s.RUnlock() + return b, err +} + func wait(waitChan <-chan struct{}, timeout time.Duration) error { if timeout < 0 { <-waitChan