red-channel: make red_client_{ref,unref} thread safe

These function are called on both sides of dispatcher so the
increment/decrement of the counter is done in multiple threads.
This caused the counter to not get incremented correctly and
freed the structure too early, leaving a dangling pointer in
the other thread.

This fixes https://bugzilla.redhat.com/show_bug.cgi?id=1253375.

Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
Acked-by: Christophe Fergeau <cfergeau@redhat.com>
Acked-by: Jonathon Jongsma <jjongsma@redhat.com>
This commit is contained in:
Frediano Ziglio 2016-04-12 16:28:07 +01:00
parent 7d616b3de3
commit b3aebf9136

View File

@ -2064,13 +2064,13 @@ RedClient *red_client_new(RedsState *reds, int migrated)
RedClient *red_client_ref(RedClient *client)
{
spice_assert(client);
client->refs++;
g_atomic_int_inc(&client->refs);
return client;
}
RedClient *red_client_unref(RedClient *client)
{
if (!--client->refs) {
if (g_atomic_int_dec_and_test(&client->refs)) {
spice_debug("release client=%p", client);
pthread_mutex_destroy(&client->lock);
free(client);