[glib: 1/2] GWin32AppInfo: Actually report the GPid in the GAppLaunchContext::launched signal




commit 45bdeeddff4b3bfcfff146023b2b7fd69ff2d0a3
Author: Luca Bacci <luca bacci982 gmail com>
Date:   Thu Jul 28 12:21:50 2022 +0200

    GWin32AppInfo: Actually report the GPid in the GAppLaunchContext::launched signal
    
    We need to pass the G_SPAWN_DO_NOT_REAP_CHILD flag to g_spawn_async,
    otherwise the returned child_pid will always be 0.

 gio/gappinfo.c      | 5 +++++
 gio/gwin32appinfo.c | 4 +++-
 glib/gspawn-win32.c | 3 +++
 3 files changed, 11 insertions(+), 1 deletion(-)
---
diff --git a/gio/gappinfo.c b/gio/gappinfo.c
index f1e866ddaa..17f453adab 100644
--- a/gio/gappinfo.c
+++ b/gio/gappinfo.c
@@ -1439,6 +1439,11 @@ g_app_launch_context_class_init (GAppLaunchContextClass *klass)
    * example if the process was launched via D-Bus). The `pid` may not be
    * set at all in subsequent releases.
    *
+   * On Windows, `pid` is guaranteed to be valid only for the duration of the
+   * #GAppLaunchContext::launched signal emission; after the signal is emitted,
+   * GLib will call g_spawn_close_pid(). If you need to keep the #GPid after the
+   * signal has been emitted, then you can duplicate `pid` using `DuplicateHandle()`.
+   *
    * Since: 2.36
    */
   signals[LAUNCHED] = g_signal_new (I_("launched"),
diff --git a/gio/gwin32appinfo.c b/gio/gwin32appinfo.c
index cdb4868438..0960eef5c6 100644
--- a/gio/gwin32appinfo.c
+++ b/gio/gwin32appinfo.c
@@ -4845,7 +4845,8 @@ g_win32_app_info_launch_internal (GWin32AppInfo      *info,
       if (!g_spawn_async (NULL,
                           argv,
                           envp,
-                          spawn_flags,
+                          spawn_flags |
+                          G_SPAWN_DO_NOT_REAP_CHILD,
                           NULL,
                           NULL,
                           &pid,
@@ -4871,6 +4872,7 @@ g_win32_app_info_launch_internal (GWin32AppInfo      *info,
           g_variant_unref (platform_data);
         }
 
+      g_spawn_close_pid (pid);
       g_strfreev (argv);
       argv = NULL;
     }
diff --git a/glib/gspawn-win32.c b/glib/gspawn-win32.c
index 3ce21819f4..5ac4910f85 100644
--- a/glib/gspawn-win32.c
+++ b/glib/gspawn-win32.c
@@ -1417,6 +1417,9 @@ g_spawn_command_line_async (const gchar *command_line,
 void
 g_spawn_close_pid (GPid pid)
 {
+  /* CRT functions such as _wspawn* return (HANDLE)-1
+   * on failure, so check also for that value. */
+  if (pid != NULL && pid != (HANDLE) -1)
     CloseHandle (pid);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]