PSA: fork() after gtk_init() is unsafe.

Alistair Buxton a.j.buxton at gmail.com
Mon May 5 00:55:21 CEST 2014


Hi all,

It has recently come to my attention that a lot of the programs in
Xfce that have a daemon mode call gtk_init() before fork()ing. This is
unsafe, and when it goes wrong this will typically manifest as the
daemon hanging shortly after forking, and appearing to simply do
nothing at all.

Some examples of this bug:

https://bugs.launchpad.net/ubuntu/+source/xfce4-volumed/+bug/1314782

https://bugs.launchpad.net/ubuntu/+source/xfce4-settings/+bug/1239014

https://bugs.launchpad.net/ubuntu/+source/xfce4-power-manager/+bug/973778

https://bugs.launchpad.net/ubuntu/+source/xfce4-appfinder/+bug/1048805

I also suspect this might be the cause of all the weird thunar
crashes, but I have no evidence for this currently.

So you're probably going to say "but Al, those are all Xubuntu
specific bugs caused by Ubuntu modifications" - while this is true, we
believe the underlying cause of all of them is the fork() after
gtk_init(). The modded Gtk stuff in Ubuntu merely exposes the problem.

fork() after gtk_init() has always been problematic, and the only
reason it works is by luck - or at least, that is what our research
into the problem has told us. For example:
http://lists.freedesktop.org/archives/dbus/2007-April/007496.html

So if you're maintaining an application in Xfce that forks in order to
become a daemon, please do it as early on as possible. It isn't
necessary to call gtk_init() to parse arguments with glib, so ideally
you would parse for the "no-daemon" argument, possibly fork, then call
gtk_init, then do everything else. This is the order of operations in
the fix I proposed for xfce4-volumed-pulse. Note that the original
xfce4-volumed isn't affected by this bug, but potentially there could
be others I haven't mentioned.

Thanks,
-- 
Alistair Buxton
a.j.buxton at gmail.com


More information about the Xfce4-dev mailing list