Problem 1868

Summary: Infinite loop on exit when geometry left spinning
Product: Geant4 Reporter: Laurie Nevay <laurie.nevay>
Component: visualization/OpenGLAssignee: Laurent Garnier <laurent.garnier>
Status: RESOLVED FIXED    
Severity: normal CC: garnier, laurent.garnier, PRTruscott
Priority: P5    
Version: 10.2   
Hardware: Apple   
OS: Mac OS X   
Attachments: Screenshot of call tree
xcode time profiler trace recorded while exiting
Xcode time profiler on crash with patch applied when exiting via window button

Description Laurie Nevay 2016-06-06 12:40:29 CEST
Created attachment 403 [details]
Screenshot of call tree

If the geometry is left spinning the Qt visualiser, a program using Geant4 will never exit.  Investigating this, it's not due to my program, but the call tree shows 100% of the time being spent in G4OpenGLQtViewer::G4MouseReleaseEvent(QMouseEvent*). With the qt visualiser it's annoyingly easy to leave the geometry rotating - ie if you have any movement at all when you release a click it starts rotating.  Therefore, this is actually quite a common occurrence, although not intentionally.  It does this with any geometry we produce (quite varied).

If the geometry is not rotating, the program will exit normally.

To reproduce: Geometry visualisation with Qt5 visualiser, drag and release to rotate, type exit while geometry is spinning. Window disappears, but program continues with 100% cpu (1 core).

Geant4.10.02.p01, Apple LLVM version 7.3.0 (clang-703.0.29), Mac OSX 10.11.4.  With glxinfo and a bit of grepping, I found for the open gl implementation:

OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: NVIDIA GeForce GT 650M OpenGL Engine
OpenGL version string: 2.1 NVIDIA-10.10.5.2 310.42.25f01
OpenGL shading language version string: 1.20

I get qt5 through macports. This is a print out of the ports relating to qt I have installed for the version info.

py27-pyqt5 @5.5.1_1 (active)
  qt5 @5.5.1_1 (active)
  qt5-qtbase @5.5.1_3 (active)
  qt5-qtcanvas3d @5.5.1_2 (active)
  qt5-qtconnectivity @5.5.1_2 (active)
  qt5-qtdeclarative @5.5.1_2 (active)
  qt5-qtenginio @5.5.1_2 (active)
  qt5-qtgraphicaleffects @5.5.1_2 (active)
  qt5-qtimageformats @5.5.1_4 (active)
  qt5-qtlocation @5.5.1_2 (active)
  qt5-qtmacextras @5.5.1_2 (active)
  qt5-qtmultimedia @5.5.1_2 (active)
  qt5-qtquickcontrols @5.5.1_2 (active)
  qt5-qtsensors @5.5.1_2 (active)
  qt5-qtserialport @5.5.1_2 (active)
  qt5-qtsvg @5.5.1_2 (active)
  qt5-qttools @5.5.1_2 (active)
  qt5-qttranslations @5.5.1_2 (active)
  qt5-qtwebchannel @5.5.1_2 (active)
  qt5-qtwebengine @5.5.1_3 (active)
  qt5-qtwebkit @5.5.1_3 (active)
  qt5-qtwebsockets @5.5.1_2 (active)
  qt5-qtxmlpatterns @5.5.1_2 (active)
  qt5-sqlite-plugin @5.5.1_2 (active)

I've also attached a screenshot of the xcode instruments time profiler call tree while it was running - captured for around 3 mins.  I'll also try and upload the trace itself, but that's ~27Mb.  I can certainly email a dropbox link.
Comment 1 Laurie Nevay 2016-06-06 12:44:14 CEST
Created attachment 404 [details]
xcode time profiler trace recorded while exiting

Hopefully, this should help diagnosing.
Comment 2 Laurent Garnier 2016-06-27 18:05:38 CEST
Hi Laurie, 

Thanks Laurie for this really good bug report!

Yes, that's true that the rotation do not have any "pause" in. Then if you left your geometry spinning, it will take 100% of your CPU. Perhaps we could think about a "micro sleep" when rotating to allow user to do something else than a kind of while(1) loop. 
That will not fix the fact than 99% of your CPU will be used for spinning, but it will prevent that it will never be 100%. And the "exit"should be ok. Let's think about that for the next release.

Cheers, 
Laurent
Comment 3 Laurie Nevay 2016-06-28 12:44:34 CEST
Hello Laurent,

Thanks for you response.  When I leave it spinning, our application / the visualiser uses around 27% cpu of 1 core as reported by the mac activity monitor.  Only after typing exit in the visualiser terminal does this jump to 100% when the geometry is rotating.  As you mentioned, there's no way for it to exit at this point from the code so it gets trapped.  I agree with you - I'm not bothered about the cpu usage when rotating, but just that the program can safely exit with the geometry still rotating.

Thanks!
Laurie
Comment 4 Laurie Nevay 2016-12-10 00:53:15 CET
Hello,

Has there been any progress on this?  This bug still seems to exist in Geant4.10.3 unfortunately.  

The visualiser gets caught in this infinite loop even when the geometry's not actually rotating after moving it.  Rotating it a small amount and then stopping it reduces the cpu usage which indicates it's safe to exit.

This bug causes our application / any geant4 application example to crash on exit around 70% of the time on mac if the geometry has been touched at all.  Our whole group uses macs and experience this (9 people).

Issue 1901 is a duplicate of this.
Comment 5 garnier 2016-12-12 16:10:50 CET
Hi Laurie,

Simple question, how do you exit your program? Ctrl+C, menu button, other ?

Cheers, 
Laurent
Comment 6 Laurent Garnier 2016-12-12 17:20:10 CET
Ok, 
I think I've got it.
Could you try the following (works for Geant4.10.2 and Geant4.10.3) :
In source/interfaces/basic/src/G4UIQt.cc

Around line 210 :
  fMainWindow = new QMainWindow();
  fMainWindow->setAttribute(Qt::WA_DeleteOnClose);  // ADD THIS LINE


Around line 262 :
G4UIQt::~G4UIQt() 
{ 
...
  if (fMainWindow!=NULL) { // TO BE REMOVED
    delete fMainWindow; // TO BE REMOVED
  } // TO BE REMOVED

Tell me if this is better.

Cheers, 
Laurent
Comment 7 Laurent Garnier 2017-01-30 18:27:48 CET
*** Problem 1901 has been marked as a duplicate of this problem. ***
Comment 8 Laurie Nevay 2017-03-29 14:00:47 CEST
Hello,

Apologies for the late reply.

The proposed patch works for me with Geant4.10.02.p03 when I type 'exit' in the UI terminal, but if I exit the application via the window close button (small red button on top of window on mac), the application crashes.  In this case, the application does not exit, but it doesn't seem to use any cpu.  Using ctrl + C then causes a proper crash.

This behaviour only happens when the geometry is spinning.  If it's stationary, the the window close button and exit work fine.
Comment 9 Laurie Nevay 2017-03-29 14:02:05 CEST
Created attachment 449 [details]
Xcode time profiler on crash with patch applied when exiting via window button
Comment 10 garnier 2017-12-13 00:35:24 CET
Ok, Fixed december 2016