Skip to content

fix: [SDK-5784] cap background tasks off the main thread - #1817

Draft
nan-li wants to merge 1 commit into
mainfrom
nan/sdk-5784
Draft

nan-li wants to merge 1 commit into
mainfrom
nan/sdk-5784

Conversation

@nan-li

@nan-li nan-li commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

One Line Summary

Cap every background task at 20 s off the main thread and track all open tasks per name in OSBackgroundTaskHandlerImpl. SDK-5784, fixes #1814.

Details

Motivation

iOS kills an app with 0x2182BAD2 when a background task stays open past the 30 s limit. Before this change, OSBackgroundTaskHandlerImpl stored only 1 UIBackgroundTaskIdentifier per task name, so a second begin under the same name orphaned the first task. Task expiry also required the main thread, so a busy main thread left tasks open until RunningBoard killed the process.

Scope

  • OSBackgroundTaskHandlerImpl keeps every open task per name, passes the task name to UIKit logs, ends each task at most once, and ends open tasks on setTaskInvalid:.
  • Every open task gets a cancellable 20 s timer on QOS_CLASS_UTILITY so stalled tasks end off the main thread inside the 30 s allowance.
  • Legacy focus-time processors (OSAttributedFocusTimeProcessor, OSUnattributedFocusTimeProcessor, and OSBaseFocusTimeProcessor) stay unchanged because Sessions V2 (FeatureFlag.sdkSessionsV2ApiCutover) replaces them. Until cutover finishes, the 20 s handler cap ends SESSION_OUTCOMES_TASK before its 30 s timer fires. iOS suspends the app cleanly, and saved unsentActiveTime retries on the next open.

Testing

Unit testing

8 unit tests in OSBackgroundTaskHandlerImplTests cover task names, UIBackgroundTaskInvalid, repeated begins per name, per-ID expiry, setTaskInvalid:, off-main-thread timeouts, early cancellation, and synchronous expiry races. Ran OSBackgroundTaskHandlerImplTests, OneSignalTrackerTests, and OneSignalNewSessionTests (19 tests total) on an iPhone 17 Pro simulator (iOS 26.5) with 0 failures, plus swiftlint with 0 violations.

Manual testing

Not run on a physical device or full test plan for this revised scope.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

@nan-li nan-li closed this Nov 23, 2026
@nan-li nan-li reopened this Nov 23, 2026
iOS kills an app with 0x2182BAD2 when a background task stays open past the 30 s limit or gets orphaned by a second begin with the same name.

Legacy focus processors stay unchanged because Sessions V2 replaces them.

Co-authored-by: Cursor <cursoragent@cursor.com>
@nan-li nan-li changed the title fix: [SDK-5784] end session background tasks without the main thread fix: [SDK-5784] cap background tasks off the main thread Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: App terminated ~40–70 s after the device locks: SESSION_OUTCOMES_TASK background task is never ended (0x2182BAD2)

1 participant