Gnome shell GLib 异步进程失效

Gnome shell GLib async process become defunct

我正在编写一个 gnome shell 扩展和 运行 一个 bash 脚本

let [, pid, , , ] = imports.gi.GLib.spawn_async(null, args, null, GLib.SpawnFlags.DO_NOT_REAP_CHILD, null);

有效,但是 运行 几次后我就能看到

ps aux | grep pid

进程在那里,并标记为已失效。这是正常的吗?我应该清理这些进程吗?或者这是正常行为?

GLib.spawn_async() 的描述确实说要调用 GLib.spawn_close_pid() 但描述说:

On some platforms, notably Windows, the GLib.Pid type represents a resource which must be closed to prevent resource leaking. GLib.spawn_close_pid is provided for this purpose. It should be used on all platforms, even though it doesn't do anything under UNIX.

我的系统是 ubuntu,我应该调用 spawn_close_pid() 吗?

来自 G_SPAWN_DO_NOT_REAP_CHILD 的文档:

the child will not be automatically reaped; you must use g_child_watch_add() yourself (or call waitpid() or handle SIGCHLD yourself), or the child will become a zombie.

所以发生的事情是正常的:子进程正在生成、做某事、退出并变得不存在——但没有父进程正在收获它。死进程会一直挂起直到被回收,以便父进程可以检查它们的退出状态。

您需要停止使用 GLib.SpawnFlags.DO_NOT_REAP_CHILD 标志(在这种情况下 GLib 将监视子进程退出并自动获取它);或使用 g_child_watch_add() and reap the defunct process in the GChildWatchFunc callback using g_spawn_close_pid().

自己添加子监视处理程序

有个例子in this question or another here