8000 issue with AsyncShowDialog and AsyncShowDialogModal on macos (abort crash) · Issue #20 · sirk390/wxasync · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

issue with AsyncShowDialog and AsyncShowDialogModal on macos (abort crash) #20

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

Open
BrendanSimon opened this issue Aug 3, 2022 · 4 comments

Comments

@BrendanSimon
Copy link
Contributor

I just checked out the latest master and tried running the examples with latest Python 3.9 and wxPython snapshot on macOS.

I tried with Python 3.10 too.

These might be wxPython snapshot issues and/or macOS specific issues?

(venv-3.9) ➜  examples git:(master) % python dialog.py 
The ReturnCode is 5100 and you entered 'asdfasdfasdf'
objc[60344]: autorelease pool page 0x7fda0f8b5000 corrupted
  magic     0x00000000 0x00000000 0x00000000 0x00000000
  should be 0xa1a1a1a1 0x4f545541 0x454c4552 0x21455341
  pthread   0x10e199600
  should be 0x10e199600

[1]    60344 abort      python dialog.py
(venv-3.9) ➜  examples git:(master) % 
(venv-3.9) ➜  examples git:(master) % python more_dialogs.py
2022-08-03 23:16:45.882 Python[60557:3888182] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007ff8181fe7c3 __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007ff817f5ebc3 objc_exception_throw + 48
	2   CoreFoundation                      0x00007ff818227076 -[NSException raise] + 9
	3   AppKit                              0x00007ff81abe30a4 -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 321
	4   AppKit                              0x00007ff81abcf054 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1288
	5   AppKit                              0x00007ff81ad77d81 -[NSPanel _initContent:styleMask:backing:defer:contentView:] + 50
	6   AppKit                              0x00007ff81abceb46 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 42
	7   AppKit                              0x00007ff81ad77d3a -[NSPanel initWithContentRect:styleMask:backing:defer:] + 59
	8   AppKit                              0x00007ff81b5c3f16 -[NSSavePanel initWithContentRect:styleMask:backing:defer:] + 91
	9   AppKit                              0x00007ff81b5cdb7b -[NSOpenPanel initWithContentRect:styleMask:backing:defer:] + 150
	10  AppKit                              0x00007ff81aeb4078 -[NSPanel init] + 69
	11  AppKit                              0x00007ff81b5c3e98 -[NSSavePanel init] + 197
	12  AppKit                              0x00007ff81b28b8a2 +[NSSavePanel(Instantiation) _crunchyRawUnbonedPanel] + 58
	13  libwx_osx_cocoau_core-3.2.0.0.0.dyl 0x000000010bfa2637 _ZNK11wxDirDialog14OSXCreatePanelEv + 39
	14  libwx_osx_cocoau_core-3.2.0.0.0.dyl 0x000000010bfa29bd _ZN11wxDirDialog9ShowModalEv + 45
	15  _core.cpython-39-darwin.so          0x000000010cc4d9a8 _ZL26meth_wxDirDialog_ShowModalP7_objectS0_ + 104
	16  Python                              0x000000010ac5f896 cfunction_call + 134
	17  Python                              0x000000010ac1c01d _PyObject_Call + 141
	18  Python                              0x000000010ad06d45 _PyEval_EvalFrameDefault + 29989
	19  Python                              0x000000010ac1c283 function_code_fastcall + 163
	20  Python                              0x000000010ad0956d call_function + 413
	21  Python                              0x000000010ad06831 _PyEval_EvalFrameDefault + 28689
	22  Python                              0x000000010ac1c283 function_code_fastcall + 163
	23  Python                              0x000000010ad06d45 _PyEval_EvalFrameDefault + 29989
	24  Python                              0x000000010ac1c283 function_code_fastcall + 163
	25  Python                              0x000000010ad0956d call_function + 413
	26  Python                              0x000000010ad06831 _PyEval_EvalFrameDefault + 28689
	27  Python                              0x000000010ac1c283 function_code_fastcall + 163
	28  Python                              0x000000010ad0956d call_function + 413
	29  Python                              0x000000010ad06831 _PyEval_EvalFrameDefault + 28689
	30  Python                              0x000000010ac1c283 function_code_fastcall + 163
	31  Python                              0x000000010ac1e663 method_vectorcall + 371
	32  Python                              0x000000010adad227 t_bootstrap + 71
	33  Python                              0x000000010ad5b8f9 pythread_wrapper + 25
	34  libsystem_pthread.dylib             0x00007ff8180bc4e1 _pthread_start + 125
	35  libsystem_pthread.dylib             0x00007ff8180b7f6b thread_start + 15
)
libc++abi: terminating with uncaught exception of type NSException
[1]    60557 abort      python more_dialogs.py
(venv-3.9) ➜  examples git:(master) % 
@sirk390
Copy link
Owner
sirk390 commented Oct 3, 2022

