realm / realm/SwiftLint

Rule request: [await_within_actor] actor re-entrancy problem may cause a subtle bug

Open
#4,883 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

rule-request
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
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/

  1. 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/

  2. 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
  }
}
  1. Should the rule be configurable, if so what parameters should be configurable?
    Not at this time.

  2. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.