fix: fix thread-safety crash in Matcher when tests run in parallel#139
Merged
fix: fix thread-safety crash in Matcher when tests run in parallel#139
Conversation
The `Matcher` singleton's `matchers` array is accessed without synchronization. Under Swift Testing's parallel execution, concurrent `register()` writes and `comparator()` reads from multiple test `init()` methods cause data races that crash the test process. Observed crash sites: - `Matcher.register<A>(_:)` — concurrent appends to the array - `closure #1 in Parameter.eraseToGenericValue()` — concurrent reads via `Matcher.comparator(for:)` while another thread appends Add an NSLock to synchronize all matchers array access. The lock is applied at the leaf level (append and snapshot-read) to avoid re-entrancy in the Sequence comparator path which calls `comparator(by:)` → `comparator(for: T.Element.self)` → `comparator(by:)`.
2 tasks
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.
The
Matchersingleton'smatchersarray is accessed without synchronization. Under Swift Testing's parallel execution, concurrentregister()writes andcomparator()reads from multiple testinit()methods cause data races that crash the test process.Observed crash sites:
Matcher.register<A>(_:)— concurrent appends to the arrayclosure #1 in Parameter.eraseToGenericValue()— concurrent reads viaMatcher.comparator(for:)while another thread appendsAdd an NSLock to synchronize all matchers array access. The lock is applied at the leaf level (append and snapshot-read) to avoid re-entrancy in the Sequence comparator path which calls
comparator(by:)→comparator(for: T.Element.self)→comparator(by:).