Skip to content

fix: prevent git progress writer race reading stageNumber #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func Run(ctx context.Context, opts options.Options) error {
newColor(color.FgCyan).Sprintf(cloneOpts.Path),
)

w := git.ProgressWriter(func(line string) { opts.Logger(log.LevelInfo, "#%d: %s", stageNumber, line) })
stageNum := stageNumber
w := git.ProgressWriter(func(line string) { opts.Logger(log.LevelInfo, "#%d: %s", stageNum, line) })
defer w.Close()
cloneOpts.Progress = w

Expand All @@ -132,6 +133,8 @@ func Run(ctx context.Context, opts options.Options) error {
opts.Logger(log.LevelError, "Falling back to the default image...")
}

_ = w.Close()

// Always clone the repo in remote repo build mode into a location that
// we control that isn't affected by the users changes.
if opts.RemoteRepoBuildMode {
Expand All @@ -146,7 +149,8 @@ func Run(ctx context.Context, opts options.Options) error {
newColor(color.FgCyan).Sprintf(cloneOpts.Path),
)

w := git.ProgressWriter(func(line string) { opts.Logger(log.LevelInfo, "#%d: %s", stageNumber, line) })
stageNum := stageNumber
w := git.ProgressWriter(func(line string) { opts.Logger(log.LevelInfo, "#%d: %s", stageNum, line) })
defer w.Close()
cloneOpts.Progress = w

Expand All @@ -158,6 +162,8 @@ func Run(ctx context.Context, opts options.Options) error {
opts.Logger(log.LevelError, "Failed to clone repository for remote repo mode: %s", fallbackErr.Error())
opts.Logger(log.LevelError, "Falling back to the default image...")
}

_ = w.Close()
}
}

Expand Down Expand Up @@ -893,7 +899,8 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
newColor(color.FgCyan).Sprintf(cloneOpts.Path),
)

w := git.ProgressWriter(func(line string) { opts.Logger(log.LevelInfo, "#%d: %s", stageNumber, line) })
stageNum := stageNumber
w := git.ProgressWriter(func(line string) { opts.Logger(log.LevelInfo, "#%d: %s", stageNum, line) })
defer w.Close()
cloneOpts.Progress = w

Expand All @@ -908,6 +915,8 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
opts.Logger(log.LevelError, "Failed to clone repository: %s", fallbackErr.Error())
opts.Logger(log.LevelError, "Falling back to the default image...")
}

_ = w.Close()
} else {
cloneOpts, err := git.CloneOptionsFromOptions(opts)
if err != nil {
Expand All @@ -920,7 +929,8 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
newColor(color.FgCyan).Sprintf(cloneOpts.Path),
)

w := git.ProgressWriter(func(line string) { opts.Logger(log.LevelInfo, "#%d: %s", stageNumber, line) })
stageNum := stageNumber
w := git.ProgressWriter(func(line string) { opts.Logger(log.LevelInfo, "#%d: %s", stageNum, line) })
defer w.Close()
cloneOpts.Progress = w

Expand All @@ -932,6 +942,8 @@ func RunCacheProbe(ctx context.Context, opts options.Options) (v1.Image, error)
opts.Logger(log.LevelError, "Failed to clone repository for remote repo mode: %s", fallbackErr.Error())
opts.Logger(log.LevelError, "Falling back to the default image...")
}

_ = w.Close()
}
}

Expand Down
11 changes: 8 additions & 3 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,14 @@ func CloneOptionsFromOptions(options options.Options) (CloneRepoOptions, error)

type progressWriter struct {
io.WriteCloser
r io.ReadCloser
r io.ReadCloser
done chan struct{}
}

func (w *progressWriter) Close() error {
err := w.r.Close()
err2 := w.WriteCloser.Close()
err := w.WriteCloser.Close()
<-w.done
err2 := w.r.Close()
if err != nil {
return err
}
Expand All @@ -331,7 +333,9 @@ func (w *progressWriter) Close() error {

func ProgressWriter(write func(line string)) io.WriteCloser {
reader, writer := io.Pipe()
done := make(chan struct{})
go func() {
defer close(done)
data := make([]byte, 4096)
for {
read, err := reader.Read(data)
Expand All @@ -351,5 +355,6 @@ func ProgressWriter(write func(line string)) io.WriteCloser {
return &progressWriter{
WriteCloser: writer,
r: reader,
done: done,
}
}