diff --git a/gio/gapplication.c b/gio/gapplication.c
index b6cdd86..6e004ed 100644
--- a/gio/gapplication.c
+++ b/gio/gapplication.c
@@ -1075,16 +1075,22 @@ g_application_hold (GApplication *application)
 }
 
 static gboolean
-inactivity_timeout_expired (gpointer data)
+emit_shutdown (gpointer data)
 {
   GApplication *application = G_APPLICATION (data);
 
-  application->priv->inactivity_timeout_id = 0;
+  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));
+
+  application->priv->inactivity_timeout = 0;
 
   return FALSE;
 }
 
-
 /**
  * g_application_release:
  * @application: a #GApplication
@@ -1101,9 +1107,18 @@ 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->is_remote)
+        /* Terminate the g_application_run() loop. */
+        application->priv->did_shutdown = TRUE;
+
+      else if (application->priv->inactivity_timeout)
+        application->priv->inactivity_timeout_id = g_timeout_add (application->priv->inactivity_timeout,
+                                                                  emit_shutdown, application);
+      else
+        g_idle_add (emit_shutdown, application);
+    }
 }
 
 /* Activate, Open {{{1 */
@@ -1326,25 +1341,15 @@ g_application_run (GApplication  *application,
       !application->priv->inactivity_timeout_id)
     {
       application->priv->inactivity_timeout_id =
-        g_timeout_add (10000, inactivity_timeout_expired, application);
+        g_timeout_add (10000, emit_shutdown, 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);
 
