Skip to content

Commit

Permalink
Fix deadlock when the builder outputs a lot of data over stdio. Ref: g…
Browse files Browse the repository at this point in the history
…olang/go#16787

Also improve developer experience by outputting logs incrementally.

Includes a small tweak in detect.ts to allow referencing the workflow in
this repository by commit.

Signed-off-by: Juliette Pretot <julsh@google.com>
Co-authored-by: Jason LeBrun <jibbl@google.com>
  • Loading branch information
jul-sh and jblebrun committed Apr 16, 2024
1 parent 4534a0b commit 1667306
Showing 1 changed file with 62 additions and 20 deletions.
82 changes: 62 additions & 20 deletions internal/builders/docker/pkg/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package pkg
// Docker image.

import (
"bufio"
"crypto/sha256"
"encoding/hex"
"encoding/json"
Expand All @@ -36,6 +37,7 @@ import (
"path"
"path/filepath"
"strings"
"sync"

intoto "github.com/in-toto/in-toto-golang/in_toto"
slsa1 "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v1"
Expand Down Expand Up @@ -476,34 +478,74 @@ func (c *GitClient) checkoutGitCommit() error {
return nil
}

type tempFileResult struct {
File *os.File
Err error
}

// A helper function used by saveToTempFile to process one individual file.
func saveOneTempFile(verbose bool, reader io.Reader, fileChannel chan tempFileResult, printChannel chan string) {
var allBytes []byte
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
bytes := scanner.Bytes()
allBytes = append(allBytes, bytes...)
allBytes = append(allBytes, '\n')

if verbose {
printChannel <- string(bytes)
}
}

tmpfile, err := os.CreateTemp("", "log-*.txt")
if err != nil {
fileChannel <- tempFileResult{Err: err}
return
}
if _, err := tmpfile.Write(allBytes); err != nil {
tmpfile.Close()
fileChannel <- tempFileResult{Err: fmt.Errorf("couldn't write bytes to tempfile: %v", err)}
}

fileChannel <- tempFileResult{File: tmpfile}
}

// saveToTempFile creates a tempfile in `/tmp` and writes the content of the
// given readers to that file.
// It processes all provided readers concurrently.
func saveToTempFile(verbose bool, readers ...io.Reader) ([]string, error) {
var files []string
if verbose {
fmt.Print("\n\n>>>>>>>>>>>>>> output from command <<<<<<<<<<<<<<\n")
}
var wg sync.WaitGroup
var fileChannel = make(chan tempFileResult, len(readers))
var printChannel = make(chan string)

for _, reader := range readers {
bytes, err := io.ReadAll(reader)
if err != nil {
return files, err
}
wg.Add(1)
go func(reader io.Reader) {
defer wg.Done()
saveOneTempFile(verbose, reader, fileChannel, printChannel)
}(reader)
}

if verbose {
if len(bytes) > 0 {
fmt.Print("\n\n>>>>>>>>>>>>>> output from command <<<<<<<<<<<<<<\n")
fmt.Printf("%s", bytes)
fmt.Print("=================================================\n\n\n")
}
}
// Close the channel once all goroutines have finished.
go func() {
wg.Wait()
close(printChannel)
close(fileChannel)
}()

tmpfile, err := os.CreateTemp("", "log-*.txt")
if err != nil {
return files, fmt.Errorf("couldn't create tempfile: %v", err)
}
for line := range printChannel {
fmt.Println(line)
}

if _, err := tmpfile.Write(bytes); err != nil {
tmpfile.Close()
return files, fmt.Errorf("couldn't write bytes to tempfile: %v", err)
var files []string
for result := range fileChannel {
if result.Err != nil {
return nil, result.Err
}
files = append(files, tmpfile.Name())
files = append(files, result.File.Name())
}

return files, nil
Expand Down

0 comments on commit 1667306

Please sign in to comment.