Python因libdispatch崩溃的子线程而崩溃

时间:2019-03-21 17:24:36

标签: python python-3.x macos pydicom libdispatch

我正在使用pynetdicom库来接收和处理医学dicom图像。该处理在回调函数“ on_association_released”中执行。但是,在接受某些研究后,由于看起来像是子线程崩溃,它将导致Python崩溃。

在OSX崩溃报告中,似乎是libdispatch库是原因,但不确定如何或为什么。

这是功能:

def on_association_released(self):

    if not self.auto_process:
        self.incoming = []
        return

    dicoms = [Dicom(f=x) for x in self.incoming]
    self.incoming = []
    incoming = Study(dicom_list=dicoms)
    log.info("Incoming study: {incoming}".format(**locals()))
    completed_tasks = {}
    time.sleep(1)
    for task in AVAILABLE_PROCESS_TASKS:

        log.info("Trying task: {task}".format(**locals()))
        process_task = task(study=incoming)

        try:
            if process_task.valid:

                log.info("{incoming} is valid for {process_task}".format(**locals()))
                try:
                    process_task.process()

                except Exception as e:
                    log.warning(
                        'Failed to perform {process_task} on {incoming}: \n {e}'.format(**locals())
                    )

                else:
                    log.info("Completed {process_task} for {incoming} !".format(**locals()))
            else:
                log.warning("{incoming} is not a valid study for {process_task}".format(**locals()))

        except Exception as e:

            log.warning("{incoming} could not be assessed by {process_task}".format(**locals()))

            myemail.nhs_mail(recipients=[admin],
                             subject=f"dicomserver {VERSION}: Failed to start listener",
                             message=f"{incoming} could not be assessed by {process_task}: {e.args}"
                             )

这是来自应用程序日志的最终日志消息:

2019-03-15 12:19:06 I [process.py:on_association_released:171] Incoming study: Study(1.2.826.0.1.2112370.55.1.12145941)

这是OSX崩溃报告:

Process:               Python [84177]
Path:                  /Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python
Identifier:            Python
Version:               3.6.1 (3.6.1)
Code Type:             X86-64 (Native)
Parent Process:        Python [84175]
Responsible:           Terminal [346]
User ID:               503

Date/Time:             2019-03-15 12:19:06.371 +0000
OS Version:            Mac OS X 10.11.6 (15G1108)
Report Version:        11
Anonymous UUID:        E7340644-9523-1C6B-0B2B-74D6043CFED6


Time Awake Since Boot: 590000 seconds

System Integrity Protection: enabled

Crashed Thread:        1

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000110

VM Regions Near 0x110:
--> 
    __TEXT                 0000000100000000-0000000100001000 [    4K] r-x/rwx SM=COW  /Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python

Application Specific Information:
*** multi-threaded process forked ***
crashed on child side of fork pre-exec

这是线程1崩溃跟踪的顶部:

        Thread 1 Crashed:
    0   libdispatch.dylib               0x00007fff8e6cc661 _dispatch_queue_push_queue + 345
    1   libdispatch.dylib               0x00007fff8e6cab06 _dispatch_queue_wakeup_with_qos_slow + 126
    2   libdispatch.dylib               0x00007fff8e6d113f _dispatch_mach_msg_send + 1952
    3   libdispatch.dylib               0x00007fff8e6d08dc dispatch_mach_send + 262
    4   libxpc.dylib                    0x00007fff86858fc9 xpc_connection_send_message_with_reply + 131
    5   com.apple.CoreFoundation        0x00007fff8ef43b3f __66-[CFPrefsSearchListSource generationCountFromListOfSources:count:]_block_invoke_2 + 143
    6   com.apple.CoreFoundation        0x00007fff8ef4396d _CFPrefsWithDaemonConnection + 381
    7   com.apple.CoreFoundation        0x00007fff8ef42af6 __66-[CFPrefsSearchListSource generationCountFromListOfSources:count:]_block_invoke + 150
    8   com.apple.CoreFoundation        0x00007fff8ef42893 -[CFPrefsSearchListSource generationCountFromListOfSources:count:] + 179
    9   com.apple.CoreFoundation        0x00007fff8ef42174 -[CFPrefsSearchListSource alreadylocked_copyDictionary] + 324
    10  com.apple.CoreFoundation        0x00007fff8ef41dbc -[CFPrefsSearchListSource alreadylocked_copyValueForKey:] + 60
    11  com.apple.CoreFoundation        0x00007fff8ef41d4c ___CFPreferencesCopyAppValueWithContainer_block_invoke + 60
    12  com.apple.CoreFoundation        0x00007fff8ef39a70 +[CFPrefsSearchListSource withSearchListForIdentifier:container:perform:] + 608
    13  com.apple.CoreFoundation        0x00007fff8ef397c7 _CFPreferencesCopyAppValueWithContainer + 183
    14  com.apple.SystemConfiguration   0x00007fff998b3a9b SCDynamicStoreCopyProxiesWithOptions + 163
    15  _scproxy.cpython-36m-darwin.so  0x000000010f0f5a63 get_proxy_settings + 35
    16  org.python.python               0x000000010006a604 _PyCFunction_FastCallDict + 436
    17  org.python.python               0x00000001000f33e4 call_function + 612
    18  org.python.python               0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892

1 个答案:

答案 0 :(得分:2)

这个问题看起来与MacOS上的Python长期存在的问题非常相似。

据我了解,根本原因是如果涉及到线程,fork()很难做,除非立即exec()

如果MacOS正在访问libdispatch但尚未fork的某些系统功能,例如Mac OS,如果访问某些系统功能,则MacOS将通过使进程崩溃来再次“保护”可能的陷阱。

不幸的是,这些调用可能发生在意外的地方,例如在堆栈跟踪的位置15处显示的exec中。

有很多与此相关的Python错误(例如123),但据我所知,并没有灵丹妙药。

在您的特定情况下,可以通过使用环境变量_scproxy.cpython-36m-darwin.so运行Python解释器来可能防止崩溃。这样可以避免调用系统配置框架no_proxy=*来查找代理设置。