Skip to content

Commit

Permalink
Fix flake on failed podman-remote build : try 2
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rhatdan authored and mheon committed Apr 16, 2021
1 parent 5c21861 commit fc99c7d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 6 additions & 4 deletions pkg/api/handlers/compat/images_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,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"))
}
}()
Expand Down Expand Up @@ -448,7 +449,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 {
Expand Down
16 changes: 14 additions & 2 deletions pkg/bindings/images/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,30 +304,42 @@ 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"`
Error string `json:"error,omitempty"`
}
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))
if re.Match([]byte(s.Stream)) {
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) {
Expand Down

0 comments on commit fc99c7d

Please sign in to comment.