Commit a05e9dcf authored by Jakub Adam's avatar Jakub Adam

tcp-bsd: Restore global locks

Avoids race between socket_send_more() and socket_close() leading either
to crash on use of freed NiceSocket, or abort upon attempting to
g_mutex_clear() a locked mutex.

Akin to what d1611c0c did to udp-turn.
parent 3c29a99c
Pipeline #9244 passed with stage
in 47 seconds
...@@ -59,8 +59,9 @@ ...@@ -59,8 +59,9 @@
* (See: https://phabricator.freedesktop.org/D230). */ * (See: https://phabricator.freedesktop.org/D230). */
#define TCP_NODELAY 1 #define TCP_NODELAY 1
static GMutex mutex;
typedef struct { typedef struct {
GMutex mutex;
NiceAddress remote_addr; NiceAddress remote_addr;
GQueue send_queue; GQueue send_queue;
GMainContext *context; GMainContext *context;
...@@ -102,7 +103,6 @@ nice_tcp_bsd_socket_new_from_gsock (GMainContext *ctx, GSocket *gsock, ...@@ -102,7 +103,6 @@ nice_tcp_bsd_socket_new_from_gsock (GMainContext *ctx, GSocket *gsock,
if (ctx == NULL) if (ctx == NULL)
ctx = g_main_context_default (); ctx = g_main_context_default ();
g_mutex_init (&priv->mutex);
priv->context = g_main_context_ref (ctx); priv->context = g_main_context_ref (ctx);
priv->remote_addr = *remote_addr; priv->remote_addr = *remote_addr;
priv->error = FALSE; priv->error = FALSE;
...@@ -214,6 +214,8 @@ socket_close (NiceSocket *sock) ...@@ -214,6 +214,8 @@ socket_close (NiceSocket *sock)
{ {
TcpPriv *priv = sock->priv; TcpPriv *priv = sock->priv;
g_mutex_lock (&mutex);
if (sock->fileno) { if (sock->fileno) {
g_socket_close (sock->fileno, NULL); g_socket_close (sock->fileno, NULL);
g_object_unref (sock->fileno); g_object_unref (sock->fileno);
...@@ -229,7 +231,7 @@ socket_close (NiceSocket *sock) ...@@ -229,7 +231,7 @@ socket_close (NiceSocket *sock)
if (priv->context) if (priv->context)
g_main_context_unref (priv->context); g_main_context_unref (priv->context);
g_mutex_clear (&priv->mutex); g_mutex_unlock (&mutex);
g_slice_free(TcpPriv, sock->priv); g_slice_free(TcpPriv, sock->priv);
} }
...@@ -428,12 +430,12 @@ socket_send_more ( ...@@ -428,12 +430,12 @@ socket_send_more (
NiceSocket *sock = (NiceSocket *) data; NiceSocket *sock = (NiceSocket *) data;
TcpPriv *priv = sock->priv; TcpPriv *priv = sock->priv;
g_mutex_lock (&priv->mutex); g_mutex_lock (&mutex);
if (g_source_is_destroyed (g_main_current_source ())) { if (g_source_is_destroyed (g_main_current_source ())) {
nice_debug ("Source was destroyed. " nice_debug ("Source was destroyed. "
"Avoided race condition in tcp-bsd.c:socket_send_more"); "Avoided race condition in tcp-bsd.c:socket_send_more");
g_mutex_unlock (&priv->mutex); g_mutex_unlock (&mutex);
return FALSE; return FALSE;
} }
...@@ -445,7 +447,7 @@ socket_send_more ( ...@@ -445,7 +447,7 @@ socket_send_more (
g_source_unref (priv->io_source); g_source_unref (priv->io_source);
priv->io_source = NULL; priv->io_source = NULL;
g_mutex_unlock (&priv->mutex); g_mutex_unlock (&mutex);
if (priv->writable_cb) if (priv->writable_cb)
priv->writable_cb (sock, priv->writable_data); priv->writable_cb (sock, priv->writable_data);
...@@ -453,6 +455,6 @@ socket_send_more ( ...@@ -453,6 +455,6 @@ socket_send_more (
return FALSE; return FALSE;
} }
g_mutex_unlock (&priv->mutex); g_mutex_unlock (&mutex);
return TRUE; return TRUE;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment