cztomczak / cefpython

Python bindings for the Chromium Embedded Framework (CEF)
Other
3.03k stars 468 forks source link

Mac: Tkinter example randomly crashes on startup #583

Open cztomczak opened 3 years ago

cztomczak commented 3 years ago

Messages in console:

Fatal Python error: PyEval_RestoreThread: NULL tstate
zsh: abort      python tkinter_.py

Getting three different crashes so far.

From the trace I can see that there is sometimes NSAlert modal window being displayed during startup and it is causing issue.

I also sometimes notice alert modal windows sometimes displayed during startup. It is MacOS saying that app previously did not quit cleanly and asks whether to reopen it.

Worth checking if delaying CEF initialization until after Tk windows are initialized and shown on screen (tk.mainloop must run for some time) can help resolve the issue.

It is also worth checking if maybe this issue occurs only when running from command line.

I think that our get_window_handler function may be failing when such alert modal window appears in app:

    def get_window_handle(self):
        if MAC:
            # Do not use self.winfo_id() on Mac, because of these issues:
            # 1. Window id sometimes has an invalid negative value (Issue #308).
            # 2. Even with valid window id it crashes during the call to NSView.setAutoresizingMask:
            #    https://github.com/cztomczak/cefpython/issues/309#issuecomment-661094466
            #
            # To fix it using PyObjC package to obtain window handle. If you change structure of windows then you
            # need to do modifications here as well.
            #
            # There is still one issue with this solution. Sometimes there is more than one window, for example when application
            # didn't close cleanly last time Python displays an NSAlert window asking whether to Reopen that window. In such
            # case app will crash and you will see in console:
            # > Fatal Python error: PyEval_RestoreThread: NULL tstate
            # > zsh: abort      python tkinter_.py
            # Error messages related to this: https://github.com/cztomczak/cefpython/issues/441

            # noinspection PyUnresolvedReferences
            from AppKit import NSApp
            # noinspection PyUnresolvedReferences
            import objc
            logger.info("winfo_id={}".format(self.winfo_id()))
            # noinspection PyUnresolvedReferences
            content_view = objc.pyobjc_id(NSApp.windows()[-1].contentView())
            logger.info("content_view={}".format(content_view))
            return content_view
        elif self.winfo_id() > 0:
            return self.winfo_id()
        else:
            raise Exception("Couldn't obtain window handle")

Stack trace 1:

Crashed Thread:        0  CrBrowserMain  Dispatch queue: com.apple.main-thread

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

Application Specific Information:
abort() called

