NordicSemiconductor / pynrfjprog

Python wrapper around the nrfjprog dynamic link library (DLL)
Other
74 stars 26 forks source link

Program crash #20

Closed kcjian closed 2 years ago

kcjian commented 3 years ago

I started ten threads, burned ten jlinks, and repeatedly tested them.

if __name__ == '__main__':
    m_file = './mfw_nrf9160_1.2.2.zip'
    a_file = './merged.hex'
    with HighLevel.API() as api:
        devices = api.get_connected_probes()
        while True:
            thread_pool = []
            for i in range(len(devices)):
                a = operationThread(devices[i], m_file, a_file, True, True)
                a.setDaemon(True)
                a.start()
                thread_pool.append(a)
            for t in thread_pool:
                t.join()
            time.sleep(5)

in operationThread:

    def run(self):
        if self.burn_modem:
            lock.acquire()
            try:
                modem_probe = HighLevel.IPCDFUProbe(api, int(self.d_name), HighLevel.CoProcessor.CP_MODEM)
            except:
                print(self.d_name, ":init modem_Jlink error")
                return
            finally:
                lock.release()
            try:
                print(self.d_name, ':Burning modem firmware ')
                err = update_fw(self.d_name, self.modem_file, modem_probe)
                if err:
                    return
                print(self.d_name, ':Burning modem firmware completed ')
                modem_probe.close()
            except:
                print(self.d_name, ":Burning modem firmware error ")
                return

        if self.burn_app:
            lock.acquire()
            try:
                app_probe = HighLevel.DebugProbe(api, int(self.d_name))
            except:
                print(self.d_name, ":init app_Jlink error")
                return
            finally:
                lock.release()
            try:
                print(self.d_name, ':Burning app firmware')
                err = program_app(self.d_name, self.app_file, app_probe)
                if err:
                    return
                print(self.d_name, ':Burning app firmware completed')
                app_probe.close()
            except:
                print(self.d_name, ":Burning app firmware error ")

After a period of time, the program would crash. There was only one log in PyCharm: process finished with exit code 134 (interrupted by signal 6: sigabrt)

But Mac will pop up a dialog, part of which is shown as follows

Process:               Python [74270]
Path:                  /usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python
Identifier:            Python
Version:               3.7.4 (3.7.4)
Code Type:             X86-64 (Native)
Parent Process:        pycharm [57298]
Responsible:           pycharm [57298]
User ID:               501

Date/Time:             2020-12-31 11:47:45.374 +0800
OS Version:            macOS 11.0.1 (20B29)
Report Version:        12
Anonymous UUID:        F911E537-4603-F972-966E-023BD9E059BD

Sleep/Wake UUID:       EC69EBF0-BFC7-4F64-91AB-E803D5EB5AD2

Time Awake Since Boot: 320000 seconds
Time Since Wake:       8600 seconds

System Integrity Protection: disabled

Crashed Thread:        6

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

Application Specific Information:
dyld: in dlopen()
abort() called

Thread 0:: Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib          0x00007fff203098e2 __psynch_cvwait + 10
1   libsystem_pthread.dylib         0x00007fff2033be6f _pthread_cond_wait + 1254
2   org.python.python               0x00000001064f11ec PyThread_acquire_lock_timed + 401
3   org.python.python               0x0000000106529b3e acquire_timed + 104
4   org.python.python               0x000000010652990a lock_PyThread_acquire_lock + 44
5   org.python.python               0x000000010642e560 _PyMethodDef_RawFastCallKeywords + 544
6   org.python.python               0x0000000106432dd1 _PyMethodDescr_FastCallKeywords + 81
7   org.python.python               0x00000001064c2f27 call_function + 801
8   org.python.python               0x00000001064bbc57 _PyEval_EvalFrameDefault + 6396
9   org.python.python               0x00000001064c37a6 _PyEval_EvalCodeWithName + 1870
10  org.python.python               0x000000010642da77 _PyFunction_FastCallKeywords + 225
11  org.python.python               0x00000001064c2ef7 call_function + 753
12  org.python.python               0x00000001064bbc57 _PyEval_EvalFrameDefault + 6396
13  org.python.python               0x00000001064c37a6 _PyEval_EvalCodeWithName + 1870
14  org.python.python               0x000000010642da77 _PyFunction_FastCallKeywords + 225
15  org.python.python               0x00000001064c2ef7 call_function + 753
16  org.python.python               0x00000001064bbc57 _PyEval_EvalFrameDefault + 6396
17  org.python.python               0x00000001064c37a6 _PyEval_EvalCodeWithName + 1870
18  org.python.python               0x00000001064ba2b8 PyEval_EvalCode + 51
19  org.python.python               0x00000001064e894b run_mod + 54
20  org.python.python               0x00000001064e7975 PyRun_FileExFlags + 163
21  org.python.python               0x00000001064e701b PyRun_SimpleFileExFlags + 263
22  org.python.python               0x00000001064ff89e pymain_main + 5389
23  org.python.python               0x00000001064fff80 _Py_UnixMain + 56
24  libdyld.dylib                   0x00007fff20356631 start + 1

