macOS Mail app crashes on startup, after playing around with Mailkit

I was playing around with Mailkit. I wanted to extract some info, and show a notification when an email arrives.

I copied some code that looked alright for notifications and added it to the defailt MailKit template. I ran it, enabled the extension, sent myself an email and.... Mail app crashes

I restart the Mail app, and it instantly crashes on launch.

I try to comment out the notification code that i added, compile it and restart the Mail app, and it still crashes.

Any help for getting my Mail app back to work would be appreciated!

Here is the exact code that made this happen. Don't run it locally unless you want your Mail app to no longer work.

import MailKit
import UserNotifications

class MessageActionHandler: NSObject, MEMessageActionHandler {

  static let shared = MessageActionHandler()
   
  func decideAction(for message: MEMessage, completionHandler: @escaping (MEMessageActionDecision?) -> Void) {
    // The action to take on the message, if any.
    var action: MEMessageActionDecision? = nil
     
    // Check if the subject of the message contains the word Mars.
    // If it does, specify an action to set the background color to red.
    if message.subject.contains("Mars") {
      action = MEMessageActionDecision.action(.setBackgroundColor(.red))
    }
     
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
    var content = UNMutableNotificationContent()
     content.title = "Message1"
         content.subtitle = "subtitle"
         content.body = "body"
         content.sound = UNNotificationSound.default
         var request = UNNotificationRequest(identifier: UUID().uuidString , content: content, trigger: trigger)
         UNUserNotificationCenter.current().add(request)
     
    // Always call the completion handler, passing the action
    // to take, or nil if there's no action.
    completionHandler(action)
  }
}

I was having crash-on-start issues too but it doesn't seem to be consistently reproducible [macOS Ventura 13.4.1 (c) (22F770820d), Mail Version 16.0 (3731.600.7)]

The crash I've been seeing even on the MailKit sample app:

Process:               Mail [2744]
Path:                  /System/Applications/Mail.app/Contents/MacOS/Mail
Identifier:            com.apple.mail
Version:               16.0 (3731.700.6)
Build Info:            Mail_App-3731700006000000~4
Code Type:             X86-64 (Native)
Parent Process:        launchd [1]
User ID:               501

Date/Time:             2023-07-27 12:14:13.0114 +0200
OS Version:            macOS 13.5 (22G74)
Report Version:        12
Bridge OS Version:     7.6 (20P6072)
Anonymous UUID:        B6039EA8-CA31-5C41-EE33-6E114E55702D


Time Awake Since Boot: 620 seconds

System Integrity Protection: enabled

Crashed Thread:        4  Dispatch queue: IMAPNetworkTaskHandler queue (QOS: BACKGROUND)

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000

Termination Reason:    Namespace SIGNAL, Code 6 Abort trap: 6
Terminating Process:   Mail [2744]

Application Specific Information:
abort() called

Does yours have a similar crash on the IMAPNetworkTaskHandler queue?

Known issue of Mail randomly crashing when downloading message while an extension (any extension, even Apple's own sample extension) is enabled. http://www.openradar.me/FB12794488 (among others submitted to Apple)

macOS Mail app crashes on startup, after playing around with Mailkit
 
 
Q