You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift CLI工具监听AppleInterfaceThemeChangedNotification时遇运行时异常求助

Swift CLI监听macOS主题切换事件时偶发崩溃

我开发了一个小型Swift CLI工具,用来监听macOS中的AppleInterfaceThemeChangedNotification事件,但运行时偶尔会抛出以下崩溃错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteTask terminationStatus]: task still running'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007ff8171bd12a __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007ff816ce30b7 objc_exception_throw + 48
    2   Foundation                          0x00007ff818046892 -[NSConcreteTask terminationStatus] + 174
    3   main                                0x0000000102eb1715 $s4main8AsyncCLIV3zshys5Int32V_So6NSTaskCSStSSKFZ + 1461
    4   main                                0x0000000102eb07e2 $s4main8AsyncCLIV16updateItermThemeyyFZ + 1810
    5   main                                0x0000000102eb00b9 $s4main8AsyncCLIVAAyyYaKFZy10Foundation12NotificationVYbcfU_ + 25
    6   main                                0x0000000102eb1040 $s10Foundation12NotificationVIeghn_So14NSNotificationCIeyBhy_TR + 128
    7   CoreFoundation                      0x00007ff81713b687 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 120
    8   CoreFoundation                      0x00007ff8171d440e ___CFXRegistrationPost_block_invoke + 88
    9   CoreFoundation                      0x00007ff8171d4363 _CFXRegistrationPost + 536
    10  CoreFoundation                      0x00007ff81710e937 _CFXNotificationPost + 729
    11  CoreFoundation                      0x00007ff81713b75c CFNotificationCenterPostNotificationWithOptions + 121
    12  SkyLight                            0x00007ff81bf2fb50 _ZL41post_coordinated_distributed_notification19CGSNotificationTypePvjS0_ + 241
    13  SkyLight                            0x00007ff81bcea20d CGSPostLocalNotification + 220
    14  SkyLight                            0x00007ff81bce9d20 _ZN12_GLOBAL__N_123notify_datagram_handlerEj15CGSDatagramTypePvmS1_ + 98
    15  SkyLight                            0x00007ff81c0094de _ZN21CGSDatagramReadStream26dispatchMainQueueDatagramsEv + 202
    16  SkyLight                            0x00007ff81c009405 ___ZN21CGSDatagramReadStream15mainQueueWakeupEv_block_invoke + 18
    17  libdispatch.dylib                   0x00007ff816ec7d91 _dispatch_call_block_and_release + 12
    18  libdispatch.dylib                   0x00007ff816ec9033 _dispatch_client_callout + 8
    19  libdispatch.dylib                   0x00007ff816ed5fcf _dispatch_main_queue_drain + 954
    20  libdispatch.dylib                   0x00007ff816ed5c07 _dispatch_main_queue_callback_4CF + 31
    21  CoreFoundation                      0x00007ff8171851a5 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    22  CoreFoundation                      0x00007ff817144ecf __CFRunLoopRun + 2452
    23  CoreFoundation                      0x00007ff817143ed1 CFRunLoopRunSpecific + 560
    24  HIToolbox                           0x00007ff820bc4f3d RunCurrentEventLoopInMode + 292
    25  HIToolbox                           0x00007ff820bc4d4e ReceiveNextEventCommon + 657
    26  HIToolbox                           0x00007ff820bc4aa8 _BlockUntilNextEventMatchingListInModeWithFilter + 64
    27  AppKit                              0x00007ff81a1e025c _DPSNextEvent + 858
    28  AppKit                              0x00007ff81a1df106 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1214
    29  AppKit                              0x00007ff81a1d1788 -[NSApplication run] + 586
    30  main                                0x0000000102eaff34 $s4main8AsyncCLIVAAyyYaKFZTY0_ + 932
    31  main                                0x0000000102eb2361 $s4main8AsyncCLIV5$mainyyYaKFZTQ0_ + 1
    32  main                                0x0000000102eb2561 async_MainTQ0_ + 1
    33  main                                0x0000000102eb26b1 $sIetH_yts5Error_pIegHrzo_TRTQ0_ + 1
    34  main                                0x0000000102eb2961 $sIetH_yts5Error_pIegHrzo_TRTATQ0_ + 1
    35  libswift_Concurrency.dylib          0x00007ffc1c46dc71 _ZL23completeTaskWithClosurePN5swift12AsyncContextEPNS_10SwiftErrorE + 1
)
libc++abi: terminating due to uncaught exception of type NSException

核心代码如下:

import Cocoa

function someFunction(n: Notification) {
    // do something here
}

DistributedNotificationCenter.default.addObserver(
    forName: Notification.Name("AppleInterfaceThemeChangedNotification"),
    object: nil,
    queue: nil) { (notification) in
        someFunction(n: notification)
}

NSApplication.shared.run()

内容的提问来源于stack exchange,提问作者unjankify

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 08:42:06