Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
iofs "io/fs"
"iter"
"path/filepath"
"runtime"
"runtime/pprof"
"sort"
"strings"
Expand Down Expand Up @@ -424,12 +425,27 @@ func (state *BuildState) Stop() {
// CloseResults closes the result channels.
func (state *BuildState) CloseResults() {
state.progress.cycleDetector.Stop()
// Drain any remaining results from the internal channel before closing.
// This prevents a race where forwardResults hasn't yet forwarded a result
// that was sent before all tasks completed.
state.drainResults()
state.progress.mutex.Lock()
defer state.progress.mutex.Unlock()
if state.progress.results != nil {
state.progress.resultOnce.Do(func() {
close(state.progress.results)
})
// Set to nil so forwardResults won't attempt to send on the closed
// channel if it reads a late result from internalResults.
state.progress.results = nil
}
}

// drainResults waits until all pending results in the internal channel have
// been forwarded to the external results channel.
func (state *BuildState) drainResults() {
for len(state.progress.internalResults) > 0 {
runtime.Gosched()
}
}

Expand Down
Loading