History queue: data race between task.Execute() and processing_queue.go's %+v error log
Background
CI's -race integration job intermittently fails host.TestIntegrationSuite
with a WARNING: DATA RACE. The failing test (TestSignalWorkflow_Cron_NoDecisionTaskCreated)
isn't the cause. The race lives in shared background history-queue machinery
(service/history/task and service/history/queue) and can surface on any
integration test that exercises the timer/transfer queue processors under
-race. The raced field is a bool on an already-anomalous "task out of
queue scope" error path, so this isn't a production correctness risk, but it
does intermittently red master's CI whenever the scope-mismatch condition and
worker timing line up.
Problem
taskImpl.shouldProcessTask (service/history/task/task.go:191) is written
unguarded inside Execute():
t.shouldProcessTask, err = t.taskFilter(t.Task)This field is called out as legacy debt at task.go:99:
// TODO: following three fields should be removed after new task lifecycle is implemented.
Concurrently, processingQueueImpl.AddTasks() (service/history/queue/processing_queue.go:197)
formats the same live task object with %+v when a task fails the
taskBelongsToProcessQueue scope check:
fmt.Errorf("processing queue state: %+v, key: %+v, task: %+v", q.state, key, task)%+v reflects over the whole *taskImpl struct, racing with the concurrent
unguarded write above.
How the same task gets raced
timerQueueProcessorBase.processQueueCollections() (timer_queue_processor_base.go:290-329)
constructs each task and calls t.submitTask(task) to hand it to the async
worker pool for Execute() immediately, inside the read-batch loop. After the
whole batch has been submitted, it passes the identical task map into
queueCollection.AddTasks(), whose error branch (when a task fails the scope
check) formats that same, already-submitted task.Task with %+v. Every task
reaching that error branch has already been handed to a worker for
Execute(), up to one batch-iteration earlier. The submit-then-inspect
pattern happens on every read cycle, so as soon as the scope-mismatch
condition fires, there's a wide timing window for the race to hit.
Suggested fix
Stop formatting the live task.Task interface with %+v in
processing_queue.go:197's error branch. Two lines above it, the "Skipping
task" debug log already does this safely by extracting identifying fields via
interface methods instead of reflecting the whole struct; apply the same
pattern here:
if !taskBelongsToProcessQueue(q.state, key, task) {
errMsg := "Processing queue encountered a task doesn't belong to its scope"
q.logger.Error(errMsg, tag.Error(
fmt.Errorf("processing queue state: %+v, key: %+v, domainID: %v, workflowID: %v, runID: %v, taskType: %v",
q.state, key, task.GetDomainID(), task.GetWorkflowID(), task.GetRunID(), task.GetTaskType()),
))
continue
}A second, more thorough (not required to unblock this) fix is to guard
shouldProcessTask/taskFilter with taskImpl's existing mutex in
task.go, consistent with the other mutable fields (attempt, state,
priority) which already take the lock.
Acceptance criteria
processing_queue.go's scope-check error branch no longer reflects the live task object with%+v.- (Optional/stretch)
shouldProcessTask/taskFilterwrites intask.goare guarded by the existing lock. -raceno longer flags this data race in CI.
References
service/history/task/task.go:99, 191service/history/queue/processing_queue.go:189, 197service/history/queue/timer_queue_processor_base.go:290-329- Introducing commit:
fef64d7ed6(#3214, "Multi-cursor processing queue implementation", 2020-04-30)
Source: cadence-workflow/cadence