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

Fix race conditions in PrimitiveSequence+Concurrency #2641

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

0xpablo
Copy link

@0xpablo 0xpablo commented Nov 4, 2024

This caused continuations to be resumed more than once, crashing clients due to a violation of the Swift Concurrency contract.

I added tests that make it easy to reproduce the issue under the previous implementation

Screenshot 2024-11-04 at 09 38 15

This should fix #2563

This caused continuations to be resumed more than once, crashing clients due to a violation of the Swift Concurrency contract
var didResume = false
let lock = RecursiveLock()
Copy link
Contributor

@nikolaykasyanov nikolaykasyanov Nov 5, 2024

Choose a reason for hiding this comment

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

Take this with a grain of salt, as far as I can tell RecursiveLock is the to-go tool in this codebase.

  • does it have to be recursive?
  • can it be replaced with an atomic & compare-and-swap?

Copy link
Author

@0xpablo 0xpablo Nov 5, 2024

Choose a reason for hiding this comment

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

Hey, that's a good point. I used RecursiveLock precisely because of that, I saw that was the tool being used to synchronize code in the rest of the codebase. I initially thought about that but given that even AtomicInt inside RxSwift is backed by a NSLock, I didn't want to introduce a different way to synchronize this.
If a maintainer wants me to switch to CAS for this I'm happy to do that.

Copy link

@hoc081098 hoc081098 Nov 7, 2024

Choose a reason for hiding this comment

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

I think changing didResume to AtomicInt and using fetchOr might be better 🙏

Copy link
Author

@0xpablo 0xpablo Nov 7, 2024

Choose a reason for hiding this comment

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

I don't think so, AtomicInt is backed by an NSLock so the only thing that would do is making this code less clear.

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.

Crash: value tried to resume its continuation more than once
3 participants