8000 A simple concurrency safety breach of a var being captured by reference · Issue #76929 · swiftlang/swift · GitHub
[go: up one dir, main page]

Skip to content

A simple concurrency safety breach of a var being captured by reference #76929

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
CrystDragon opened this issue Oct 9, 2024 · 6 comments
Open
Labels
8000 bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features

Comments

@CrystDragon
Copy link
CrystDragon commented Oct 9, 2024

Description

The following code compiles without warnings.

nonisolated  func f() async -> Int {
    var value = 0
    let task1 = Task { @MainActor in
        for _ in 0..<100000 {
            value += 1
        }
    }
    for _ in 0..<100000 {
        value += 1
    }
    await _ = task1.value
    return value
}

print(await f())

But it cannot produce 200000 because of the apparent race on value.

Also, for the following modified code, the compiler will complain "Pattern that the region based isolation checker does not understand how to check".

nonisolated func f() async -> Int {
    var value = 0
    let task1 = Task { @MainActor in
        for _ in 0..<100000 {
            value += 1
        }
    }
    let task2 = Task { @MainActor in
        for _ in 0..<100000 {
            value += 1
        }
    }
    await _ = task1.value
    await _ = task2.value
    return value
}

Reproduction

See above.

Expected behavior

  • There should be warnings/errors for the first example.
  • Solve "Pattern that the region based isolation checker does not understand how to check" for the second example.

Environment

  • Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
  • Target: arm64-apple-macosx14.0
  • Version 16.0 (16A242d)

Additional information

No response

@CrystDragon CrystDragon added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Oct 9, 2024
@NSFatalError
Copy link

This issue seems to be related to the problem I've described on Swift Forums

@ktoso ktoso added concurrency Feature: umbrella label for concurrency language features and removed triage needed This issue needs more specific labels labels Oct 13, 2024
@CrystDragon CrystDragon changed the title Simple concurrency safety breach. A simple concurrency safety breach of a var being captured by reference Oct 17, 2024
@CrystDragon
Copy link
Author

Just tested this using the latest release/6.1 snapshot. There's still no diagnostics.
Isn't this one a crucial regression? @hborla

@CrystDragon
Copy link
Author
CrystDragon commented Jan 14, 2025

Status update after tested against the release/6.1 nightly build.

For the second code snippet, now the compiler can emit a error saying "Sending 'value' risks causing data races"

But for the first code snippet, the compiler still fails to produce any error.
Note that if compiled using Swift 5.9, there will be an error.
in Swift 5.9.

@ktoso Forgive my ignorance, but don't you agree this a particular naive bad case?
Are there any underlying complexities that make this one so hard to address?

@CrystDragon
Copy link
Author

I would argue that in most Swift Concurrency cases, from a developer's view, "a false positive" could be a better option compared to "a false negative".
Personally I'd rather choose to be limited while free-of-error than to be uncertain of correctness.

@hborla
Copy link
Member
hborla commented Mar 27, 2025
8234

Yes, this is a data-race safety hole that needs to be fixed. It's a bug in region based isolation.

@CrystDragon
Copy link
Author

The nightly build can now correctly emit diagnostics for the 2 code snippets in OP. Good work!

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. concurrency Feature: umbrella label for concurrency language features
Projects
None yet
Development

No branches or pull requests

4 participants
0