Fix data race on SessionDataTask.started and tighten concurrency tests#2549
Open
onevcat wants to merge 1 commit into
Open
Fix data race on SessionDataTask.started and tighten concurrency tests#2549onevcat wants to merge 1 commit into
onevcat wants to merge 1 commit into
Conversation
started was written by resume() without synchronization while ImageDownloader.startDownloadTask reads it from other threads — the same class of race as the forceCancel one fixed in #2539. Guard it with the existing lock, making resume() an atomic check-and-set with task.resume() called outside the lock. Also condense the forceCancel comment introduced in #2539 and add a single combined stress test hammering all mutating and reading surfaces of SessionDataTask concurrently (addCallback, forceCancel, resume, started, containsCallbacks, didReceiveData, mutableDataCount, completeAndRemoveAllCallbacks). Like the #2539 regression test, it is meaningful mainly under Thread Sanitizer: xcodebuild test -project Kingfisher.xcodeproj -scheme Kingfisher \ -destination 'platform=macOS' -enableThreadSanitizer YES \ -only-testing:KingfisherTests/ImageDownloaderTests/testSessionDataTaskConcurrentAccessIsThreadSafe
8a84c2b to
4baa44e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #2539 (thanks @devzahirul), which fixed the unlocked
callbacksStoreiteration inforceCancel()and is now merged.started, found while reviewing Fix data race in SessionDataTask.forceCancel() #2539:resume()wrote the flag unsynchronized whileImageDownloader.startDownloadTaskreads it from other threads. It is now guarded by the existing lock, withresume()performing an atomic check-and-set and callingtask.resume()outside the lock.forceCancelcomment from Fix data race in SessionDataTask.forceCancel() #2539 to the two load-bearing facts (cross-thread mutation;cancel(token:)re-acquires the non-recurrent lock).testSessionDataTaskConcurrentAccessIsThreadSafethat hammers all mutating and reading surfaces ofSessionDataTaskconcurrently (addCallback,forceCancel,resume,started,containsCallbacks,didReceiveData,mutableDataCount,completeAndRemoveAllCallbacks). The focused Fix data race in SessionDataTask.forceCancel() #2539 regression test remains as-is.Verification
Before the fix, the
startedrace is reported deterministically by Thread Sanitizer (process crashes):After the fix, both the combined test and the #2539 regression test run clean under TSan:
Full macOS suite passes (666 tests).
Ref: #2539