Skip to content

[minor] [bugfix]: Fix Swift 6 actor-isolation crash for all public callback typedefs#2920

Open
kaisong1990 wants to merge 4 commits into
devfrom
kasong/fix-swift6-crash
Open

[minor] [bugfix]: Fix Swift 6 actor-isolation crash for all public callback typedefs#2920
kaisong1990 wants to merge 4 commits into
devfrom
kasong/fix-swift6-crash

Conversation

@kaisong1990
Copy link
Copy Markdown
Contributor

@kaisong1990 kaisong1990 commented Apr 9, 2026

PR Title Format

Required Format: [Keyword1] [Keyword2]: Description

  • Keyword1: major, minor, or patch (case-insensitive)
  • Keyword2: feature, bugfix, engg, or tests (case-insensitive)

Proposed changes

This pull request annotates all public block typedefs in MSALDefinitions.h with NS_SWIFT_SENDABLE and NS_SWIFT_NONISOLATED to fix Swift 6 runtime crashes (EXC_BREAKPOINT / SIGTRAP) caused by actor-isolation checks when callbacks are invoked on background threads.

Root cause

In Swift 6, closures defined in @MainActor contexts (e.g., SwiftUI App structs) inherit @MainActor isolation. When MSAL invokes these callbacks on background threads (logger serial queue, network completion, etc.), the Swift 6 runtime asserts the closure is on the main thread and crashes with dispatch_assert_queue_fail.

Changes

  • MSALDefinitions.h: Added NS_SWIFT_SENDABLE NS_SWIFT_NONISOLATED to all 8 public block typedefs:
    • MSALCompletionBlock (acquireToken/acquireTokenSilent)
    • MSALAccountsCompletionBlock
    • MSALCurrentAccountCompletionBlock
    • MSALSignoutCompletionBlock
    • MSALDeviceInformationCompletionBlock
    • MSALWPJMetaDataCompletionBlock
    • MSALLogCallback
    • MSALTelemetryCallback
  • IdentityCore submodule: Updated to include matching MSIDLogCallback annotation (PR [minor] [bugfix]: fix crash in Swift 6  microsoft-authentication-library-common-for-objc#1761)
  • CHANGELOG.md: Added entry for this fix

Impact

  • Swift 6 users: Fixes runtime crashes with no code changes needed on their side
  • Swift 5 users: Zero impact — NS_SWIFT_SENDABLE and NS_SWIFT_NONISOLATED are compiler import hints that Swift 5 effectively ignores
  • Source compatibility: @Sendable annotation may produce new compile-time warnings for callers capturing non-Sendable state, but this is preferable to runtime crashes

Fixes #2474

Type of change

  • Feature work
  • Bug fix
  • Documentation
  • Engineering change
  • Test
  • Logging/Telemetry

Risk

  • High
  • Medium – Errors could cause regression of 1 or more scenarios.
  • Small

Mark MSALLogCallback and MSIDLogCallback with NS_SWIFT_SENDABLE and
NS_SWIFT_NONISOLATED to fix Swift 6 strict concurrency crash when log
callbacks are invoked from a non-main-actor context.

Update IdentityCore submodule with matching MSIDLogCallback fix.
@kaisong1990 kaisong1990 requested a review from a team as a code owner April 9, 2026 20:49
@kaisong1990 kaisong1990 changed the title Add NS_SWIFT_SENDABLE and NS_SWIFT_NONISOLATED to log callbacks [minor] [bugfix]: fix crash NS_SWIFT_SENDABLE and NS_SWIFT_NONISOLATED to log callbacks and prevent crash in Swift 6 Apr 9, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates MSAL’s public logging callback type to improve Swift 6 strict concurrency interoperability (avoiding crashes when callbacks are invoked off the main actor), and it also updates the IdentityCore submodule to pick up the corresponding MSIDLogCallback fix.

Changes:

  • Annotate MSALLogCallback with NS_SWIFT_SENDABLE and NS_SWIFT_NONISOLATED for Swift concurrency compatibility.
  • (Per PR description) Update IdentityCore submodule reference to include the matching MSIDLogCallback change.

Comment thread MSAL/src/public/MSALDefinitions.h
…edefs

Annotate all remaining public block typedefs in MSALDefinitions.h with
NS_SWIFT_SENDABLE and NS_SWIFT_NONISOLATED to prevent Swift 6 runtime
actor-isolation crashes when callbacks are invoked on background threads.

Affected typedefs:
- MSALCompletionBlock (acquireToken/acquireTokenSilent)
- MSALAccountsCompletionBlock
- MSALCurrentAccountCompletionBlock
- MSALSignoutCompletionBlock
- MSALDeviceInformationCompletionBlock
- MSALWPJMetaDataCompletionBlock
- MSALTelemetryCallback

MSALLogCallback was already annotated in the previous commit.

Fixes: #2474

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread MSAL/src/public/MSALDefinitions.h
Comment thread MSAL/src/public/MSALDefinitions.h
Comment thread MSAL/src/public/MSALDefinitions.h
Add entry for NS_SWIFT_SENDABLE NS_SWIFT_NONISOLATED annotations on all
public block typedefs to fix Swift 6 runtime crashes (#2474).
@kaisong1990 kaisong1990 requested a review from a team as a code owner April 9, 2026 22:27
@kaisong1990 kaisong1990 changed the title [minor] [bugfix]: fix crash NS_SWIFT_SENDABLE and NS_SWIFT_NONISOLATED to log callbacks and prevent crash in Swift 6 [minor] [bugfix]: Fix Swift 6 actor-isolation crash for all public callback typedefs Apr 9, 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.

Swift 6 Concurrency issue

3 participants