Browse code

Use WaitWithContext for WaitStop

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

Brian Goff authored on 2016/11/17 12:08:37
Showing 1 changed files
... ...
@@ -177,26 +177,24 @@ func wait(waitChan <-chan struct{}, timeout time.Duration) error {
177 177
 // immediately. If you want wait forever you must supply negative timeout.
178 178
 // Returns exit code, that was passed to SetStopped
179 179
 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()
185 185
 	}
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
+		}
189 190
 		return -1, err
190 191
 	}
191
-	s.Lock()
192
-	defer s.Unlock()
193
-	return s.ExitCode(), nil
192
+	return 0, nil
194 193
 }
195 194
 
196 195
 // WaitWithContext waits for the container to stop. Optional context can be
197 196
 // passed for canceling the request.
198 197
 func (s *State) WaitWithContext(ctx context.Context) error {
199
-	// todo(tonistiigi): make other wait functions use this
200 198
 	s.Lock()
201 199
 	if !s.Running {
202 200
 		state := newStateStatus(s.ExitCode(), s.Error())