Problem with GLib.spawn_async / GLib.child_watch_add




I need to be able to run a command from an extension, and as this may take a while I want to run it async and catch the process completing. However, there's something screwy with spawn_async that's stopping the child_watch_add from working. gnome-shell itself has code akin to this, so I don't know what I'm doing wrong.

In the following test case, a “sleep” is spawned, but if you do a “pstree -pal” while it's active, the child process is actually not created as a child. So, when the child_watch_add tries to do a waitpid() call (ref. strace -eprocess -f) it fails with ECHILD (pid is not a child) and my callback never occurs.

  Is it broken or am I doing something stupid?


#!/usr/bin/gjs

const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;

// Initialize GTK+
Gtk.init(null, 0);


// Supposed to handle end of process
function jobby()
{
    print("In jobby");
}


let [success, pid] = GLib.spawn_async(
    null,
    ['sleep', '5'],
    null,
    GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.NO_NOT_REAP_CHILD,
    null );
if (success  &&  pid != 0)
{
    // Wait for answer
    print("created process, pid=" + pid);
GLib.child_watch_add( GLib.PRIORITY_DEFAULT, pid, function(pid,status) {
        GLib.spawn_close_pid(pid);
        print("process completed, status=" + status);
        jobby();
    });
}
else
{
    print("failed process creation");
}

// Main loop
Gtk.main();


--
[phoenix@fnx ~]# rm -f .signature
[phoenix@fnx ~]# ls -l .signature
ls: .signature: No such file or directory
[phoenix@fnx ~]# exit



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