diff --git a/gio/gapplication.c b/gio/gapplication.c
index b6cdd86..93f108b 100644
--- a/gio/gapplication.c
+++ b/gio/gapplication.c
@@ -1075,12 +1075,30 @@ g_application_hold (GApplication *application)
 }
 
 static gboolean
+idle_shutdown (gpointer data)
+{
+  GApplication *application = G_APPLICATION (data);
+
+  g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
+
+  if (!application->priv->did_shutdown)
+    g_critical ("GApplication subclass '%s' failed to chain up on"
+                " ::shutdown (from end of override function)",
+                G_OBJECT_TYPE_NAME (application));
+
+  return FALSE;
+}
+
+static gboolean
 inactivity_timeout_expired (gpointer data)
 {
   GApplication *application = G_APPLICATION (data);
 
   application->priv->inactivity_timeout_id = 0;
 
+  if (!application->priv->is_remote)
+    g_idle_add (idle_shutdown, application);
+
   return FALSE;
 }
 
@@ -1101,9 +1119,14 @@ g_application_release (GApplication *application)
 {
   application->priv->use_count--;
 
-  if (application->priv->use_count == 0 && application->priv->inactivity_timeout)
-    application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
-                                                              inactivity_timeout_expired, application);
+  if (application->priv->use_count == 0)
+    {
+      if (application->priv->inactivity_timeout)
+        application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
+                                                                  inactivity_timeout_expired, application);
+      else if (!application->priv->is_remote)
+        g_idle_add (idle_shutdown, application);
+    }
 }
 
 /* Activate, Open {{{1 */
@@ -1329,22 +1352,12 @@ g_application_run (GApplication  *application,
         g_timeout_add (10000, inactivity_timeout_expired, application);
     }
 
-  while (application->priv->use_count || application->priv->inactivity_timeout_id)
+  while (!application->priv->did_shutdown)
     {
       g_main_context_iteration (NULL, TRUE);
       status = 0;
     }
 
-  if (!application->priv->is_remote)
-    {
-      g_signal_emit (application, g_application_signals[SIGNAL_SHUTDOWN], 0);
-
-      if (!application->priv->did_shutdown)
-        g_critical ("GApplication subclass '%s' failed to chain up on"
-                    " ::shutdown (from end of override function)",
-                    G_OBJECT_TYPE_NAME (application));
-    }
-
   if (application->priv->impl)
     g_application_impl_flush (application->priv->impl);
 