Thread 1:
0   libsystem_kernel.dylib          0x00007fff20306e7e mach_msg_trap + 10
1   libsystem_kernel.dylib          0x00007fff203071f0 mach_msg + 60
2   com.apple.CoreFoundation        0x00007fff20433be7 __CFRunLoopServiceMachPort + 316
3   com.apple.CoreFoundation        0x00007fff204322ba __CFRunLoopRun + 1315
4   com.apple.CoreFoundation        0x00007fff204316be CFRunLoopRunSpecific + 563
5   com.apple.CoreFoundation        0x00007fff204b7cc9 CFRunLoopRun + 40
6   libhighlevelnrfjprog.dylib      0x0000000106bb815a NRFDL::Apple::AppleHotplugWorker::threadFunction() + 74
7   libhighlevelnrfjprog.dylib      0x0000000106bb937e void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void (NRFDL::Apple::AppleHotplugWorker::*)(), NRFDL::Apple::AppleHotplugWorker*> >(void*) + 62
8   libsystem_pthread.dylib         0x00007fff2033b950 _pthread_start + 224
9   libsystem_pthread.dylib         0x00007fff2033747b thread_start + 15

Thread 2:
0   libsystem_pthread.dylib         0x00007fff20337458 start_wqthread + 0

Thread 3:
0   libsystem_kernel.dylib          0x00007fff20306e7e mach_msg_trap + 10
1   libsystem_kernel.dylib          0x00007fff203071f0 mach_msg + 60
2   com.apple.framework.IOKit       0x00007fff22b08b88 io_connect_method + 383
3   com.apple.framework.IOKit       0x00007fff22b089d9 IOConnectCallMethod + 244
4   com.apple.iokit.IOUSBLib        0x0000000106d0026f IOUSBInterfaceClass::ReadPipe(unsigned char, void*, unsigned int*, unsigned int, unsigned int) + 189
5   libjlinkarm.6.80.4.dylib        0x00000001290af805 0x128e8a000 + 2250757
6   libjlinkarm.6.80.4.dylib        0x00000001290af051 0x128e8a000 + 2248785
7   libjlinkarm.6.80.4.dylib        0x0000000128fe49b7 0x128e8a000 + 1419703
8   libjlinkarm.6.80.4.dylib        0x0000000128f79dea 0x128e8a000 + 982506
9   libjlinkarm.6.80.4.dylib        0x0000000128ec1fe7 0x128e8a000 + 229351
10  libjlinkarm.6.80.4.dylib        0x0000000128f99947 JLINKARM_CORESIGHT_ReadAPDPReg + 103
11  libjlinkarm_nrf91_nrfjprogdll.dylib 0x00000001289d080b SeggerBackend::just_read_debug_port_register(unsigned char, unsigned int*) + 139
12  libjlinkarm_nrf91_nrfjprogdll.dylib 0x00000001289d071d SeggerBackend::just_is_debug_region_powered(bool&) + 157
13  libjlinkarm_nrf91_nrfjprogdll.dylib 0x00000001289d0b5d SeggerBackend::just_power_debug_region() + 45
14  libjlinkarm_nrf91_nrfjprogdll.dylib 0x00000001289d66e5 SeggerBackend::just_is_connected_to_device(bool&) + 229
15  libjlinkarm_nrf91_nrfjprogdll.dylib 0x00000001289d6ee7 SeggerBackend::just_connect_to_device() + 71
16  libjlinkarm_nrf91_nrfjprogdll.dylib 0x00000001289d8e9d SeggerBackend::read_u32(unsigned int, unsigned int*, bool) + 381
17  libjlinkarm_nrf91_nrfjprogdll.dylib 0x00000001289c38e7 nRF::just_read_u32(unsigned int, unsigned int*) + 87
18  libjlinkarm_nrf91_nrfjprogdll.dylib 0x00000001289c079e nRF::read_u32(unsigned int, unsigned int*) + 174
19  libjlinkarm_nrf91_nrfjprogdll.dylib 0x00000001289bd4c9 NRFJPROG_read_u32 + 41
20  libnrfdfu.dylib                 0x0000000106e3f90b IPCHandler::just_ipc_wait_for_event_and_ack() + 347
21  libnrfdfu.dylib                 0x0000000106e43212 IPCHandler::program_file(std::__1::__fs::filesystem::path) + 2706
22  libnrfdfu.dylib                 0x0000000106e40f67 IPCHandler::program_files(std::__1::vector<std::__1::__fs::filesystem::path, std::__1::allocator<std::__1::__fs::filesystem::path> >) + 2775
23  libnrfdfu.dylib                 0x0000000106e1941f NRFDFU_program_package + 1199
24  libhighlevelnrfjprog.dylib      0x0000000106b8587e DFUProbe::program(std::__1::__fs::filesystem::path const&, program_options_t) + 190
25  libhighlevelnrfjprog.dylib      0x0000000106b53a7d NRFJPROG_program + 269
26  _ctypes.cpython-37m-darwin.so   0x000000010695736f ffi_call_unix64 + 79
27  _ctypes.cpython-37m-darwin.so   0x0000000106957b35 ffi_call + 713
28  _ctypes.cpython-37m-darwin.so   0x0000000106957b35 ffi_call + 713

