Skip to content

Regression crashing intents and notifications #975

@digitaldan

Description

@digitaldan

is looks like #961 changed how preferences are used on the main thread (MainActor) which is causing the notification handler to crash when loading images and also our intent handler to crash when loading anything. Adding this line before we use the Network seems to fix these issue.

       // Ensure Preferences initializes on the MainActor to avoid crashes
        await MainActor.run { _ = Preferences.shared } // <---THIS LINE ADDED

        let connections: [ConnectionConfiguration]

        if let cloudUserId,
           let instance = await Preferences.shared.storedHome(forCloudUserId: cloudUserId) {
            logger.info("Setting up network tracking for \(cloudUserId)")
            connections = await [instance.localConnectionConfig, instance.remoteConnectionConfig]
        } else {
            logger.info("Using default connection configurations")
            let homePreferences = await MainActor.run { Preferences.shared.currentHomePreferences }
            connections = await [
                homePreferences.localConnectionConfig,
                homePreferences.remoteConnectionConfig
            ]
        }

I also added await MainActor.run { _ = Preferences.shared } to all the intent handlers as well.

I'm not sure however this is really the right way to fix? To be honest, what needs to be marked as "MainActor" and does not is still not super clear to me, so while i understand why the fix works, i'm not sure if its just a cheap bandaid. I can open a PR if this is the correct approach, but would like some feedback.

here's the stack trace.

(lldb) bt
* thread #2, queue = 'com.apple.root.default-qos.cooperative', stop reason = EXC_BREAKPOINT (code=1, subcode=0x18017fe60)
    frame #0: 0x000000018017fe60 libdispatch.dylib`_dispatch_assert_queue_fail + 116
    frame #1: 0x00000001801b226c libdispatch.dylib`dispatch_assert_queue$V2.cold.1 + 136
    frame #2: 0x000000018017fde8 libdispatch.dylib`dispatch_assert_queue + 84
    frame #3: 0x0000000249b2d46c libswift_Concurrency.dylib`swift_task_isCurrentExecutorWithFlagsImpl(swift::SerialExecutorRef, swift::swift_task_is_current_executor_flag) + 288
    frame #4: 0x00000001050a884c NotificationService.debug.dylib`HomePreferences.init(id=F36EDA85-1F56-48E9-B3AC-96CB822A5271) at <stdin>:0
  * frame #5: 0x00000001050bb2d0 NotificationService.debug.dylib`Preferences.init() at Preferences.swift:157:6
    frame #6: 0x00000001050ac680 NotificationService.debug.dylib`Preferences.__allocating_init() at Preferences.swift:0
    frame #7: 0x00000001050ac640 NotificationService.debug.dylib`one-time initialization function for shared at Preferences.swift:152:32
    frame #8: 0x0000000180197278 libdispatch.dylib`_dispatch_client_callout + 12
    frame #9: 0x000000018017f3b4 libdispatch.dylib`_dispatch_once_callout + 28
    frame #10: 0x00000001050ac6d8 NotificationService.debug.dylib`Preferences.shared.unsafeMutableAddressor at Preferences.swift:152:23
    frame #11: 0x0000000104ba45d0 NotificationService.debug.dylib`NotificationServiceHandler.networkTracker() at NotificationService.swift:283:45
    frame #12: 0x0000000104ba0ed8 NotificationService.debug.dylib`NotificationServiceHandler.downloadItemImage(itemURI=Swift.String @ 0x000000010380a0a0) at NotificationService.swift:225:30
    frame #13: 0x0000000104ba0460 NotificationService.debug.dylib`NotificationServiceHandler.downloadAndAttachItemImage(itemURI=Swift.String @ 0x0000000103812be0) at NotificationService.swift:213:49
    frame #14: 0x0000000104b9a004 NotificationService.debug.dylib`closure #2 in NotificationServiceHandler.didReceive(attachmentURLString=Swift.String @ 0x0000000103812aa0, bestAttemptContent=0x0000000104404ce0, contentHandler=0x0000000104baba1c NotificationService.debug.dylib`partial apply forwarder for reabstraction thunk helper from @escaping @callee_unowned @convention(block) @Sendable (@unowned __C.UNNotificationContent) -> () to @escaping @callee_guaranteed @Sendable (@guaranteed __C.UNNotificationContent) -> () at <compiler-generated>) at NotificationService.swift:118:35

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions