[FSSDK-12729] fix: prevent EXC_BAD_ACCESS from premature URLSession deallocation#649
Open
muzahidul-opti wants to merge 3 commits into
Open
[FSSDK-12729] fix: prevent EXC_BAD_ACCESS from premature URLSession deallocation#649muzahidul-opti wants to merge 3 commits into
muzahidul-opti wants to merge 3 commits into
Conversation
The defer block fires at synchronous scope exit — before the async URLSession callback runs — deallocating the session while the request is still in-flight. This causes EXC_BAD_ACCESS, especially when Firebase Performance Monitoring swizzles URLSession delegate methods. Move session.finishTasksAndInvalidate() from the outer defer into the completion callback body (or inner defer for ODP modules) so the session stays alive until the response is fully processed. Affected modules: - DefaultDatafileHandler.downloadDatafile() - DefaultEventDispatcher.sendEvent() - OdpEventApiManager.sendOdpEvents() - OdpSegmentApiManager.fetchSegments() Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
MockUrlSession used lock.async for both incrementing (init) and decrementing (finishTasksAndInvalidate) validSessions. With the URLSession fix moving invalidation into the async callback, the decrement could still be pending when tearDown asserts the counter is zero. Change lock.async to lock.sync so counter updates complete before the caller proceeds, eliminating the race between finishTasksAndInvalidate and the test assertion in tearDown. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR addresses a crash risk (EXC_BAD_ACCESS) caused by invalidating ephemeral URLSession instances too early (via defer on the calling scope), by moving finishTasksAndInvalidate() to run only after each async network callback completes.
Changes:
- Move
session.finishTasksAndInvalidate()from synchronousdeferblocks intoURLSessioncompletion callbacks in Customization + ODP network code. - Ensure the datafile handler also invalidates the session on the guard-failure path.
- Update the test
MockUrlSessionto update itsvalidSessionscounter synchronously.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| Tests/TestUtils/MockUrlSession.swift | Makes validSessions counter updates synchronous for tests. |
| Sources/Customization/DefaultDatafileHandler.swift | Invalidates session after download completion and on request-creation failure. |
| Sources/Customization/DefaultEventDispatcher.swift | Invalidates session at the end of the upload task callback. |
| Sources/ODP/OdpEventApiManager.swift | Invalidates session in the data task callback defer after completion is invoked. |
| Sources/ODP/OdpSegmentApiManager.swift | Invalidates session in the data task callback defer after completion is invoked. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- DefaultDatafileHandler: call completionHandler with failure result when getRequest returns nil, preventing caller deadlock on DispatchGroup.wait() - MockUrlSession: make lock static so all instances share a single serial queue for the static validSessions counter Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
session.finishTasksAndInvalidate()fromdeferblock (synchronous scope exit) into async completion callbacks across all four network modulesRoot Cause
Swift
deferexecutes when the enclosing scope exits — which for these methods is before the URLSession async callback fires. The session is invalidated while the network request is still in-flight, causing use-after-free crashes.Changes
DefaultDatafileHandler.swiftdownloadTaskcallback + guard-failure pathDefaultEventDispatcher.swiftuploadTaskcallbackOdpEventApiManager.swiftdeferblock aftercompletionHandlerOdpSegmentApiManager.swiftdeferblock aftercompletionHandlerTest plan
swift buildpassesdefer { session.finishTasksAndInvalidate() }remains in any affected file🤖 Generated with Claude Code
Issue