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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 4 deletions

View File

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