Hi @BrendanSimon, thanks for reporting this issue.
I've verified this issue with HtmlHelpDIalog, Fontdialog, FileDIalog, DirDialog, ColourDIalog.
If comes from the fact that on macos "ShowModal" cannot be run from a background thread, and Show doesn't work so there is no way to create an async modal dialog without freezing the event loop.
I'm not sure that is acceptable, what do you think?

See the following code which works on windows but crashes on macos:

import wx
import time
from threading import Thread


class TestFrame(wx.Frame):
    def __init__(self, parent=None):
        super(TestFrame, self).__init__(parent)
        vbox = wx.BoxSizer(wx.VERTICAL)
        button1 =  wx.Button(self, label="Submit")
        vbox.Add(button1, 2, wx.EXPAND|wx.ALL)
        vbox.AddStretchSpacer(1)
        self.Bind(wx.EVT_BUTTON, self.OnOpen, button1)

        self.SetSizer(vbox)
        self.Layout()
        
    def OnOpen(self, event):
        thread = Thread(target=self.thread_main)
        thread.start()

    def thread_main(self):
        with wx.FileDialog(self, "Open XYZ file", wildcard="XYZ files (*.xyz)|*.xyz",
                           style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

            if fileDialog.ShowModal() == wx.ID_CANCEL:
                return


def main():            
    app = wx.App()
    frame = TestFrame()
    frame.Show()
    app.SetTopWindow(frame)
    app.MainLoop()
    

main()

@sirk390 sirk390 changed the title examples abort on macos with wxpython 4.2.0a1.dev5452+d57949fd issue with AsyncShowDialog and AsyncShowDialogModal on macos (abort crash) Oct 3, 2022
@sirk390
Copy link
Owner
sirk390 commented Oct 3, 2022

There are also the options of ShowWindowModal or ShowWindowModalThenDo. It's not so clear what would be the best solution. Would you have any idea?

@BrendanSimon
Copy link
Contributor Author

I'm not aware of the other ShowWindow functions. The wxwigets/wxpython discussion group is probably the best for those.

Probably the best option to call the dialog from a thread is wx.CallAfter (or wx.CallLater).

https://docs.wxpython.org/wx.CallLater.html
https://docs.wxpython.org/wx.functions.html#wx.CallAfter

Does AsyncShowDialog and AsyncShowDialogModal use threads?

I thought the idea of wxasync was to run everything in the main gui thread.

@sirk390
Copy link
Owner
sirk390 commented Oct 4, 2022

OS level dialogs, like FileDialog, DirDialog, ColourDIalog only support ShowModal on windows and will always block the main thread and thus the event loop. The only way around that I know of is to call ShowModal in a background thread. This works under windows, but crashes on macos as you see.

(wx.CallAfter and wx.CallLater won't change anything about this)

I have no idea what pattern could be used for macOS, or how to update wxwidgets to improve this.
On Mac, Show() also doesn't seem to do anything, and ShowModal will block the thread.

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

No branches or pull requests

2 participants
0