wxExec Exit problem

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

wxExec Exit problem

GadgetSteve
Hi,

I have 2 wxPython based applications than for a number of reasons need
to be separate applications but one is normally launched from the other,
(both are either running as python processes or as py2exe builds). Call
them Controller & Listener. Controller launches Listener and needs to
know if it exits so uses wx.Execute Async with a process parameter and
has an OnTerminate handler, it can also send a signal to Listener to
tell it to exit, (e.g. because Controller is about to shut down itself).
(There is other signalling between the two but I don't believe that this
is a part of the problem).

My problem is that if Controller is abnormally terminated, e.g. with a
crash or a debugger stop, Listener will still be running. Shouldn't be a
problem as we can just select the Exit menu item on Listener - this is
where it all goes horribly wrong. Listener starts to shut down and then
hangs. The only way to get rid of the window for it is to use process
explorer and kill it or to shut down & restart the computer.

I have narrowed this down to one of two possibilities:
  1. It is hanging because it is trying to execute the wx.Terminate
callback on the now non-existent launching process, (Controller).
  2. It is hanging because it is trying to print to the pipe created by
wx.Execute which of course also no longer exists.

I have looked at wx.Process.Detach but as the process belongs to the
terminated process, (Controller), calling it within Listener doesn't
seem to do anything.

Any thoughts on what I could try next?

wxPython: 4.0.3 msw
wxWidgets: 3.0.5
Python 3.7.0 & 2.7.13 (32 & 64 bit)
Windows 7 & 10.
--
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect
those of my employer.

---
This email has been checked for viruses by AVG.
https://www.avg.com

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: wxExec Exit problem

Anders Munch
Steve Barnes wrote:
>  1. It is hanging because it is trying to execute the wx.Terminate callback on the now non-existent launching process, (Controller).

I don't understand, doesn't the callback happen in the Controller program, since
that's the one that called wx.Execute?

> 2. It is hanging because it is trying to print to the pipe created by wx.Execute which of course also no longer exists.

Writing to a broken pipe should cause an exception, not a hang.
Could it be that an exception did happen, causing a disorderly shutdown that
forgot to close a worker thread?

Suggestion: add a debugging print to your Listener program, right after
app.MainLoop(), showing active threads:
    time.sleep(1)
    print(threading.enumerate())
Normally that should show just a single thread, <MainThread>. Any other threads
are what's keeping your program alive.
The point of the sleep() is to allow for threads that take some time to shut down.

> Any thoughts on what I could try next?

subprocess instead of wx.Execute?  Might be easier to troubleshoot.
It doesn't give you an exact equivalent to the callback argument, but
subprocess.Popen.poll() from a timer event might be good enough.

Speaking of timers: Make sure you don't have any still-running timers when exiting.

regards, Anders

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: wxExec Exit problem

GadgetSteve


On 04/10/2018 15:15, Anders Munch wrote:

> Steve Barnes wrote:
>>   1. It is hanging because it is trying to execute the wx.Terminate callback on the now non-existent launching process, (Controller).
>
> I don't understand, doesn't the callback happen in the Controller program, since
> that's the one that called wx.Execute?
>
>> 2. It is hanging because it is trying to print to the pipe created by wx.Execute which of course also no longer exists.
>
> Writing to a broken pipe should cause an exception, not a hang.
> Could it be that an exception did happen, causing a disorderly shutdown that
> forgot to close a worker thread?
>
> Suggestion: add a debugging print to your Listener program, right after
> app.MainLoop(), showing active threads:
>      time.sleep(1)
>      print(threading.enumerate())
> Normally that should show just a single thread, <MainThread>. Any other threads
> are what's keeping your program alive.
> The point of the sleep() is to allow for threads that take some time to shut down.
>

Anders,

Thanks for that suggestion - it put me on the right track as it gave me
a print out in the normal exit of Listener but it hung without output in
the parent stopped case. Based on that I tried the same in the OnExit
method before a visible change that happened in the hang case and sure
enough it hung before that change. This made me think that the problem
was a no longer existent pipe.

Accordingly I made the print output during exit conditional on the
parent still being running (wx.Process.Exits) and my hang stopped happening.

It is a shame that we don't have a fall-back or time-out method when a
pipe breaks.

--
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect
those of my employer.

---
This email has been checked for viruses by AVG.
https://www.avg.com

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: wxExec Exit problem

Robin Dunn
In reply to this post by Anders Munch
On Thursday, October 4, 2018 at 7:15:30 AM UTC-7, Anders Munch wrote:
Steve Barnes wrote:
> Any thoughts on what I could try next?

subprocess instead of wx.Execute?  

That would be my suggestion as well. It's much more capable, easier to use and fits the Python mindset better. If the subprocess module had existed at the time I probably would not have wrapped wxExecute and wxProcess.

--
Robin

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.