Thread 0 Crashed:: CrBrowserMain  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib          0x00007fff6e68033a __pthread_kill + 10
1   libsystem_pthread.dylib         0x00007fff6e73ce60 pthread_kill + 430
2   libsystem_c.dylib               0x00007fff6e607808 abort + 120
3   org.python.python               0x00007fff3f70a0ff 0x7fff3f640000 + 827647
4   org.python.python               0x00007fff3f6e1c3a Py_FatalError + 9
5   org.python.python               0x00007fff3f6c0746 PyEval_RestoreThread + 27
6   _tkinter.so                     0x000000010a7e5c65 0x10a7e0000 + 23653
7   Tcl                             0x00007fff41ee756e TclInvokeStringCommand + 121
8   Tcl                             0x00007fff41ee897f 0x7fff41eda000 + 59775
9   Tcl                             0x00007fff41f28891 0x7fff41eda000 + 321681
10  Tcl                             0x00007fff41f2c83b 0x7fff41eda000 + 337979
11  Tcl                             0x00007fff41eea3a1 TclEvalObjEx + 118
12  Tcl                             0x00007fff41f6b1ee 0x7fff41eda000 + 594414
13  Tcl                             0x00007fff41f6b438 0x7fff41eda000 + 595000
14  Tcl                             0x00007fff41f51fe4 Tcl_ServiceEvent + 139
15  Tcl                             0x00007fff41f52389 Tcl_ServiceAll + 164
16  Tcl                             0x00007fff41f80a60 0x7fff41eda000 + 682592
17  com.apple.CoreFoundation        0x00007fff344cd335 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18  com.apple.CoreFoundation        0x00007fff344cd267 __CFRunLoopDoObservers + 457
19  com.apple.CoreFoundation        0x00007fff344cc92c __CFRunLoopRun + 1169
20  com.apple.CoreFoundation        0x00007fff344cbe3e CFRunLoopRunSpecific + 462
21  com.apple.HIToolbox             0x00007fff330f8abd RunCurrentEventLoopInMode + 292
22  com.apple.HIToolbox             0x00007fff330f87d5 ReceiveNextEventCommon + 584
23  com.apple.HIToolbox             0x00007fff330f8579 _BlockUntilNextEventMatchingListInModeWithFilter + 64
24  com.apple.AppKit                0x00007fff3173e039 _DPSNextEvent + 883
25  com.apple.AppKit                0x00007fff3173c880 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1352
26  Tk                              0x00007fff420576d3 0x7fff41fa3000 + 739027
27  com.apple.AppKit                0x00007fff31a3eef2 -[NSApplication _doModalLoop:peek:] + 315
28  com.apple.AppKit                0x00007fff31a3dc85 __35-[NSApplication runModalForWindow:]_block_invoke_2 + 64
29  com.apple.AppKit                0x00007fff31a3dc32 __35-[NSApplication runModalForWindow:]_block_invoke + 70
30  com.apple.AppKit                0x00007fff31a3d4c4 _NSTryRunModal + 100
31  com.apple.AppKit                0x00007fff31a3d3a9 -[NSApplication runModalForWindow:] + 128
32  com.apple.AppKit                0x00007fff31bc67bc __19-[NSAlert runModal]_block_invoke_2 + 147
33  com.apple.AppKit                0x00007fff31bc6716 __19-[NSAlert runModal]_block_invoke + 70
34  com.apple.AppKit                0x00007fff31a3d4c4 _NSTryRunModal + 100
35  com.apple.AppKit                0x00007fff31ad7d87 -[NSAlert runModal] + 121
36  com.apple.AppKit                0x00007fff31744bfe __55-[NSPersistentUIRestorer promptToIgnorePersistentState]_block_invoke + 908
37  com.apple.AppKit                0x00007fff31744833 -[NSApplication _suppressFinishLaunchingFromEventHandlersWhilePerformingBlock:] + 25
38  com.apple.AppKit                0x00007fff317447e3 -[NSPersistentUIRestorer promptToIgnorePersistentState] + 229
39  com.apple.AppKit                0x00007fff317444a4 -[NSApplication _reopenWindowsAsNecessaryIncludingRestorableState:completionHandler:] + 171
40  com.apple.AppKit                0x00007fff317471ec -[NSApplication _sendFinishLaunchingNotification] + 138
41  com.apple.AppKit                0x00007fff3173e3a4 _DPSNextEvent + 1758
42  com.apple.AppKit                0x00007fff3173c880 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1352
43  Tk                              0x00007fff420576d3 0x7fff41fa3000 + 739027
44  Tk                              0x00007fff4205799d 0x7fff41fa3000 + 739741
45  Tcl                             0x00007fff41f52235 Tcl_DoOneEvent + 232
46  _tkinter.so                     0x000000010a7e3adf 0x10a7e0000 + 15071
47  org.python.python               0x00007fff3f6c5bbf PyEval_EvalFrameEx + 18888
48  org.python.python               0x00007fff3f6c0bec PyEval_EvalCodeEx + 531
49  org.python.python               0x00007fff3f6ca6ea 0x7fff3f640000 + 567018
50  org.python.python               0x00007fff3f6c5b00 PyEval_EvalFrameEx + 18697
51  org.python.python               0x00007fff3f6ca68f 0x7fff3f640000 + 566927
52  org.python.python               0x00007fff3f6c5b00 PyEval_EvalFrameEx + 18697
53  org.python.python               0x00007fff3f6c0bec PyEval_EvalCodeEx + 531
54  org.python.python               0x00007fff3f6c09d3 PyEval_EvalCode + 32
55  org.python.python               0x00007fff3f6e29b6 0x7fff3f640000 + 666038
56  org.python.python               0x00007fff3f6e2a5d PyRun_FileExFlags + 130
57  org.python.python               0x00007fff3f6e25e4 PyRun_SimpleFileExFlags + 718
58  org.python.python               0x00007fff3f6f431b Py_Main + 3219
59  libdyld.dylib                   0x00007fff6e538cc9 start + 1

Stack trace 2:

Crashed Thread:        0  CrBrowserMain  Dispatch queue: com.apple.main-thread

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

Application Specific Information:
abort() called

