Re: Crash-on-exit after call to app.processEvents()

Raymond Osborn <[email protected]> Mon, 25 May 2020 16:35:02 -0500
Newsgroups gmane.comp.python.pyqt-pykde
Message-ID <[email protected]>
--Apple-Mail=_F1A9DF5D-4395-45F8-9AD3-8CBE05C38AD2
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

Hi Armando,
Good to hear from you. I thought for a while that I had managed to fix =
it with the following code :

    def main(filename=3DNone):
        app =3D NXConsoleApp()
        app.initialize(filename=3Dfilename)
        app.start()
        for w in QApplication.topLevelWindows():
            del w
        del app.window, app.app
        sys.exit(0)

It proved to be a false dawn - the problem started to recur again. =
I=E2=80=99ll see if your version catches things that mine missed, and =
let you know.

I have spent a lot of time trying to cover up PyQt5 bugs in the past =
year. The problem is that I can=E2=80=99t guarantee that my users will =
have the latest version of PyQt, so even if it is fixed in v5.14, I =
still have to patch the code. I just have no idea why simply calling =
processEvents() triggers the bug. If I knew that, perhaps it would give =
some clue how to work around it.

Ray


> On May 25, 2020, at 3:38 PM, V. Armando Sole <[email protected]> wrote:
>=20
> Hi Ray,
>=20
> I do not know if it will help your case. I was getting rid of some =
crashes on exit by deleting all widgets that had no parent. For =
instance, when closing the QMainWindow by doing:
>=20
>     def closeEvent(self, event):
>         if __name__ =3D=3D "__main__":
>             app =3D qt.QApplication.instance()
>             allWidgets =3D app.allWidgets()
>             for widget in allWidgets:
>                 try:
>                     # we cannot afford to crash here
>                     if id(widget) !=3D id(self):
>                         if widget.parent() is None:
>                             widget.close()
>                 except:
>                     _logger.debug("Error closing widget")
>         return qt.QMainWindow.closeEvent(self, event)
>=20
> Best regards,
>=20
> Armando
>=20
> On 25.05.2020 19:48, Raymond Osborn wrote:
>=20
>> I added a call to QtWidgets.QApplication.instance().processEvents() =
in order to force adjustResize() to work when switching tabs (that's =
another story). It succeeded in fixing my resize issue, but now it =
triggers a segfault when I exit the application. This seemed to be =
related to the bug described in =
https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.html#crashes-=
on-exit =
<https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.html#crashes=
-on-exit>. One fix, suggested by @ekhumoro on stackoverflow =
(https://stackoverflow.com/questions/59120337/why-does-pyqt-sometimes-cras=
h-on-exit =
<https://stackoverflow.com/questions/59120337/why-does-pyqt-sometimes-cras=
h-on-exit>) was to delete the main window and the app first, but that =
doesn't seem to fix it. I am running PyQt 5.12 because that is the last =
version supported by conda, so I can't test the one-exit fix in v5.13, =
and my users probably wouldn't have it installed anyway. It happens on =
Macs and linux.
>> =20
>> What I am asking is if there are additional things to try in addition =
to @ekhumoro's suggestion? Basically he suggests doing something like:
>> =20
>> def main():
>>     app =3D QApplication(sys.argv)
>>     main_window =3D MainWindow()
>>     main_window.show()
>>     app.exec_()
>>     # ensure correct deletion order
>>     del main_window, app
>> Does anyone understand why the crash only occurs if I have calls to =
processEvents() in the code? It never happens when I remove them.
>> =20
>> Thanks,
>> Ray
>> =20
>> =20


