8000 Misleading error "Sending 'self' risks causing data races" when it's another object that's causing the issue · Issue #82021 · swiftlang/swift · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Misleading error "Sending 'self' risks causing data races" when it's another object that's causing the issue #82021

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

Open
nnoniash opened this issue Jun 5, 2025 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@nnoniash
Copy link
nnoniash commented Jun 5, 2025

Description

The below code results in the following error on the "print(foo)" line:

Sending 'self' risks causing data races
Task-isolated 'self' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses

despite the issue being that "object" is being captured in the main actors closure, not the fact self is being referenced in this closure. Indeed, when I move the guard line out of the main actor, the code compiles fine.

Reproduction

import UIKit

public class SomeClass : UIView {
    let foo = 16092022
    
    public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            
        Task { @MainActor in
            guard let boo = object as? UIScrollView else {
                return
            }

            print(foo)
        }
    }
}

Expected behavior

The error should be clear about what the issue is which is that object is being captured in a main-actor isolated closure.

Environment

Apple Swift version 6.1 (swift-6.1-RELEASE)
Target: arm64-apple-macosx15.0

Additional information

No response

@nnoniash nnoniash added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jun 5, 2025
@adozenlines
Copy link

The error message seems to be correct in context.

@nnoniash
Copy link
Author
nnoniash commented Jun 8, 2025

@adozenlines isn't the problem here that the object is being passed to the task, while the error message indicates that the problem is foo (since that's the only usage of self in the closure) being passed to the task? When I remove that print line from the task, the error appropriately indicates that object being passed to the task is the problem, and when i remove the guard line, there is no problem at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants
0