What happens when there are two main-loops in one process?

Simon McVittie simon.mcvittie at collabora.co.uk
Mon Apr 15 04:52:55 PDT 2013

On 14/04/13 20:45, Lars Hanisch wrote:
>  I experience a segfault sometimes [...]
> dbus/libdbus is version "1.4.18-1ubuntu1.3"

Thread-safety was broken in libdbus 1.4.x (fixed in 1.6, we think, but
there might be more bugs). If that's the version you have, it is only
safe to touch libdbus from one thread (in your case I think that means
the Avahi thread).

Even in 1.6, if dbus2vdr and avahi4vdr are fighting over the same
DBusConnection (because dbus_bus_get() returns a shared instance)
they'll both be trying to dispatch messages from it. This is unlikely to
work particularly well.

If you have to use threads, I would recommend GDBus, part of the GIO
library, which comes with GLib (the libglib2.0-0 package in Ubuntu). It
does its D-Bus interactions in yet another thread, and passes each
message to the correct thread for it to be handled safely (each thread
that receives messages is expected to be running its own GLib "main
context"). It's a more elaborate design, but much better for
applications that are already multi-threaded.

>  Is this actually "allowed", having too main loops in one process?

"Sometimes". It depends what libraries you use and how thread-safe they are.

libdbus is meant to work if you this, but it was never very well-tested,
and it's known to be broken in 1.4.x (bug 43744). Even with that bug
fixed, its design - where a single connection is shared between, and
"dispatched" by, several threads - is just not very helpful in
situations like this where your application doesn't use D-Bus or a
particular main loop itself, but its plugins want to. GDBus does this
much better.

>  What happens, if some other plugin author uses some lib with another dbus main loop?

Pain, suffering, occasional crashes. GDBus avoids this problem.

>  I haven't worked with glibc (and its main loop) for now,
> I thought I could come around that... :)
>  Any advice for me?

glibc (libc.so, the GNU C library) doesn't have a main loop
implementation. You might be thinking of GLib (libglib-2.0.so, a
general-purpose utility library originating from GNOME) which has APIs
called GMainContext and GMainLoop.


More information about the dbus mailing list