Skip to content

Commit 7660acf

Browse files
committed
progress: ensure bake waits for progress to finish printing on error conditions
Some minor fixes to the printer and how bake invokes it. Bake previously had a race condition that could result in the display not updating on an error condition, but it was much rarer because the channel communication was much closer. The refactor added a proxy for the status channel so there was more of an opportunity to surface the race condition. When bake exits with an error when reading the bakefiles, it doesn't wait for the printer to finish so it is possible for the printer to update the display after an error is printed. This adds an extra `Wait` in a defer to make sure the printer is finished. `Wait` has also been fixed to allow it to be called multiple times and have the same behavior. Previously, it only waited for the done channel once so only the first wait would block. The `onclose` method is now called every time the display is paused or stopped. That was the previous behavior and it's been restored here. The display only gets refreshed if we aren't exiting. There's no point in initializing another display if we're about to exit. The metric writer attached to the printer was erroneously removed. It is now assigned properly. Signed-off-by: Jonathan A. Sternberg <[email protected]>
1 parent ba782f1 commit 7660acf

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

commands/bake.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,13 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
162162
attributes := bakeMetricAttributes(dockerCli, driverType, url, cmdContext, targets, &in)
163163

164164
progressMode := progressui.DisplayMode(cFlags.progress)
165+
165166
var printer *progress.Printer
167+
defer func() {
168+
if printer != nil {
169+
printer.Wait()
170+
}
171+
}()
166172

167173
makePrinter := func() error {
168174
var err error

util/progress/printer.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ type Printer struct {
5151
func (p *Printer) Wait() error {
5252
p.closeOnce.Do(func() {
5353
close(p.status)
54-
<-p.done
5554
})
55+
<-p.done
5656
return p.err
5757
}
5858

@@ -144,6 +144,7 @@ func NewPrinter(ctx context.Context, out console.File, mode progressui.DisplayMo
144144
interrupt: make(chan interruptRequest),
145145
state: printerStateRunning,
146146
done: make(chan struct{}),
147+
metrics: opt.mw,
147148
}
148149
go pw.run(ctx, d)
149150

@@ -155,21 +156,21 @@ func (p *Printer) run(ctx context.Context, d progressui.Display) {
155156
defer close(p.interrupt)
156157

157158
var ss []*client.SolveStatus
158-
for p.state != printerStateDone {
159+
for {
159160
switch p.state {
160161
case printerStatePaused:
161162
ss, p.err = p.bufferDisplay(ctx, ss)
162163
case printerStateRunning:
163-
var warnings []client.VertexWarning
164-
warnings, ss, p.err = p.updateDisplay(ctx, d, ss)
165-
p.warnings = append(p.warnings, warnings...)
166-
167-
d, _ = p.newDisplay()
164+
p.warnings, ss, p.err = p.updateDisplay(ctx, d, ss)
165+
if p.opt.onclose != nil {
166+
p.opt.onclose()
167+
}
168168
}
169-
}
170169

171-
if p.opt.onclose != nil {
172-
p.opt.onclose()
170+
if p.state == printerStateDone {
171+
break
172+
}
173+
d, _ = p.newDisplay()
173174
}
174175
}
175176

0 commit comments

Comments
 (0)