The complete log is as follows pynrfjprog_log.txt

I don't know if I'm not using it right or if there's something wrong with this library. Now our burning software can't run stably for a long time (sometimes it will crash soon). I hope to get your help.

simtind commented 3 years ago

I see that you don't close your probes when you encounter errors. Do you know if the failing process encountered any pynrfjprog errors?

It might be usefull to make sure that probe.close() is always closed. For example by using a context handler:

def run(self):
    if self.burn_modem:
        try:
            lock.acquire()
            with HighLevel.IPCDFUProbe(api, int(self.d_name), HighLevel.CoProcessor.CP_MODEM) as modem_probe:
                lock.release()
                try:
                    print(self.d_name, ':Burning modem firmware ')
                    err = update_fw(self.d_name, self.modem_file, modem_probe)
                    if err:
                        return
                    print(self.d_name, ':Burning modem firmware completed ')
                except:
                    print(self.d_name, ":Burning modem firmware error ")
                    return
        except:
            print(self.d_name, ":init modem_Jlink error")
            lock.release()
            return

    if self.burn_app:
        try:
            lock.acquire()
            with HighLevel.DebugProbe(api, int(self.d_name)) as app_probe:
                lock.release()
                try:
                    print(self.d_name, ':Burning app firmware')
                    err = program_app(self.d_name, self.app_file, app_probe)
                    if err:
                        return
                    print(self.d_name, ':Burning app firmware completed')
                except:
                    print(self.d_name, ":Burning app firmware error ")
        except:
            print(self.d_name, ":init app_Jlink error")
            lock.release()
            return

Or like this:

def run(self):
    if self.burn_modem:
        with lock.acquire():
            try:
                modem_probe = HighLevel.IPCDFUProbe(api, int(self.d_name), HighLevel.CoProcessor.CP_MODEM)
            except:
                print(self.d_name, ":init modem_Jlink error")
                return

        try:
            print(self.d_name, ':Burning modem firmware ')
            err = update_fw(self.d_name, self.modem_file, modem_probe)
            if err:
                return
            print(self.d_name, ':Burning modem firmware completed ')
        except:
            print(self.d_name, ":Burning modem firmware error ")
            return
        finally:
            modem_probe.close()

    if self.burn_app:
        with lock.acquire():
            try:
                app_probe = HighLevel.DebugProbe(api, int(self.d_name))
            except:
                print(self.d_name, ":init app_Jlink error")
                return
        try:
            print(self.d_name, ':Burning app firmware')
            err = program_app(self.d_name, self.app_file, app_probe)
            if err:
                return
            print(self.d_name, ':Burning app firmware completed')
        except:
            print(self.d_name, ":Burning app firmware error ")
            return
        finally:
            modem_probe.close()
            try:
                app_probe = HighLevel.DebugProbe(api, int(self.d_name))
            except:
                print(self.d_name, ":init app_Jlink error")
                return
            finally:
                lock.release()
            try:
                print(self.d_name, ':Burning app firmware')
                err = program_app(self.d_name, self.app_file, app_probe)
                if err:
                    return
                print(self.d_name, ':Burning app firmware completed')
                app_probe.close()
            except:
                print(self.d_name, ":Burning app firmware error ")
simtind commented 2 years ago

Closing due to inactivity