[Interest] QTimer::singleshot extending into global destruction phase

Thiago Macieira thiago.macieira at intel.com
Sun May 31 21:48:31 CEST 2020


On Sunday, 31 May 2020 07:09:23 PDT René J.V. Bertin wrote:
> Hi,
> 
> I have an application with a lengthy shutdown procedure that's been known to
> get stuck. So, I added
> 
>         static bool coreShuttingDown = true;
>         QTimer::singleShot(60000, [&] {
>             if (coreShuttingDown) {
>                 // when our time is up, raise the SIGHUP signal that causes
> us to exit "barely cleanly". qCCritical(SHELL) << "We didn't shut down in
> under a minute; calling it quits"; std::raise(SIGHUP);
>             }
>         });
>         core->shutdown();
>         coreShuttingDown = false;

Instead of a timer and SIGHUP, you can just use the alarm() function, which 
will send you a SIGALRM. This also removes the need for the event loop to be 
involved.

> to the cleanup routine. I've assumed that this singleshot timer will be
> destroyed during the global destruction, and most of the time that does
> appear to be the case. Sometimes however this app gets stuck very late in
> the GD phase, apparently in a deadlock that doesn't contain any clue on
> what's causing it (a mutex destroyed before freeing it?).

Ok, but unfortunately if you don't give us some hint of what's going on, we're 
in as much a dark as you are.

BTW, if you're on Windows, remember that you must never BEGIN exiting with 
locked mutexes. You must always unlock all your mutexes and exit all your 
threads BEFORE you call exit(). Failing to do that leads to deadlocks in 
global destructors. That's clearly not your case because you're using Unix 
signals, but since it's required for cross-platform software, you may want to 
take a look at your design anyway.

> I was going to add a lowlevel, SIGALARM based version of this protection;
> should I rather replace the QTimer::singleShot version or can I be certain
> that that cannot be the cause for the deadlock I'm seeing?

We can't answer without more detail.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel System Software Products





More information about the Interest mailing list