Skip to content

Commit db1763c

Browse files
authored
fix: fix progress bar stuck when error occurred during pull/push (#65)
Signed-off-by: chlins <[email protected]>
1 parent 8c2f434 commit db1763c

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

pkg/backend/progress.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ func (p *ProgressBar) PrintMessage(prompt string, desc ocispec.Descriptor, messa
105105
p.bars[desc.Digest.String()] = bar
106106
}
107107

108+
// Abort aborts the progress bar by the given desc.
109+
func (p *ProgressBar) Abort(desc ocispec.Descriptor) {
110+
p.mu.Lock()
111+
defer p.mu.Unlock()
112+
// if the bar already exists, abort it.
113+
bar, ok := p.bars[desc.Digest.String()]
114+
if ok {
115+
bar.Abort(false)
116+
}
117+
}
118+
108119
// Wait waits for the progress bar to finish.
109120
func (p *ProgressBar) Wait() {
110121
p.mpb.Wait()

pkg/backend/pull.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func pullIfNotExist(ctx context.Context, pb *ProgressBar, prompt string, src *re
163163
if desc.MediaType == ocispec.MediaTypeImageManifest {
164164
body, err := io.ReadAll(pb.Add(prompt, desc, content))
165165
if err != nil {
166+
pb.Abort(desc)
166167
return fmt.Errorf("failed to read the manifest: %w", err)
167168
}
168169

@@ -171,6 +172,7 @@ func pullIfNotExist(ctx context.Context, pb *ProgressBar, prompt string, src *re
171172
}
172173
} else {
173174
if _, _, err := dst.PushBlob(ctx, repo, pb.Add(prompt, desc, content)); err != nil {
175+
pb.Abort(desc)
174176
return err
175177
}
176178
}

pkg/backend/push.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func pushIfNotExist(ctx context.Context, pb *ProgressBar, prompt string, src sto
142142
}
143143

144144
if err := dst.Manifests().Push(ctx, desc, pb.Add(prompt, desc, bytes.NewReader(manifestRaw))); err != nil {
145+
pb.Abort(desc)
145146
return err
146147
}
147148

@@ -159,6 +160,7 @@ func pushIfNotExist(ctx context.Context, pb *ProgressBar, prompt string, src sto
159160
defer content.Close()
160161

161162
if err := dst.Blobs().Push(ctx, desc, pb.Add(prompt, desc, content)); err != nil {
163+
pb.Abort(desc)
162164
return err
163165
}
164166
}

0 commit comments

Comments
 (0)