From 855a5a89dd24e9ef5527a34b14ef0d37cd039a26 Mon Sep 17 00:00:00 2001
From: Daniel J Walsh <dwalsh@redhat.com>
Date: Wed, 14 Apr 2021 15:55:09 -0400
Subject: [PATCH] Fix flake on failed podman-remote build : try 2

This time we are checking if the function actually succeeded,
otherwise we will report an error.

Also if we did not get the id, report unexpected failure.

[NO TESTS NEEDED] Still no good way to test this, but manually.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
---
 pkg/api/handlers/compat/images_build.go | 10 ++++++----
 pkg/bindings/images/build.go            | 16 ++++++++++++++--
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/pkg/api/handlers/compat/images_build.go b/pkg/api/handlers/compat/images_build.go
index e0c79e5a74..ec40fdd2d2 100644
--- a/pkg/api/handlers/compat/images_build.go
+++ b/pkg/api/handlers/compat/images_build.go
@@ -464,15 +464,16 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
 
 	var (
 		imageID string
-		failed  bool
+		success bool
 	)
 
 	runCtx, cancel := context.WithCancel(context.Background())
 	go func() {
 		defer cancel()
 		imageID, _, err = runtime.Build(r.Context(), buildOptions, query.Dockerfile)
-		if err != nil {
-			failed = true
+		if err == nil {
+			success = true
+		} else {
 			stderr.Write([]byte(err.Error() + "\n"))
 		}
 	}()
@@ -534,7 +535,8 @@ loop:
 			}
 			flush()
 		case <-runCtx.Done():
-			if !failed {
+			flush()
+			if success {
 				if !utils.IsLibpodRequest(r) {
 					m.Stream = fmt.Sprintf("Successfully built %12.12s\n", imageID)
 					if err := enc.Encode(m); err != nil {
diff --git a/pkg/bindings/images/build.go b/pkg/bindings/images/build.go
index 34d6cee05b..c0e5706a5c 100644
--- a/pkg/bindings/images/build.go
+++ b/pkg/bindings/images/build.go
@@ -340,6 +340,7 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
 	re := regexp.MustCompile(`[0-9a-f]{12}`)
 
 	var id string
+	var mErr error
 	for {
 		var s struct {
 			Stream string `json:"stream,omitempty"`
@@ -347,11 +348,21 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
 		}
 		if err := dec.Decode(&s); err != nil {
 			if errors.Is(err, io.EOF) {
-				return &entities.BuildReport{ID: id}, nil
+				if mErr == nil && id == "" {
+					mErr = errors.New("stream dropped, unexpected failure")
+				}
+				break
 			}
 			s.Error = err.Error() + "\n"
 		}
 
+		select {
+		case <-response.Request.Context().Done():
+			return &entities.BuildReport{ID: id}, mErr
+		default:
+			// non-blocking select
+		}
+
 		switch {
 		case s.Stream != "":
 			stdout.Write([]byte(s.Stream))
@@ -359,11 +370,12 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO
 				id = strings.TrimSuffix(s.Stream, "\n")
 			}
 		case s.Error != "":
-			return nil, errors.New(s.Error)
+			mErr = errors.New(s.Error)
 		default:
 			return &entities.BuildReport{ID: id}, errors.New("failed to parse build results stream, unexpected input")
 		}
 	}
+	return &entities.BuildReport{ID: id}, mErr
 }
 
 func nTar(excludes []string, sources ...string) (io.ReadCloser, error) {