-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix thread safety issue in package collections #3136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import Basics | ||
import PackageModel | ||
import TSCBasic | ||
|
||
|
@@ -100,12 +101,11 @@ public struct PackageCollections: PackageCollectionsProtocol { | |
if sources.isEmpty { | ||
return callback(.success([])) | ||
} | ||
let lock = Lock() | ||
var refreshResults = [Result<Model.Collection, Error>]() | ||
let refreshResults = ThreadSafeArrayStore<Result<Model.Collection, Error>>() | ||
sources.forEach { source in | ||
self.refreshCollectionFromSource(source: source) { refreshResult in | ||
lock.withLock { refreshResults.append(refreshResult) } | ||
if refreshResults.count == (lock.withLock { sources.count }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was the issue that prompted the PR. I saw a few PRs failing on PackageCollectionsTests.testHappyRefresh inconsistently so I was hunting for a race condition (interesting TSAN did not report this). the issue here is that the lock is on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed this coding mistake in #3108 though didn't anticipate it causing race condition. Anyway, 👍 |
||
refreshResults.append(refreshResult) | ||
if refreshResults.count == sources.count { | ||
let errors = refreshResults.compactMap { $0.failure } | ||
callback(errors.isEmpty ? .success(sources) : .failure(MultipleErrors(errors))) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.