dk / Prima

prima.eu.org
Other
106 stars 27 forks source link

POE::Loop::Prima, Prima and Signal handling #77

Closed vikasnkumar closed 1 year ago

vikasnkumar commented 1 year ago

Hi @dk I want to capture signals like SEGV, INT and QUIT in my GUI that I built almost 9 years go using Prima. I am porting the GUI to the latest Prima 1.67 version and need to be able to trap SEGV, and maybe other signals, to prevent the UI from crashing outright. Maybe it can also dump a call-stack somehow ?

The issue I am facing is that one of my child windows that uses Prima::Edit is segfaulting when I try to expand the menu, causing the whole application to crash and I need to figure out what is happening.

Is there a debug flag that can be set within Prima to see some more deeper low level details ? Would love some advice here.

For reference, the code which used to work with Prima 1.44 is here.

vikasnkumar commented 1 year ago

OK I found the symptom that causes the crash. If I load my own icon, the menu crashes on open in the child window. if I use the undef value as icon in the MainWindow, and let the child windows use the same ownerIcon, then it works fine.

My MainWindow icon loading code is here.

My child Window creation code is here.

PNG Icon file is here

I have the same problem with loading any other Icon file such as this .

dk commented 1 year ago

Hi Vikas

I'm not sure if that crash is Prima's fault or not, because if it is, I'd suggest just the usual suspects -

perl Makefile.PL DEBUG=1
make -j 20
make install
perl myscript
gdb --core=core perl

and see if that stack trace leads anywhere.

If you want track generic signals, Prima won't help much here even theoretically since it doesn't bother with signals and if whatever happens, happpens mostly inside the event loop. What you can do at most is use code f ex something like this:

use Prima qw(Message);
my $stack = '';
local $SIG{__DIE__} = sub { $stack = Carp::longmess; };
while ( 1) {
    eval { run Prima; };
    last unless $@;
    my $err = $@;
    my $res = message_box(
        'Fatal error',
        $err,
        mb::Abort|mb::Ignore|mb::Error|mb::Retry,
        { buttons => {
            mb::Abort => { text => '~Quit' },
            mb::Retry => { text => '~Trace' }
        }},
    );
    last if $res == mb::Abort;
    message($stack) if $res == mb::Retry;
}
vikasnkumar commented 1 year ago

Thanks @dk. i figured out the issue and it seemed unrelated to icons per se. the app was dying when the menu was being rendered in a bitmap form, somewhere along a dup call during the menu creation.

I built a test script and it had no problems. so i looked into the menu options I was using and one of the menu options was named close_window and I felt that it may be clashing since a lot of the Prima objects are name based. So I changed that name, and voila everything stopped segfaulting.

Thanks for the instructions on how to setup __DIE__ handling and debugging Prima.

dk commented 1 year ago

Hi Vikas

I've got some free time and checked how is Prima doing with signal handlers - not optimally, actually. I've added some fixes, converted the exception dialog to be a full-fledged Prima feature, and also updated documentation with code how to deal with SIG DIE/WARN/etc if one desires.

If you feel like it you're welcome to check out these new features and see if these add any value for you

/dk

vikasnkumar commented 1 year ago

Thanks @dk yes i will check it out since my goal is to get my application to run on Windows, Linux and Mac. Primarily, at least Windows and Linux, but it is so possible that my friend who uses only Apple Macs may want to use it, so i will work to support it. Just setup a used 2014 Mac Mini yesterday that I received from someone for free, and am making sure things work.

So definitely having a standardized signal handling way, and being able to track where and why is useful to have long term.

dk commented 1 year ago

Prima runs on mac through XQuartz, which introduces some quirks but not much really. Much more hassle is the installation of the dependencies needed. I tried to cover that in README but it's never a smooth ride so YMMV

vikasnkumar commented 1 year ago

@dk indeed it was not easy. I got at least my test Prima scripts working with XQuartz and libxft, but my application dies though. I am yet to try out the custom libraries you have suggested in your README. One thing that helped was installed gtk3 from brew, so that reduced a bunch of display errors with the font and it would automatically start the X11 server etc.

Now I get different warnings, and once I experiment with your versions of the library, i'll know more. If I can reproduce something with a test script, I'll open a bug report.

I am updating an application which I wrote in 2014, and at that time with the Mac OSX of that time, it worked just fine on the Mac. So it will work, just needs some minor effort.

dk commented 1 year ago

Hi Vikas,

I've made sig{die} exception a 1st class citizen in Prima, available with use Prima qw(sys::GUIException) - you're welcome to try that if you fell like it. sig{warn} and generic signals are still not handled, but I've added code in documentation that shows how to reuse that exception dialog so the UX is a bit more uniform (see podview 'Prima::Application/Exceptions and signals')

/dk

run4flat commented 1 year ago

Nice!

On Sat, Jan 14, 2023 at 5:28 AM Dmitry Karasik @.***> wrote:

Hi Vikas,

I've made sig{die} exception a 1st class citizen in Prima, available with use Prima qw(sys::GUIException) - you're welcome to try that if you fell like it. sig{warn} and generic signals are still not handled, but I've added code in documentation that shows how to reuse that exception dialog so the UX is a bit more uniform (see podview 'Prima::Application/Exceptions and signals')

/dk

— Reply to this email directly, view it on GitHub https://github.com/dk/Prima/issues/77#issuecomment-1382708924, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB7Q5YQJWKYI2G3KB62WL3WSJ5TTANCNFSM6AAAAAATPIZTJM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." -- Brian Kernighan

vikasnkumar commented 1 year ago

Hi @dk Thank you !! I am using it now in my application and it is working. I do not have to use the __DIE__ signal since I am including your Prima::sys::GUIException class, but I am using a local signal handling function to handle a bunch of other standard signals like SEGV, INT, TERM, HUP, QUIT. I tested it by using the kill command and sending signals to the application.

However, I have not tested on Windows or OS X but will do it this week sometime and report on the Prima github repo directly if I notice anything different.

Will you be making a release for CPAN ? I would love to make my minimum required Prima version be a newer release.

Thank you again @dk .

@run4flat nice to see you online. hope you're doing well.

dk commented 1 year ago

Hi Vikas,

I'm not yet ready for release, I rationalize that there should be enough new features, and these should be relatively bug free ... but somehow that happens strictly every 3 months, and the next quarter ends for me in the end of february... if that's not critical for you, I'd rather wait until then (or probably even later ). That way I can more or less guarantee that the release is usable

dk commented 1 year ago

I've released 1.67_1 for testing purposes, don't know if you can use it and/or ExtUtils::MakeMaker is smart enough to consume that, but so far this is the only release has the new modules included