Browse code

Merge pull request #26719 from AkihiroSuda/fix-race-builder-vs-container-1

Fix a race found in TestBuildCancellationKillsSleep

Tibor Vass authored on 2016/10/11 01:54:55
Showing 1 changed files
... ...
@@ -16,7 +16,6 @@ import (
16 16
 	"path/filepath"
17 17
 	"sort"
18 18
 	"strings"
19
-	"sync"
20 19
 	"time"
21 20
 
22 21
 	"github.com/Sirupsen/logrus"
... ...
@@ -524,10 +523,7 @@ func (b *Builder) run(cID string) (err error) {
524 524
 	}()
525 525
 
526 526
 	finished := make(chan struct{})
527
-	var once sync.Once
528
-	finish := func() { close(finished) }
529 527
 	cancelErrCh := make(chan error, 1)
530
-	defer once.Do(finish)
531 528
 	go func() {
532 529
 		select {
533 530
 		case <-b.clientCtx.Done():
... ...
@@ -541,22 +537,37 @@ func (b *Builder) run(cID string) (err error) {
541 541
 	}()
542 542
 
543 543
 	if err := b.docker.ContainerStart(cID, nil, true, ""); err != nil {
544
+		close(finished)
545
+		if cancelErr := <-cancelErrCh; cancelErr != nil {
546
+			logrus.Debugf("Build cancelled (%v) and got an error from ContainerStart: %v",
547
+				cancelErr, err)
548
+		}
544 549
 		return err
545 550
 	}
546 551
 
547 552
 	// Block on reading output from container, stop on err or chan closed
548 553
 	if err := <-errCh; err != nil {
554
+		close(finished)
555
+		if cancelErr := <-cancelErrCh; cancelErr != nil {
556
+			logrus.Debugf("Build cancelled (%v) and got an error from errCh: %v",
557
+				cancelErr, err)
558
+		}
549 559
 		return err
550 560
 	}
551 561
 
552 562
 	if ret, _ := b.docker.ContainerWait(cID, -1); ret != 0 {
563
+		close(finished)
564
+		if cancelErr := <-cancelErrCh; cancelErr != nil {
565
+			logrus.Debugf("Build cancelled (%v) and got a non-zero code from ContainerWait: %d",
566
+				cancelErr, ret)
567
+		}
553 568
 		// TODO: change error type, because jsonmessage.JSONError assumes HTTP
554 569
 		return &jsonmessage.JSONError{
555 570
 			Message: fmt.Sprintf("The command '%s' returned a non-zero code: %d", strings.Join(b.runConfig.Cmd, " "), ret),
556 571
 			Code:    ret,
557 572
 		}
558 573
 	}
559
-	once.Do(finish)
574
+	close(finished)
560 575
 	return <-cancelErrCh
561 576
 }
562 577