Thread 0 Crashed:: CrBrowserMain  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib          0x00007fff6e68033a __pthread_kill + 10
1   libsystem_pthread.dylib         0x00007fff6e73ce60 pthread_kill + 430
2   libsystem_c.dylib               0x00007fff6e607808 abort + 120
3   org.python.python               0x00007fff3f70a0ff 0x7fff3f640000 + 827647
4   org.python.python               0x00007fff3f6e1c3a Py_FatalError + 9
5   org.python.python               0x00007fff3f6c0746 PyEval_RestoreThread + 27
6   _tkinter.so                     0x000000010e4ddc65 0x10e4d8000 + 23653
7   Tcl                             0x00007fff41ee756e TclInvokeStringCommand + 121
8   Tcl                             0x00007fff41ee897f 0x7fff41eda000 + 59775
9   Tcl                             0x00007fff41f28891 0x7fff41eda000 + 321681
10  Tcl                             0x00007fff41f2c83b 0x7fff41eda000 + 337979
11  Tcl                             0x00007fff41eea3a1 TclEvalObjEx + 118
12  Tcl                             0x00007fff41f6b1ee 0x7fff41eda000 + 594414
13  Tcl                             0x00007fff41f6b438 0x7fff41eda000 + 595000
14  Tcl                             0x00007fff41f51fe4 Tcl_ServiceEvent + 139
15  Tcl                             0x00007fff41f5228c Tcl_DoOneEvent + 319
16  _tkinter.so                     0x000000010e4dbadf 0x10e4d8000 + 15071
17  org.python.python               0x00007fff3f6c5bbf PyEval_EvalFrameEx + 18888
18  org.python.python               0x00007fff3f6c0bec PyEval_EvalCodeEx + 531
19  org.python.python               0x00007fff3f6ca6ea 0x7fff3f640000 + 567018
20  org.python.python               0x00007fff3f6c5b00 PyEval_EvalFrameEx + 18697
21  org.python.python               0x00007fff3f6ca68f 0x7fff3f640000 + 566927
22  org.python.python               0x00007fff3f6c5b00 PyEval_EvalFrameEx + 18697
23  org.python.python               0x00007fff3f6c0bec PyEval_EvalCodeEx + 531
24  org.python.python               0x00007fff3f6c09d3 PyEval_EvalCode + 32
25  org.python.python               0x00007fff3f6e29b6 0x7fff3f640000 + 666038
26  org.python.python               0x00007fff3f6e2a5d PyRun_FileExFlags + 130
27  org.python.python               0x00007fff3f6e25e4 PyRun_SimpleFileExFlags + 718
28  org.python.python               0x00007fff3f6f431b Py_Main + 3219
29  libdyld.dylib                   0x00007fff6e538cc9 start + 1

Stack trace 3:

Crashed Thread:        0  CrBrowserMain  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes:       0x0000000000000001, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Illegal instruction: 4
Termination Reason:    Namespace SIGNAL, Code 0x4
Terminating Process:   exc handler [2504]

Application Specific Information:
Crashing on exception: -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fd2c644c3d0

Application Specific Backtrace 1:
0   CoreFoundation                      0x00007fff34549b57 __exceptionPreprocess + 250
1   libobjc.A.dylib                     0x00007fff6d3905bf objc_exception_throw + 48
2   CoreFoundation                      0x00007fff345c8be7 -[NSObject(NSObject) __retain_OA] + 0
3   CoreFoundation                      0x00007fff344ae3bb ___forwarding___ + 1427
4   CoreFoundation                      0x00007fff344add98 _CF_forwarding_prep_0 + 120
5   Tk                                  0x00007fff42050f50 TkpInit + 655
6   Tk                                  0x00007fff41fd10f7 Tk_Init + 1706
7   _tkinter.so                         0x0000000115bf2bc4 Tcl_AppInit + 84
8   _tkinter.so                         0x0000000115bf2598 init_tkinter + 20648
9   Python                              0x00007fff3f6c5bbf PyEval_EvalFrameEx + 18888
10  Python                              0x00007fff3f6c0bec PyEval_EvalCodeEx + 531
11  Python                              0x00007fff3f667aa8 PyFunction_SetClosure + 772
12  Python                              0x00007fff3f64a143 PyObject_Call + 97
13  Python                              0x00007fff3f6548f4 PyMethod_New + 1169
14  Python                              0x00007fff3f64a143 PyObject_Call + 97
15  Python                              0x00007fff3f6c9feb PyEval_CallObjectWithKeywords + 159
16  Python                              0x00007fff3f652c31 PyInstance_New + 123
17  Python                              0x00007fff3f64a143 PyObject_Call + 97
18  Python                              0x00007fff3f6c5bad PyEval_EvalFrameEx + 18870
19  Python                              0x00007fff3f6c0bec PyEval_EvalCodeEx + 531
20  Python                              0x00007fff3f6ca6ea _PyEval_SliceIndexNotNone + 459
21  Python                              0x00007fff3f6c5b00 PyEval_EvalFrameEx + 18697
22  Python                              0x00007fff3f6c0bec PyEval_EvalCodeEx + 531
23  Python                              0x00007fff3f6c09d3 PyEval_EvalCode + 32
24  Python                              0x00007fff3f6e29b6 PyParser_ASTFromFile + 279
25  Python                              0x00007fff3f6e2a5d PyRun_FileExFlags + 130
26  Python                              0x00007fff3f6e25e4 PyRun_SimpleFileExFlags + 718
27  Python                              0x00007fff3f6f431b Py_Main + 3219
28  libdyld.dylib                       0x00007fff6e538cc9 start + 1