--Apple-Mail=_F1A9DF5D-4395-45F8-9AD3-8CBE05C38AD2
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" class=3D"">Hi =
Armando,<div class=3D"">Good to hear from you. I thought for a while =
that I had managed to fix it with the following code :</div><div =
class=3D""><br class=3D""></div><div class=3D""><div class=3D"">&nbsp; =
&nbsp; def main(filename=3DNone):</div><div class=3D"">&nbsp; &nbsp; =
&nbsp; &nbsp; app =3D NXConsoleApp()</div><div class=3D"">&nbsp; &nbsp; =
&nbsp; &nbsp; app.initialize(filename=3Dfilename)</div><div =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; app.start()</div><div =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; for w in =
QApplication.topLevelWindows():</div><div class=3D"">&nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; del w</div><div class=3D"">&nbsp; &nbsp; =
&nbsp; &nbsp; del app.window, app.app</div><div class=3D"">&nbsp; &nbsp; =
&nbsp; &nbsp; sys.exit(0)</div><div class=3D""><br class=3D""></div><div =
class=3D"">It proved to be a false dawn - the problem started to recur =
again. I=E2=80=99ll see if your version catches things that mine missed, =
and let you know.</div><div class=3D""><br class=3D""></div><div =
class=3D"">I have spent a lot of time trying to cover up PyQt5 bugs in =
the past year. The problem is that I can=E2=80=99t guarantee that my =
users will have the latest version of PyQt, so even if it is fixed in =
v5.14, I still have to patch the code. I just have no idea why simply =
calling processEvents() triggers the bug. If I knew that, perhaps it =
would give some clue how to work around it.</div><div class=3D""><br =
class=3D""></div><div class=3D"">Ray</div><div class=3D""><br =
class=3D""></div><div><br class=3D""><blockquote type=3D"cite" =
class=3D""><div class=3D"">On May 25, 2020, at 3:38 PM, V. Armando Sole =
&lt;<a href=3D"mailto:[email protected]" class=3D"">[email protected]</a>&gt; =
wrote:</div><br class=3D"Apple-interchange-newline"><div class=3D""><meta =
http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF-8" =
class=3D""><div style=3D"font-size: 10pt; font-family: =
Verdana,Geneva,sans-serif" class=3D""><p class=3D"">Hi Ray,</p><p =
class=3D"">I do not know if it will help your case. I was getting rid of =
some crashes on exit by deleting all widgets that had no parent. For =
instance, when closing the QMainWindow by doing:</p><p =
class=3D"">&nbsp;&nbsp;&nbsp; def closeEvent(self, event):<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if __name__ =3D=3D =
"__main__":<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; app =3D qt.QApplication.instance()<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; allWidgets =3D app.allWidgets()<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; for widget in allWidgets:<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # we cannot afford =
to crash here<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if id(widget) !=3D =
id(self):<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
; if widget.parent() is None:<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp; widget.close()<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; except:<br =
class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _logger.debug("Error =
closing widget")<br class=3D"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
return qt.QMainWindow.closeEvent(self, event)</p><p class=3D"">Best =
regards,</p><p class=3D"">Armando</p><p class=3D"">On 25.05.2020 19:48, =
Raymond Osborn wrote:</p>
<blockquote type=3D"cite" style=3D"padding: 0 0.4em; border-left: =
#1010ff 2px solid; margin: 0" class=3D""><!-- html ignored --><!-- head =
ignored --><!-- meta ignored -->I added a call =
to&nbsp;QtWidgets.QApplication.instance().processEvents() in order to =
force adjustResize() to work when switching tabs (that's another story). =
It succeeded in fixing my resize issue, but now it triggers a segfault =
when I exit the application. This seemed to be related to the bug =
described in&nbsp;<a =
href=3D"https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.html#=
crashes-on-exit" =
class=3D"">https://www.riverbankcomputing.com/static/Docs/PyQt5/gotchas.ht=
ml#crashes-on-exit</a>. One fix, suggested by @ekhumoro on stackoverflow =
(<a =
href=3D"https://stackoverflow.com/questions/59120337/why-does-pyqt-sometim=
es-crash-on-exit" =
class=3D"">https://stackoverflow.com/questions/59120337/why-does-pyqt-some=
times-crash-on-exit</a>) was to delete the main window and the app =
first, but that doesn't seem to fix it. I am running PyQt 5.12 because =
that is the last version supported by conda, so I can't test the =
one-exit fix in v5.13, and my users probably wouldn't have it installed =
anyway. It happens on Macs and linux.
<div class=3D"">&nbsp;</div>
<div class=3D"">What I am asking is if there are additional things to =
try in addition to @ekhumoro's suggestion? Basically he suggests doing =
something like:</div>
<div class=3D"">&nbsp;</div>
<div class=3D"">
<pre class=3D"lang-py prettyprinted prettyprint" style=3D"margin-top: =
0px; margin-bottom: 1em; padding: 12px 8px; border: 0px; font-family: =
Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu =
Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, =
sans-serif; font-stretch: inherit; line-height: inherit; font-size: =
13px; vertical-align: baseline; box-sizing: inherit; width: auto; =
max-height: 600px; overflow: auto; border-top-left-radius: 3px; =
border-top-right-radius: 3px; border-bottom-right-radius: 3px; =
border-bottom-left-radius: 3px; color: #242729; word-wrap: normal; =
caret-color: #242729;"><code style=3D"margin: 0px; padding: 0px; border: =
0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation =
Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', =
monospace, sans-serif; font-style: inherit; font-variant-caps: inherit; =
font-stretch: inherit; line-height: inherit; vertical-align: baseline; =
box-sizing: inherit; white-space: inherit;" class=3D""><span class=3D"kwd"=
 style=3D"margin: 0px; padding: 0px; border: 0px; font-family: inherit; =
font-style: inherit; font-variant-caps: inherit; font-stretch: inherit; =
line-height: inherit; vertical-align: baseline; box-sizing: inherit; =
color: var(--blue-800);">def</span><span class=3D"pln" style=3D"margin: =
0px; padding: 0px; border: 0px; font-family: inherit; font-style: =
inherit; font-variant-caps: inherit; font-stretch: inherit; line-height: =
inherit; vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);"> main</span><span class=3D"pun" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">():</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">
    app </span><span class=3D"pun" style=3D"margin: 0px; padding: 0px; =
border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">=3D</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);"> </span><span class=3D"typ" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
#2b91af;">QApplication</span><span class=3D"pun" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">(</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">sys</span><span class=3D"pun" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">.</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">argv</span><span class=3D"pun" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">)</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">
    main_window </span><span class=3D"pun" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">=3D</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);"> </span><span class=3D"typ" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
#2b91af;">MainWindow</span><span class=3D"pun" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">()</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">
    main_window</span><span class=3D"pun" style=3D"margin: 0px; padding: =
0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">.</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">show</span><span class=3D"pun" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">()</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">
    app</span><span class=3D"pun" style=3D"margin: 0px; padding: 0px; =
border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">.</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">exec_</span><span class=3D"pun" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">()</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">
    </span><span class=3D"com" style=3D"margin: 0px; padding: 0px; =
border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-400);"># ensure correct deletion order</span><span =
class=3D"pln" style=3D"margin: 0px; padding: 0px; border: 0px; =
font-family: inherit; font-style: inherit; font-variant-caps: inherit; =
font-stretch: inherit; line-height: inherit; vertical-align: baseline; =
box-sizing: inherit; color: var(--black-750);">
    </span><span class=3D"kwd" style=3D"margin: 0px; padding: 0px; =
border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--blue-800);">del</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);"> main_window</span><span class=3D"pun" style=3D"margin:=
 0px; padding: 0px; border: 0px; font-family: inherit; font-style: =
inherit; font-variant-caps: inherit; font-stretch: inherit; line-height: =
inherit; vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);">,</span><span class=3D"pln" style=3D"margin: 0px; =
padding: 0px; border: 0px; font-family: inherit; font-style: inherit; =
font-variant-caps: inherit; font-stretch: inherit; line-height: inherit; =
vertical-align: baseline; box-sizing: inherit; color: =
var(--black-750);"> app</span></code></pre>
<div class=3D"">Does anyone understand why the crash only occurs if I =
have calls to processEvents() in the code? It never happens when I =
remove them.</div>
</div>
<div class=3D"">&nbsp;</div>
<div class=3D"">Thanks,</div>
<div class=3D"">Ray</div>
<div class=3D"">&nbsp;</div>
<div class=3D"">&nbsp;</div>
</blockquote>
</div>
</div></blockquote></div><br class=3D""></div></body></html>=

--Apple-Mail=_F1A9DF5D-4395-45F8-9AD3-8CBE05C38AD2--