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

Mark Operator Closures as @Sendable to Prevent Crashes in Swift 6 Isolated Contexts #2638

Open
5 of 17 tasks
AndreiArdelean1 opened this issue Oct 18, 2024 · 2 comments
Open
5 of 17 tasks

Comments

@AndreiArdelean1
Copy link

Short description of the issue:

In Swift 6, closures created in an isolated context automatically inherit the isolation of that context, unless they are marked as @Sendable. Because of this, creating an operation like map, flatMap, distinctUntilChanged, etc. from an isolated context, but calling it from a different thread, causes the call to crash.

Expected outcome:

No crash should happen even without manually marking the closures as @Sendable. All closures passed to operators that could be executed on another thread, should be marked as @Sendable inside the RxSwift framework.

What actually happens:

Unless manually specifying that the closure is Sendable, the code crashes

@preconcurrency import RxSwift

@MainActor
func test() -> any Disposable {
    return Observable<Int>.just(1)
        // any non main thread scheduler
        .observe(on: RxSwift.SerialDispatchQueueScheduler(qos: .background))
        .map({ $0 + 1 })
        .subscribe()
}

Workaround:

@preconcurrency import RxSwift

@MainActor
func test() -> any Disposable {
    return Observable<Int>.just(1)
        // any non main thread scheduler
        .observe(on: RxSwift.SerialDispatchQueueScheduler(qos: .background))
        .map({ @Sendable in $0 + 1 })
        .subscribe()
}

Example fix (Map.swift):

    public func map<Result>(_ transform: @escaping (Element) throws -> Result)
        -> Observable<Result> {

should be:

    public func map<Result>(_ transform: @escaping @Sendable (Element) throws -> Result)
        -> Observable<Result> {

Proposed fix:

  • do a find and replace for @escaping and replace it with @escaping @Sendable, as most closures that are escaping should also be Sendable
  • make needed classes to conform to the @unchecked Sendable protocol. Most classes already are thread-safe but are not marked as Sendable

RxSwift/RxCocoa/RxBlocking/RxTest version/commit

6.8.0

Platform/Environment

  • iOS
  • macOS
  • tvOS
  • watchOS
  • playgrounds

How easy is to reproduce? (chances of successful reproduce after running the self contained code)

  • easy, 100% repro
  • sometimes, 10%-100%
  • hard, 2% - 10%
  • extremely hard, %0 - 2%

Xcode version:

16.0

⚠️ Fields below are optional for general issues or in case those questions aren't related to your issue, but filling them out will increase the chances of getting your issue resolved. ⚠️

Installation method:

  • CocoaPods
  • Carthage
  • Git submodules

I have multiple versions of Xcode installed:
(so we can know if this is a potential cause of your issue)

  • yes (which ones)
  • no

Level of RxSwift knowledge:
(this is so we can understand your level of knowledge
and formulate the response in an appropriate manner)

  • just starting
  • I have a small code base
  • I have a significant code base
@danielt1263
Copy link
Collaborator

@AndreiArdelean1 Can you submit a PR to that effect?

@AndreiArdelean1
Copy link
Author

@danielt1263 I've created pull request #2639 with the changes

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

No branches or pull requests

2 participants