Skip to content

Crash on iOS with URLSessionInstrumentation using swizzling: 'Cannot set task delegate after resumption' #937

@maitbelarbi

Description

@maitbelarbi

When using URLSessionInstrumentation in SwiftUI, the app crashes on iOS with the following exception:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Cannot set task delegate after resumption'

Code Example:

self.urlSessionInstrumentation = URLSessionInstrumentation(
    configuration: URLSessionInstrumentationConfiguration(
        shouldInstrument: { request in
            // filtering applied
        }
    )
)

Environment:

iOS version: 26

Swift version: 6

OpenTelemetry SDK: 2.1.0

SwiftUI app using .task on a view

Steps to Reproduce:

Initialize URLSessionInstrumentation as above.

Trigger a network request with URLSession.

App crashes with the exception.

Expected behavior:
The instrumentation should not crash the app, even if URLSession tasks are resumed/swizzled.

Additional context:

The crash seems related to swizzling and delegate reassignment.

Occurs regardless of shouldInstrument filtering.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions