Skip to content

Commit 36d6d76

Browse files
committedNov 17, 2016
Use WaitWithContext for WaitStop
Signed-off-by: Brian Goff <[email protected]>
1 parent a27e51e commit 36d6d76

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed
 

‎container/state.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -177,26 +177,24 @@ func wait(waitChan <-chan struct{}, timeout time.Duration) error {
177177
// immediately. If you want wait forever you must supply negative timeout.
178178
// Returns exit code, that was passed to SetStopped
179179
func (s *State) WaitStop(timeout time.Duration) (int, error) {
180-
s.Lock()
181-
if !s.Running {
182-
exitCode := s.ExitCodeValue
183-
s.Unlock()
184-
return exitCode, nil
180+
ctx := context.Background()
181+
if timeout >= 0 {
182+
var cancel func()
183+
ctx, cancel = context.WithTimeout(ctx, timeout)
184+
defer cancel()
185185
}
186-
waitChan := s.waitChan
187-
s.Unlock()
188-
if err := wait(waitChan, timeout); err != nil {
186+
if err := s.WaitWithContext(ctx); err != nil {
187+
if status, ok := err.(*StateStatus); ok {
188+
return status.ExitCode(), nil
189+
}
189190
return -1, err
190191
}
191-
s.Lock()
192-
defer s.Unlock()
193-
return s.ExitCode(), nil
192+
return 0, nil
194193
}
195194

196195
// WaitWithContext waits for the container to stop. Optional context can be
197196
// passed for canceling the request.
198197
func (s *State) WaitWithContext(ctx context.Context) error {
199-
// todo(tonistiigi): make other wait functions use this
200198
s.Lock()
201199
if !s.Running {
202200
state := newStateStatus(s.ExitCode(), s.Error())

0 commit comments

Comments
 (0)
Please sign in to comment.