Fix DownloadManager sessions racing conditions#4080
Conversation
|
@claude can you review this |
|
Claude finished @SergioEstevao's task in 1m 9s —— View job Code Review
SummaryThis is a small, focused fix that correctly addresses a real threading issue. Swift's What's Good ✅
SuggestionsMissing explanatory comment (main concern)
/// Eagerly initializes all URLSessions to avoid race conditions.
/// Swift lazy properties are not thread-safe: if multiple threads access an
/// uninitialized lazy var concurrently, the initializer can run more than once.
/// Calling this from `init()` guarantees single-threaded first access.
private func setupSessions() {
_ = wifiOnlyBackgroundSession
_ = cellularBackgroundSession
_ = cellularForegroundSession
}
The more idiomatic Swift spelling is Consider making sessions non-lazy (optional / medium) Since TestingRace conditions are inherently hard to unit-test deterministically. The PR checklist correctly notes this was considered. The fix itself is sound; no test gap here. Overall: Approachable fix, correct approach. The main actionable item is adding the explanatory comment to prevent the fix from being accidentally reverted later. |
bjtitus
left a comment
There was a problem hiding this comment.
Fix looks good and I tested downloads over various network conditions 👍
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent DownloadManager URLSession creation race conditions by eagerly initializing the lazy session properties during initialization, and documents the fix in the changelog.
Changes:
- Add
setupSessions()to force initialization of the threeURLSessionlazy properties. - Call
setupSessions()fromDownloadManager.init(dataManager:). - Add an entry to
CHANGELOG.mdfor the fix.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| podcasts/DownloadManager.swift | Forces early creation of the background/foreground URLSessions to avoid lazy-init races. |
| CHANGELOG.md | Adds a release note entry for the DownloadManager session race fix. |
Comments suppressed due to low confidence (1)
podcasts/DownloadManager.swift:172
setupSessions()is invoked beforetempDownloadFolder/ download directories are set up. Creating a backgroundURLSessionwith an existing identifier can immediately start delivering delegate callbacks for pending tasks; those callbacks callpathForEpisode/streamingBufferPathForEpisode(and may need the directories to exist). Consider movingsetupSessions()to the end ofinit(after directory creation) so the filesystem state is ready before any session events can be handled.
init(dataManager: DataManager) {
self.dataManager = dataManager
super.init()
setupSessions()
// setup the temp download folder, in caches where iOS can purge it if need be
let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
if let cachePath = paths.first as NSString? {
tempDownloadFolder = cachePath.appendingPathComponent("temp_download")
| 📘 Part of: # |
|:---:|
Fixes PCIOS-
Ensure lazy sessions are created on init so that no racing condition happen to avoid racing conditions on creation.
Related issues: https://a8c.sentry.io/issues/6791357208/?project=4503921846583296&query=is%3Aunresolved&referrer=issue-stream&sort=freq
To test
Checklist
CHANGELOG.mdif necessary.