GTK - killing all subprocesses when application window is closed

266 views Asked by At

I noticed that if I kill the app launched from the command line, with CTRL+C - then the subprocesses get killed, while, when clicking the close button of the window title, the process remains running.

static void * spawn_process(void *data)
{
    GError *local_error = NULL;
    GError **error = &local_error;
    GSubprocess *process = g_subprocess_new (
        G_SUBPROCESS_FLAGS_INHERIT_FDS,
        error,
        "podman",
        "system",
        "service",
        "--time=0",
        "unix:///tmp/podman.sock",
        "--log-level=debug",
        NULL
    );
    g_assert_no_error (local_error);
    g_subprocess_wait_check(process, NULL, error);
    g_assert_no_error (local_error);
    g_object_unref (process);
    return NULL;
}

static void activate(GtkApplication *app) {
    GtkWidget *window = gtk_application_window_new(app);
    GThread *process_thread = g_thread_new("Podman service", &spawn_process, NULL);
}

int main(int argc, char **argv) {
    GtkApplication *app = gtk_application_new("com.github.application.name",G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
    int status = g_application_run(G_APPLICATION (app), argc, argv);
    g_object_unref(app);
    return status;
}
1

There are 1 answers

0
ptomato On

I believe this is because you either have to join or abort the thread when you quit the application normally; otherwise it's orphaned and continues to run with no way to stop it. The same goes for the subprocess, you should make sure either to signal it to stop somehow (depends on which process you are spawning) or use g_subprocess_force_exit() when quitting the application.

However, if you are already spawning a subprocess, there is actually no need to start a new thread to do it in! You can use g_subprocess_wait_check_async().