Browse code

Fix race on state serialization

Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com>

Alexandr Morozov authored on 2014/08/15 00:38:06
Showing 1 changed files
... ...
@@ -1,6 +1,7 @@
1 1
 package daemon
2 2
 
3 3
 import (
4
+	"encoding/json"
4 5
 	"fmt"
5 6
 	"sync"
6 7
 	"time"
... ...
@@ -49,6 +50,16 @@ func (s *State) String() string {
49 49
 	return fmt.Sprintf("Exited (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
50 50
 }
51 51
 
52
+type jState State
53
+
54
+// MarshalJSON for state is needed to avoid race conditions on inspect
55
+func (s *State) MarshalJSON() ([]byte, error) {
56
+	s.RLock()
57
+	b, err := json.Marshal(jState(*s))
58
+	s.RUnlock()
59
+	return b, err
60
+}
61
+
52 62
 func wait(waitChan <-chan struct{}, timeout time.Duration) error {
53 63
 	if timeout < 0 {
54 64
 		<-waitChan