Thread 0 Crashed:: CrBrowserMain  Dispatch queue: com.apple.main-thread
0   com.apple.AppKit                0x00007fff31b20373 -[NSApplication _crashOnException:] + 106
1   com.apple.AppKit                0x00007fff31b20256 -[NSApplication reportException:] + 901
2   com.apple.AppKit                0x00007fff31bcfd8d uncaughtErrorProc + 145
3   com.apple.CoreFoundation        0x00007fff345c293c __handleUncaughtException + 726
4   libobjc.A.dylib                 0x00007fff6d3925a3 _objc_terminate() + 90
5   libc++abi.dylib                 0x00007fff6b865887 std::__terminate(void (*)()) + 8
6   libc++abi.dylib                 0x00007fff6b8681a2 __cxxabiv1::failed_throw(__cxxabiv1::__cxa_exception*) + 27
7   libc++abi.dylib                 0x00007fff6b868169 __cxa_throw + 113
8   libobjc.A.dylib                 0x00007fff6d3906ed objc_exception_throw + 350
9   com.apple.CoreFoundation        0x00007fff345c8be7 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
10  com.apple.CoreFoundation        0x00007fff344ae3bb ___forwarding___ + 1427
11  com.apple.CoreFoundation        0x00007fff344add98 _CF_forwarding_prep_0 + 120
12  Tk                              0x00007fff42050f50 TkpInit + 655
13  Tk                              0x00007fff41fd10f7 0x7fff41fa3000 + 188663
14  _tkinter.so                     0x0000000115bf2bc4 Tcl_AppInit + 84
15  _tkinter.so                     0x0000000115bf2598 0x115bec000 + 26008
16  org.python.python               0x00007fff3f6c5bbf PyEval_EvalFrameEx + 18888
17  org.python.python               0x00007fff3f6c0bec PyEval_EvalCodeEx + 531
18  org.python.python               0x00007fff3f667aa8 0x7fff3f640000 + 162472
19  org.python.python               0x00007fff3f64a143 PyObject_Call + 97
20  org.python.python               0x00007fff3f6548f4 0x7fff3f640000 + 84212
21  org.python.python               0x00007fff3f64a143 PyObject_Call + 97
22  org.python.python               0x00007fff3f6c9feb PyEval_CallObjectWithKeywords + 159
23  org.python.python               0x00007fff3f652c31 PyInstance_New + 123
24  org.python.python               0x00007fff3f64a143 PyObject_Call + 97
25  org.python.python               0x00007fff3f6c5bad PyEval_EvalFrameEx + 18870
26  org.python.python               0x00007fff3f6c0bec PyEval_EvalCodeEx + 531
27  org.python.python               0x00007fff3f6ca6ea 0x7fff3f640000 + 567018
28  org.python.python               0x00007fff3f6c5b00 PyEval_EvalFrameEx + 18697
29  org.python.python               0x00007fff3f6c0bec PyEval_EvalCodeEx + 531
30  org.python.python               0x00007fff3f6c09d3 PyEval_EvalCode + 32
31  org.python.python               0x00007fff3f6e29b6 0x7fff3f640000 + 666038
32  org.python.python               0x00007fff3f6e2a5d PyRun_FileExFlags + 130
33  org.python.python               0x00007fff3f6e25e4 PyRun_SimpleFileExFlags + 718
34  org.python.python               0x00007fff3f6f431b Py_Main + 3219
35  libdyld.dylib                   0x00007fff6e538cc9 start + 1

Other tkinter Mac issues: https://github.com/cztomczak/cefpython/issues/441 https://github.com/cztomczak/cefpython/issues/309

elampietti commented 3 years ago

Thanks for creating this library, I'm currently using it in my tkinter application to have a tab that acts as an embedded web browser. It works fine on Windows however I'm experiencing these crashes on Mac.

Have their been any updates on this issue or is there a work around to be able to consistently display webpages without crashing?