Fix race condition in generate task (#1888)

This commit is contained in:
WithoutPants
2021-10-26 08:41:40 +11:00
committed by GitHub
parent 1e5889ba17
commit 595e8efb73

View File

@@ -53,6 +53,8 @@ func (j *GenerateJob) Execute(ctx context.Context, progress *job.Progress) {
queue := make(chan Task, generateQueueSize) queue := make(chan Task, generateQueueSize)
go func() { go func() {
defer close(queue)
var totals totalsGenerate var totals totalsGenerate
sceneIDs, err := utils.StringSliceToIntSlice(j.input.SceneIDs) sceneIDs, err := utils.StringSliceToIntSlice(j.input.SceneIDs)
if err != nil { if err != nil {
@@ -117,8 +119,11 @@ func (j *GenerateJob) Execute(ctx context.Context, progress *job.Progress) {
} }
wg.Add() wg.Add()
go progress.ExecuteTask(f.GetDescription(), func() { // #1879 - need to make a copy of f - otherwise there is a race condition
f.Start(ctx) // where f is changed when the goroutine runs
localTask := f
go progress.ExecuteTask(localTask.GetDescription(), func() {
localTask.Start(ctx)
wg.Done() wg.Done()
progress.Increment() progress.Increment()
}) })
@@ -136,8 +141,6 @@ func (j *GenerateJob) Execute(ctx context.Context, progress *job.Progress) {
} }
func (j *GenerateJob) queueTasks(ctx context.Context, queue chan<- Task) totalsGenerate { func (j *GenerateJob) queueTasks(ctx context.Context, queue chan<- Task) totalsGenerate {
defer close(queue)
var totals totalsGenerate var totals totalsGenerate
const batchSize = 1000 const batchSize = 1000