Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tkinter_.py] [Mac] Crashes with message "Segmentation fault: 11" #309

Closed
cztomczak opened this issue Mar 1, 2017 · 30 comments
Closed

[tkinter_.py] [Mac] Crashes with message "Segmentation fault: 11" #309

cztomczak opened this issue Mar 1, 2017 · 30 comments

Comments

@cztomczak
Copy link
Owner

cztomczak commented Mar 1, 2017

It often crashes with this message. With system Python it crashes about 50% of the time, with Homebrew Python it crashes 100% of the time.

This error occurs randomly during initial app loading. This issue occurs only on Mac. Below logs:

[tkinter_.py] CEF Python 56.0
[tkinter_.py] Python 2.7.5 
[tkinter_.py] Tk 8.5.9
Segmentation fault: 11

There are some issues with Tk <> CEF initializations, they are conflicting each other. Tk must be initialized before CEF otherwise there was an another error described in Issue #306.

I've tried debugging it using gdb/lldb instructions from KB however I couldn't get a meaningful stack trace. On Linux gdb can show Python stack trace with "py-bt", but I have no idea how to debug this on Mac.

cztomczak added a commit that referenced this issue Mar 1, 2017
Fix tkinter error on Mac "unrecognized selector sent to instance"
(Issue #306"). CEF must be initialized after Tk.

Fix error with winfo_id() returning a negative value (Issue #308).
This was resolved by obtaining NSView pointer using PyObjC
package.

There is yet another error "Segmentation fault: 11" which
crashes app often, however it's hard to debug it (Issue #309).
@cztomczak cztomczak added this to the v56 milestone Mar 1, 2017
@cztomczak cztomczak added the bug label Mar 1, 2017
@cztomczak cztomczak changed the title Mac: tkinter_.py example often crashes with Segmentation fault: 11 [Mac] tkinter_.py example: Crash with message "Segmentation fault: 11" Mar 3, 2017
@cztomczak cztomczak changed the title [Mac] tkinter_.py example: Crash with message "Segmentation fault: 11" [Mac] tkinter_.py example: Crashes with message "Segmentation fault: 11" Mar 3, 2017
@tallforasmurf
Copy link

I am walking through this under Wing IDE. It fails consistently on my system. The crash happens after the call to app.mainloop(). The window appears, then the crash, with no opportunity for user interaction. With log level set to DEBUG the following appear in the log prior to cef.Initialize():

[tkinter_.py] CEF Python 57.0
[tkinter_.py] Python 3.5.1 64bit
[tkinter_.py] Tk 8.5.9

Upon cef.Initialize() this appears,

[0725/124614.059902:ERROR:child_thread_impl.cc(762)] Request for unknown Channel-associated interface: ui::mojom::GpuMain

(which I think is a red herring). Upon the app.mainloop() call these appear:

[tkinter_.py] MainFrame.on_root_configure
[tkinter_.py] MainFrame.on_root_configure
[tkinter_.py] MainFrame.on_configure
[tkinter_.py] MainFrame.on_root_configure

then the window appears and the segfault happens.

By setting a lot of breakpoints I have verified that everything works fine up until entry to embed_browser() on line 158. The crash happens during

self.browser = cef.CreateBrowserSync(window_info,
                                         url="https://www.google.com/")

I can't debug-step into this (actually I can, but the debugger next surfaces in re.py).

@tallforasmurf
Copy link

tallforasmurf commented Jul 25, 2017

Since the hello_world.py example does not crash, the only difference would seem to be the inclusion of "window_info" in the CreateBrowserSync call. Unfortunately window_info is an opaque object from cefWindowInfo() so I can't tell you what's in it.

However, if I delete the window_info argument from the call, the segfault does not occur.

Note that in both hello_world and tkinter_ examples, the resulting window is blank, there is no display of google.com. Is there supposed to be?

Edit: also the qt.py example works, except that it also displays no web page, here is a screen grab:
qtcef

@cztomczak
Copy link
Owner Author

When you remove the window_info parameter then you stop embedding browser inside tkinter window, so it stops being a tkinter example and thus it doesn't crash, because CEF creates the root window itself, just like in the hello world example.

A blank window might appear when your Python does not support GUI applications, see this explanation on KB page: https://github.com/cztomczak/cefpython/blob/master/docs/Knowledge-Base.md#a-blank-window-on-maclinux

@cztomczak
Copy link
Owner Author

Thanks for trying to debug this. The only idea I have for this moment would be to play with various tkinter options - global options or window options. Maybe changing some property of the window or some other option would get rid of the error.

@cztomczak
Copy link
Owner Author

Or maybe the code that fetches window handle is not right - see Issue #308. You could try hardcoding the window handle value in the script - but first you would have to obtain such handle. On Linux you can do this using "xdotool" program, for Mac you would need to google that.

@tallforasmurf
Copy link

My motive in this is to replace QWebEngine in an app based on Python 3.5, PyQt and Qt5.9. The app already works on MacOS, Ubuntu and Windows 7, but the use of QWebEngine prevents bunding it with PyInstaller.

The local Python 3.5 is standard, located in /Library/Frameworks/Python.Framework, as normal when installed from the python.org download. The Mac system Python is at 2.7, located in /System/Library/Frameworks but is not involved in any way.

I haven't touched tkinter in years so hesitate to get into that aspect but will keep poking at this in hopes of learning something useful.

@cztomczak
Copy link
Owner Author

Your issue with a blank window is off-topic here (not a segmentation fault and not a tkinter issue). Please report it on the Forum and I can give you hints how to debug this.

@cztomczak
Copy link
Owner Author

The issue with a blank window is a separate issue that needs be resolved as well. However this is not strictly a tkinter issue because as you said it is also reproducible in other examples. In most cases a blank window is an issue with broken Python and can be easily confirmed by running hello_world.py example using system Python. However like I said that's a separate issue that should be discussed elsewhere.

@tallforasmurf
Copy link

Right, staying on topic of tkinter_.py. This is a pastebin of the full crash log and the following are the most relevant lines:

Thread 0 Crashed:: CrBrowserMain  Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib                 0x00007fff9ad2f0d7 objc_msgSend + 23
1   org.chromium.ContentShell.framework 0x0000000104a59e91 0x103624000 + 21192337
2   org.chromium.ContentShell.framework 0x0000000104a59a2e 0x103624000 + 21191214
3   org.chromium.ContentShell.framework 0x0000000104a597f4 0x103624000 + 21190644
4   org.chromium.ContentShell.framework 0x000000010362ca1d cef_browser_host_create_browser_sync + 557
5   cefpython_py35.so               0x0000000103504b45 CefBrowserHost::CreateBrowserSync(CefWindowInfo const&, scoped_refptr<CefClient>, CefStringBase<CefStringTraitsUTF16> const&, CefStructBase<CefBrowserSettingsTraits> const&, scoped_refptr<CefRequestContext>) + 261
6   cefpython_py35.so               0x00000001034aff0d __pyx_pf_14cefpython_py35_26CreateBrowserSync(_object*, _object*, _object*, _object*, _object*, _object*) + 8269
7   cefpython_py35.so               0x00000001034ad7de __pyx_pw_14cefpython_py35_27CreateBrowserSync(_object*, _object*, _object*) + 238
8   org.python.python               0x0000000100064f89 PyCFunction_Call + 281

That appears to be the stack from the call of CreateBrowserSync in the Python code, and it dies in libobjc. Google "libobjc sendMsg crash" and you get some hits, mostly talking about sending a message to an object that has already been released.

Further info: self.window_width is 0x320
self.window_height is 0x234,
self.get_window_handle() returns 0x2149310 (different every run)

Do you think that the get_window_handle() method is not returning something useful in MacOS?

@cztomczak
Copy link
Owner Author

Some other important info from the full logs:

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

objc_msgSend() selector name: addSubview:

So now we know for sure it fails during embedding browser when adding subview.

Regarding:

Do you think that the get_window_handle() method is not returning something useful in MacOS?

It might be. However why would then embedding browser work 50% of the times as reported originally.

@cztomczak
Copy link
Owner Author

Looks like googling for "addSubview" "KERN_INVALID_ADDRESS" might give some relevant results.

This answer makes some sense:

To further elaborate on why your application is crashing, when a UIViewController is instantiated its view is not created right way. This is called lazy loading, so you are trying to access a value that doesn't exist yet.

Ref: https://stackoverflow.com/a/7134555/623622

So that would explain why it sometimes work and sometimes not. Maybe the window handle returned is "lazy loading" meaning the handle is available, but the view wasn't really yet created?

What if we delayed calling embed_browser() by for example 1 second to let tkinter complete loading its view? Can you try that?

@cztomczak
Copy link
Owner Author

Some other search result mentions "autorelease". There were some fixes in upstream in regards to incorrect use of autorelease pools by CEF [1]. This is supposed to be fixed in CEF v59+, so worth trying running tkinter example using latest version of CEF.

[1] https://bitbucket.org/chromiumembedded/cef/issues/2160/incorrect-use-of-autorelease-pools-in-3071

@cztomczak cztomczak changed the title [Mac] tkinter_.py example: Crashes with message "Segmentation fault: 11" [tkinter_.py] [Mac] Crashes with message "Segmentation fault: 11" Aug 23, 2017
@rafaelthca
Copy link

Hi there,

Thank you creating this amazing python library.

I tried to run the tkinter example with the latest CEF framework (60) downloaded from http://opensource.spotify.com/cefbuilds/index.html.

It doesn't crash with segmentation fault anymore but I get this error:

[0824/113932.392342:INFO:cef_log.cpp(8)] [Browser process] CefInitialize() failed

[0824/113932.483314:INFO:cef_log.cpp(8)] [Browser process] WindowUtils::IsWindowHandle() not implemented (always True)

[0824/113932.483397:INFO:cef_log.cpp(8)] [Browser process] CreateBrowserSync() called

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1559, in __call__
    return self.func(*args)
  File "tkinter_.py", line 194, in on_configure
    self.embed_browser()
  File "tkinter_.py", line 162, in embed_browser
    url="https://www.google.com/")
  File "cefpython_py35.pyx", line 797, in cefpython_py35.CreateBrowserSync (cefpython_py35.cpp:98665)
AssertionError: cefpython.CreateBrowserSync() may only be called on the UI thread

@cztomczak
Copy link
Owner Author

@rafaelthca Your post is off-topic here. Report problems on the Forum.

@rafaelthca
Copy link

Is it? You mentioned that CEF v59+ could be the solution to fix the segmentation fault, I tested with v60 to see if it works

@cztomczak
Copy link
Owner Author

It doesn't work and it's a different error, so this doesn't really say much. By off-topic I mean that that new error is off-topic here and if you want to debug it further we should discuss it elsewhere.

@freegroup
Copy link

Hi,

I tested the tkinter_.py example as well and it ends in segmentation fault: 11 as well.
A normal cefpython is running well, but the integration with tkinter don't.

python ./src/010-image-crawler/tk_demo.py 
[tk_demo.py] CEF Python 66.0
[tk_demo.py] Python 3.7.3 64bit
[tk_demo.py] Tk 8.5.9

DevTools listening on ws://127.0.0.1:54075/devtools/browser/d5e4397c-1c55-4d11-8f59-e3876bcc98e8
GVA info: Successfully connected to the Intel plugin, offline Gen9 
Segmentation fault: 11

Any idea how to fixit?

The main reason why I what to use tkinter is, that I'm unable to bring the main window to front
on startup. It is always behind all other windows. With tkinter, I'm able to bring them on top on startup.

@cztomczak
Copy link
Owner Author

@freegroup Can you post a meaningful stack trace? Is it the same as in this comment #309 (comment) ?

@freegroup
Copy link

how to get the stacktrace?

@cztomczak
Copy link
Owner Author

Did anyone test tkinter example on Mac with Tk 8.6? That newer version of Tk seems to be more stable with CEF, at least on Windows.

@jaywalkerpaz
Copy link

jaywalkerpaz commented Feb 13, 2020

I kept getting segmentation fault 11 every time i opened up a second instance of tk. wtf? well turns out you can only have one instance of tk even though i had destroyed and quit it in the previous module. i avoided this cryptic error by using toplevel instead of creating new root instances each time.. just in case someone had this issue

@cztomczak
Copy link
Owner Author

tkinter example never worked for me on Mac at all. Did you get it working somehow? What version of Tk, python and Mac OS version?

@jaywalkerpaz
Copy link

Actually no.. sorry man I hope u get it

[x.py] CEF Python 66.0
[x.py] Python 3.7.5
[x.py] Tk 8.5.9

DevTools listening on ws://127.0.0.1:64947/devtools/browser/
Segmentation fault: 11

@solydvision
Copy link

2 years! No solution

@cztomczak
Copy link
Owner Author

This still haven't been debugged properly. We need a meaningful stack trace to understand the problem.

@tallforasmurf You've provided stack trace, but the Chromium's part of stack trace does not contain meaningful symbols. You can download CEF symbols from here: http://opensource.spotify.com/cefbuilds/cef_binary_3.3359.1774.gd49d25f_macosx64_release_symbols.tar.bz2

@vollstock
Copy link

Did anyone test tkinter example on Mac with Tk 8.6? That newer version of Tk seems to be more stable with CEF, at least on Windows.

Python 3.7.7
Tk 8.6.8

no improvement

@vollstock
Copy link

I was trying to symbolicate the stack trace provided by @tallforasmurf using your .dSYM file.
However, neither in this, nor in a stack trace of my own did I get any output using this command:

dwarfdump --lookup=0x103624000 Chromium\ Embedded\ Framework.dSYM

Does anybody know whether I am doing this wrong?
Just typing

dwarfdump Chromium\ Embedded\ Framework.dSYM

without an address outputs loads and loads of stuff. Using an address from that output works with the above command.

@yusuf8ahmed
Copy link

yusuf8ahmed commented Jul 16, 2020

Does anyone have a solution to this issue???

@cztomczak
Copy link
Owner Author

Here is the code that crashes:

  CefBrowserHostView* browser_view = [[CefBrowserHostView alloc] initWithFrame:contentRect];
  browser_view.browser = browser_;
  [parentView addSubview:browser_view];

!! CRASH -->  [browser_view setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];

  [browser_view setNeedsDisplay:YES];
  [browser_view release];

Ref: https://bitbucket.org/chromiumembedded/cef/src/d49d25f881b68f418e243e12801cbbb7caebb563/libcef/browser/native/browser_platform_delegate_native_mac.mm#lines-214

@interface CefBrowserHostView : NSView {

Apple docs for NSView autoresizingMask:
https://developer.apple.com/documentation/appkit/nsview/1483281-autoresizingmask

Below is a meaningful stack trace for the crash:

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

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x000000005f84b990
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [1084]

VM Regions Near 0x5f84b990:
-->
    __TEXT                 000000010073c000-000000010073e000 [    8K] r-x/r-x SM=COW  /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

Application Specific Information:
objc_msgSend() selector name: addSubview:


Thread 0 Crashed:: CrBrowserMain  Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib               	0x00007fff6d383817 objc_msgSend + 23
1   org.chromium.ContentShell.framework	0x000000010285df57 CefBrowserPlatformDelegateNativeMac::CreateHostWindow() + 711 (browser_platform_delegate_native_mac.mm:214)
2   org.chromium.ContentShell.framework	0x000000010278862d CefBrowserHostImpl::CreateInternal(CefStructBase<CefBrowserSettingsTraits> const&, scoped_refptr<CefClient>, content::WebContents*, scoped_refptr<CefBrowserInfo>, scoped_refptr<CefBrowserHostImpl>, bool, scoped_refptr<CefRequestContext>, std::__1::unique_ptr<CefBrowserPlatformDelegate, std::__1::default_delete<CefBrowserPlatformDelegate> >, scoped_refptr<CefExtension>) + 589 (browser_host_impl.cc:418)
3   org.chromium.ContentShell.framework	0x00000001027881b3 CefBrowserHostImpl::Create(CefBrowserHostImpl::CreateParams&) + 963 (browser_host_impl.cc:359)
4   org.chromium.ContentShell.framework	0x0000000102787cb8 CefBrowserHost::CreateBrowserSync(CefWindowInfo const&, scoped_refptr<CefClient>, CefStringBase<CefStringTraitsUTF16> const&, CefStructBase<CefBrowserSettingsTraits> const&, scoped_refptr<CefRequestContext>) + 744 (browser_host_impl.cc:276)
5   org.chromium.ContentShell.framework	0x0000000100c64b2d cef_browser_host_create_browser_sync + 557 (browser_host_cpptoc.cc:94)
6   cefpython_py27.so             	0x0000000100b22745 CefBrowserHost::CreateBrowserSync(CefWindowInfo const&, scoped_refptr<CefClient>, CefStringBase<CefStringTraitsUTF16> const&, CefStructBase<CefBrowserSettingsTraits> const&, scoped_refptr<CefRequestContext>) + 261
7   cefpython_py27.so             	0x0000000100a66603 __pyx_pf_14cefpython_py27_30CreateBrowserSync(_object*, _object*, _object*, _object*, _object*, _object*) + 10723
8   cefpython_py27.so             	0x0000000100a638fe __pyx_pw_14cefpython_py27_31CreateBrowserSync(_object*, _object*, _object*) + 238
9   org.python.python             	0x00007fff3f6c5ca6 PyEval_EvalFrameEx + 19119
10  org.python.python             	0x00007fff3f6ca68f 0x7fff3f640000 + 566927
11  org.python.python             	0x00007fff3f6c5b00 PyEval_EvalFrameEx + 18697
12  org.python.python             	0x00007fff3f6c0bec PyEval_EvalCodeEx + 531   
...

cztomczak added a commit that referenced this issue Jul 20, 2020
Still some issues on startup, see: #583
@cztomczak
Copy link
Owner Author

Fixed some of the issues in commit 0da1393 .

There are still startup issues that occur randomly for me and that will be tracked in the newly created issue: #583

Closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants