Rule request: [await_within_actor] actor re-entrancy problem may cause a subtle bug
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
New rule request
Please describe the rule idea:
[await_within_actor] Awaiting within an actor may cause subtle bugs
Actor state may change during suspension points and assumptions made before need to be verified afterwards.
Another approach is to avoid accessing mutable state before an await within a function call or restructuring logic:
https://swiftsenpai.com/swift/actor-reentrancy-problem/
-
Why should this rule be added?
Awaiting within an actor may cause subtle bugs as shown in the link below
https://www.wwdcnotes.com/notes/wwdc21/10133/ -
Example - trigger:
actor ImageDownloader {
private var cache: [URL: Image] = [:]
func image(from url: URL) async throws -> Image? {
if let cached = cache[url] { return cached }
let image = try await downloadImage(from: url) // Trigger
cache[url] = image // 👈🏻 Potential bug: `cache` may have changed
return image
}
}
-
Should the rule be configurable, if so what parameters should be configurable?
Not at this time. -
Should the rule be opt-in or enabled by default? Why?
Opt-in because this may be difficult to guard against false positives as there are many patterns to protect against the 'actor re-entrancy problem'.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the actor re-entrancy example in the issue and the linked Swift concurrency references; no implementation files or tests are identified. Done means the opt-in [await_within_actor] rule's behavior and false-positive boundaries are agreed and verified against representative examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100