Skip to content
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

BUG : RSAtomic implementation is flawed #508

Open
TizianoCoroneo opened this issue Apr 19, 2024 · 1 comment
Open

BUG : RSAtomic implementation is flawed #508

TizianoCoroneo opened this issue Apr 19, 2024 · 1 comment
Assignees
Labels

Comments

@TizianoCoroneo
Copy link

Describe the bug

The current implementation of RSAtomic does not guarantee atomicity. This is because of a couple reasons:

  • it is implemented as a struct property wrapper, which could incur additional copies of the internal state of the atomic wrapper, breaking atomicity.
  • hiding the load and store operations in a getter/setter allows obvious race conditions like value += 1 that will expanded to two separate load and store operations, while it should be implemented as a CAS.

More in-detail explanation here: swiftlang/swift-evolution#1387

To Reproduce
Try this in a playground:

import Foundation

var atomicCounter = RSAtomic(wrappedValue: 0)

DispatchQueue.concurrentPerform(iterations: 1000) { _ in
    atomicCounter.value += 1
}

print(atomicCounter.value)

The result will most often be less than 1000, like in this screenshot:
Screenshot 2024-04-19 at 3 23 32 PM

You should either use DispatchQueues, or actors, or the official Swift Atomics package instead.

Expected behavior
RSAtomic should be atomic.

Version of the iOS SDK
Swift SDK 1.26.2, latest.

SDK initialisation snippet
Irrelevant.

Check for Correct Usage of writeKey and dataPlaneUrl
Irrelevant.

@1abhishekpandey 1abhishekpandey self-assigned this Apr 19, 2024
@1abhishekpandey
Copy link
Contributor

Hey @TizianoCoroneo,
Thank you for bringing this to our attention. We will investigate the matter and get back to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants