[Core] Make instance manager conform to Swift 6 principles #13933
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.
Problem:
Static property 'cachedInstances' is not concurrency-safe because it is nonisolated global shared mutable stateThe fix-it to add
nonisolated(unsafe)is for when it's okay toDisable concurrency-safety checks if accesses are protected by an external synchronization mechanism.The "external synchronization mechanism" is the added lock. I went with
NSLockofos_unfair_lockbecause the latter would need to be anotherstatic varand require an extranonisolated(unsafe)attribute. This would be technically okay, but it looked cleaner to use the NSLock. NSLock is slightly slower but I think it would have a negligible effect based on the use case here.Because core internal has < iOS 13 availability, structured concurrency cannot be used.
The added test crashes with the current implementation, and succeeds with this PR's changes.
#no-changelog