Skip to content

Commit

Permalink
Fix race on state serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com>
  • Loading branch information
LK4D4 committed Aug 14, 2014
1 parent 93d6adf commit f1975cb
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions daemon/state.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package daemon

import (
"encoding/json"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f1975cb

Please sign in to comment.