Skip to content

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

Merged
merged 2 commits into from
Dec 21, 2020

Conversation

tomerd
Copy link
Contributor

@tomerd tomerd commented Dec 21, 2020

motivation: a failing test hinted to a thread safety issue, which was then diagnosed to an incorrect lock on an array. this prompted creating a thread safe utility for array to avoid such cases

changes:

  • create a thread-safe utlitiy for array
  • replace array + lock with the new utility
  • add test

motivation: a failing test hinted to a thread safety issue, which was then diagnosed to an incorrect lock on an array. this prompted creating a thread safe utility for array to avoid such cases

changes:
* create a thread-safe utlitiy for array
* replace array + lock with the new utility
* add test
sources.forEach { source in
self.refreshCollectionFromSource(source: source) { refreshResult in
lock.withLock { refreshResults.append(refreshResult) }
if refreshResults.count == (lock.withLock { sources.count }) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 sources instead of refreshResults. so in addition to fixing this, I decided to create this new utility for arrays so we dont have to mess with locks

Copy link
Contributor

Choose a reason for hiding this comment

The 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, 👍

@tomerd
Copy link
Contributor Author

tomerd commented Dec 21, 2020

@swift-ci please test

@tomerd
Copy link
Contributor Author

tomerd commented Dec 21, 2020

@swift-ci please smoke test

@tomerd tomerd changed the title fix thread safety issue in pacakge collections fix thread safety issue in package collections Dec 21, 2020
@tomerd tomerd self-assigned this Dec 21, 2020
@tomerd
Copy link
Contributor Author

tomerd commented Dec 21, 2020

@swift-ci please smoke test

}
}
}

/// Thread-safe value boxing structure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Thread-safe value boxing structure
/// Thread-safe value boxing structure

sources.forEach { source in
self.refreshCollectionFromSource(source: source) { refreshResult in
lock.withLock { refreshResults.append(refreshResult) }
if refreshResults.count == (lock.withLock { sources.count }) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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, 👍

@tomerd tomerd merged commit b2342f1 into swiftlang:main Dec 21, 2020
federicobucchi pushed a commit to federicobucchi/swift-package-manager that referenced this pull request Jan 6, 2021
motivation: a failing test hinted at a thread safety issue, which was then diagnosed to be an incorrect lock on an array. this prompted creating a thread safe utility for array to avoid such cases

changes:
* create a thread-safe utlitiy for array
* replace array + lock with the new utility
* add test
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.

2 participants