Skip to content

Commit

Permalink
Fix deadlock (#2630)
Browse files Browse the repository at this point in the history
* Fix deadlock

Signed-off-by: Matej Vašek <[email protected]>

* Rework digest detection

Signed-off-by: Matej Vašek <[email protected]>

---------

Signed-off-by: Matej Vašek <[email protected]>
  • Loading branch information
matejvasek authored Dec 20, 2024
1 parent 1122bf7 commit 8475f86
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions hack/update-builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,43 +189,45 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
}(rc)

pr, pw := io.Pipe()
digestCh := make(chan string)
r := io.TeeReader(rc, pw)

go func() {
var (
jm jsonmessage.JSONMessage
dec = json.NewDecoder(pr)
err error
)
for {
err = dec.Decode(&jm)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
panic(err)
}
if jm.Error != nil {
continue
}
fd := os.Stdout.Fd()
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
e := jsonmessage.DisplayJSONMessagesStream(pr, os.Stderr, fd, isTerminal, nil)
_ = pr.CloseWithError(e)
}()

re := regexp.MustCompile(`\sdigest: (?P<hash>sha256:[a-zA-Z0-9]+)\s`)
matches := re.FindStringSubmatch(jm.Status)
if len(matches) == 2 {
digestCh <- matches[1]
var (
digest string
jm jsonmessage.JSONMessage
dec = json.NewDecoder(r)
re = regexp.MustCompile(`\sdigest: (?P<hash>sha256:[a-zA-Z0-9]+)\s`)
)
for {
err = dec.Decode(&jm)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return "", err
}
if jm.Error != nil {
continue
}
}()
r := io.TeeReader(rc, pw)

fd := os.Stdout.Fd()
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
err = jsonmessage.DisplayJSONMessagesStream(r, os.Stderr, fd, isTerminal, nil)
_ = pw.Close()
if err != nil {
return "", err
matches := re.FindStringSubmatch(jm.Status)
if len(matches) == 2 {
digest = matches[1]
_, _ = io.Copy(io.Discard, r)
break
}
}

return <-digestCh, nil
if digest == "" {
return "", fmt.Errorf("digest not found")
}
return digest, nil
}

var d string
Expand Down

0 comments on commit 8475f86

Please sign in to comment.