Skip to content

fix(queue): read running under mutex in loop to avoid data race#909

Open
Noethix55555 wants to merge 1 commit into
gocolly:masterfrom
Noethix55555:fix/queue-running-data-race
Open

fix(queue): read running under mutex in loop to avoid data race#909
Noethix55555 wants to merge 1 commit into
gocolly:masterfrom
Noethix55555:fix/queue-running-data-race

Conversation

@Noethix55555

Copy link
Copy Markdown

@
Fixes #907

Problem

Queue.loop() read the running field without holding q.mut, while Stop() and Run() write it under q.mut. This is a data race, reported by go test -race:

WARNING: DATA RACE
Write at ... by goroutine N: queue.(*Queue).Stop()  queue/queue.go:156
Previous read at ... by goroutine M: queue.(*Queue).loop()  queue/queue.go:168

Fix

Snapshot running under q.mut at the top of each loop iteration and use the local copy for the termination check.

Test

Added TestQueueStopRace: queues several thousand requests, then calls Stop() from another goroutine while Run() is processing. Run with -race it fails on master and passes with this change. The full go test -race ./queue/... suite passes.
@

loop() read q.running without holding q.mut while Stop() and Run() wrote
it under the mutex, a data race detected by go test -race. Snapshot
running under q.mut at the top of each loop iteration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data race on Queue.running between loop() and Stop()/Run()

1 participant