log: Make sure glib threading is initialized

While testing spice-server on EL6, I was getting random crashes in the
glib logging code because g_logv was called recursively even though this
was not visible in a backtrace.

It turns out on older glib versions (EL6 has 2.28), g_logv is not
thread-safe unless g_thread_init() is called first. If g_thread_init()
is not called, the GMutex/GPrivate calls g_logv makes are turned into
no-ops, which is going to cause the recursion issue I was seeing.

This commit adds a call to g_thread_init() for these older glib
versions.

(gdb) bt
    0x7fff9f9fb110 "item.type: 114", unused_data=0x0) at gmessages.c:863
    format=0x7ffff50e72ac "item.type: %d", args1=0x7fff9f9fb640) at gmessages.c:517
    SPICE_LOG_LEVEL_DEBUG, strloc=0x7ffff50e72ba "dcc.c:1652", function=
    0x7ffff50e7320 "release_item_before_push", format=0x7ffff50e72ac "item.type: %d", args=
    0x7fff9f9fb640) at log.c:163
    SPICE_LOG_LEVEL_DEBUG, strloc=0x7ffff50e72ba "dcc.c:1652", function=
    0x7ffff50e7320 "release_item_before_push", format=0x7ffff50e72ac "item.type: %d") at log.c:195
    at dcc.c:1652
    at dcc.c:1719
    at dcc-send.c:2450
    at red-channel.c:578
    at red-channel.c:1587
    at event-loop.c:122
    0x7ffff4fb2ef2 <watch_func>, user_data=0x7fff980244e0) at giounix.c:166
    0x7ffff86ec770) at gmain.c:3092
This commit is contained in:
Christophe Fergeau 2016-04-04 18:02:42 +02:00
parent 8473d0ae71
commit 2a4bf49edd
2 changed files with 9 additions and 1 deletions

View File

@ -138,6 +138,14 @@ SPICE_CONSTRUCTOR_FUNC(spice_log_init)
g_log_set_handler(SPICE_LOG_DOMAIN,
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
spice_logger, NULL);
/* Threading is always enabled from 2.31.0 onwards */
/* Our logging is potentially used from different threads.
* Older glibs require that g_thread_init() is called when
* doing that. */
#if !GLIB_CHECK_VERSION(2, 31, 0)
if (!g_thread_supported())
g_thread_init(NULL);
#endif
}
static void spice_logv(const char *log_domain,

View File

@ -173,7 +173,7 @@ AC_DEFUN([SPICE_CHECK_PIXMAN], [
# use in the GLIB2_CFLAGS and GLIB2_LIBS variables.
#------------------
AC_DEFUN([SPICE_CHECK_GLIB2], [
PKG_CHECK_MODULES(GLIB2, glib-2.0 >= 2.22 gio-2.0 >= 2.22)
PKG_CHECK_MODULES(GLIB2, glib-2.0 >= 2.22 gio-2.0 >= 2.22 gthread-2.0 >= 2.22)
])
# SPICE_CHECK_PYTHON_MODULES()