Skip to content
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

progress: fix possible race in Stop(); fixes #322 #332

Merged
merged 1 commit into from
Oct 2, 2024
Merged
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
76 changes: 41 additions & 35 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,38 @@ var (

// Progress helps track progress for one or more tasks.
type Progress struct {
autoStop bool
lengthMessage int
lengthProgress int
lengthProgressOverall int
lengthTracker int
logsToRender []string
logsToRenderMutex sync.RWMutex
numTrackersExpected int64
outputWriter io.Writer
overallTracker *Tracker
overallTrackerMutex sync.RWMutex
pinnedMessages []string
pinnedMessageMutex sync.RWMutex
pinnedMessageNumLines int
renderContext context.Context
renderContextCancel context.CancelFunc
renderInProgress bool
renderInProgressMutex sync.RWMutex
sortBy SortBy
style *Style
terminalWidth int
terminalWidthMutex sync.RWMutex
terminalWidthOverride int
trackerPosition Position
trackersActive []*Tracker
trackersActiveMutex sync.RWMutex
trackersDone []*Tracker
trackersDoneMutex sync.RWMutex
trackersInQueue []*Tracker
trackersInQueueMutex sync.RWMutex
updateFrequency time.Duration
autoStop bool
lengthMessage int
lengthProgress int
lengthProgressOverall int
lengthTracker int
logsToRender []string
logsToRenderMutex sync.RWMutex
numTrackersExpected int64
outputWriter io.Writer
overallTracker *Tracker
overallTrackerMutex sync.RWMutex
pinnedMessages []string
pinnedMessageMutex sync.RWMutex
pinnedMessageNumLines int
renderContext context.Context
renderContextCancel context.CancelFunc
renderContextCancelMutex sync.Mutex
renderInProgress bool
renderInProgressMutex sync.RWMutex
sortBy SortBy
style *Style
terminalWidth int
terminalWidthMutex sync.RWMutex
terminalWidthOverride int
trackerPosition Position
trackersActive []*Tracker
trackersActiveMutex sync.RWMutex
trackersDone []*Tracker
trackersDoneMutex sync.RWMutex
trackersInQueue []*Tracker
trackersInQueueMutex sync.RWMutex
updateFrequency time.Duration
}

// Position defines the position of the Tracker with respect to the Tracker's
Expand Down Expand Up @@ -284,7 +285,10 @@ func (p *Progress) ShowValue(show bool) {

// Stop stops the Render() logic that is in progress.
func (p *Progress) Stop() {
if p.IsRenderInProgress() {
p.renderContextCancelMutex.Lock()
defer p.renderContextCancelMutex.Unlock()

if p.renderContextCancel != nil {
p.renderContextCancel()
}
}
Expand All @@ -309,15 +313,17 @@ func (p *Progress) getTerminalWidth() int {
}

func (p *Progress) initForRender() {
// reset the signals
p.renderContextCancelMutex.Lock()
p.renderContext, p.renderContextCancel = context.WithCancel(context.Background())
p.renderContextCancelMutex.Unlock()

// pick a default style
p.Style()
if p.style.Options.SpeedOverallFormatter == nil {
p.style.Options.SpeedOverallFormatter = FormatNumber
}

// reset the signals
p.renderContext, p.renderContextCancel = context.WithCancel(context.Background())

// pick default lengths if no valid ones set
if p.lengthTracker <= 0 {
p.lengthTracker = DefaultLengthTracker
Expand Down