Crash Hunt #2: reading while updating an optional struct
🐛 The problem
A download screen tracks progress for the selected download. A class or actor stores that selection as an optional: nil means nothing is selected.
private var selectedDownload: Download?
To refresh its progress, you call:
selectedDownload?.progress = await loadProgress()
loadProgress() reads selectedDownload?.id to know which download to fetch. That read crashes with Simultaneous accesses, before the API request even starts.
The optional matters here. The same example does not crash when selectedDownload is a non-optional Download.
This crashes in both a class and an actor. The actor is worth mentioning because you might expect it to protect you from conflicting accesses. It prevents parallel access to its isolated state, but this conflict happens within a single call.
🔍 Why it crashes
Swift starts write access to selectedDownload before calling loadProgress(). That access stays open until the assignment finishes.
Reading selectedDownload?.id touches the same storage. Different fields, but one optional struct. Swift rejects the overlapping read and write with a runtime check called exclusivity.
No second task or thread is needed. Actor isolation does not prevent that overlap.
🧪 Full example
Save both files in the same folder. The API call returns a fake value, so no server is needed.
crash.swift
actor DownloadTracker {
struct Download {
let id: String
var progress: Double
}
private var selectedDownload: Download?
func select(id: String) {
selectedDownload = Download(id: id, progress: 0)
}
func refresh() async {
// 🔒 Opens write access to selectedDownload.
selectedDownload?.progress = await loadProgress()
}
private func loadProgress() async -> Double {
// 💥 Reads it while write access is still open.
guard let id = selectedDownload?.id else { return 0 }
return await fetchProgress(for: id)
}
private func fetchProgress(for id: String) async -> Double {
// Fake API response to keep this example runnable.
return 0.5
}
}
@main
struct ReproduceCrash {
static func main() async {
let tracker = DownloadTracker()
await tracker.select(id: "report.pdf")
await tracker.refresh()
}
}
run-crash.sh
#!/usr/bin/env bash
set -e
swiftc -parse-as-library -o /tmp/actor-state-crash crash.swift
/tmp/actor-state-crash
Run from that folder with Swift installed:
chmod +x run-crash.sh
./run-crash.sh
The output includes:
Simultaneous accesses to 0x..., but modification requires exclusive access.
...
Fatal access conflict detected.
✅ Fix: await first, then update state
Replace refresh() with:
func refresh() async {
let progress = await loadProgress()
selectedDownload?.progress = progress
}
Now the read finishes before the write starts.
For a real API request, the selection might change while you wait. Capture the ID first and check it again before applying the result:
func refresh() async {
guard let id = selectedDownload?.id else { return }
let progress = await fetchProgress(for: id)
guard selectedDownload?.id == id else { return }
selectedDownload?.progress = progress
}
Further reading: Swift memory safety and actor reentrancy.