Merge branch 'wip/tingping/socket-client-slow-fix' into 'master'

gsocketclient: Fix critical on cancellation

Closes #1693 and #1653

See merge request GNOME/glib!688
This commit is contained in:
Philip Withnall 2019-02-27 14:35:53 +00:00
commit 0d63ee0908
3 changed files with 86 additions and 11 deletions

View File

@ -1338,6 +1338,8 @@ typedef struct
GSList *connection_attempts; GSList *connection_attempts;
GError *last_error; GError *last_error;
gboolean completed;
} GSocketClientAsyncConnectData; } GSocketClientAsyncConnectData;
static void connection_attempt_unref (gpointer attempt); static void connection_attempt_unref (gpointer attempt);
@ -1424,9 +1426,15 @@ g_socket_client_async_connect_complete (GSocketClientAsyncConnectData *data)
data->connection = (GIOStream *)wrapper_connection; data->connection = (GIOStream *)wrapper_connection;
} }
if (!g_task_return_error_if_cancelled (data->task))
{
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, data->connection); g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, data->connection);
g_task_return_pointer (data->task, data->connection, g_object_unref); g_task_return_pointer (data->task, g_steal_pointer (&data->connection), g_object_unref);
data->connection = NULL; }
else
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
data->completed = TRUE;
g_object_unref (data->task); g_object_unref (data->task);
} }
@ -1545,12 +1553,21 @@ g_socket_client_proxy_connect_callback (GObject *object,
} }
static gboolean static gboolean
task_completed_or_cancelled (GTask *task) task_completed_or_cancelled (GSocketClientAsyncConnectData *data)
{ {
if (g_task_get_completed (task)) GTask *task = data->task;
GCancellable *cancellable = g_task_get_cancellable (task);
GError *error = NULL;
if (data->completed)
return TRUE; return TRUE;
else if (g_task_return_error_if_cancelled (task)) else if (g_cancellable_set_error_if_cancelled (cancellable, &error))
{
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
g_task_return_error (task, g_steal_pointer (&error));
data->completed = TRUE;
return TRUE; return TRUE;
}
else else
return FALSE; return FALSE;
} }
@ -1567,7 +1584,7 @@ g_socket_client_connected_callback (GObject *source,
GProxy *proxy; GProxy *proxy;
const gchar *protocol; const gchar *protocol;
if (task_completed_or_cancelled (data->task) || g_cancellable_is_cancelled (attempt->cancellable)) if (task_completed_or_cancelled (data) || g_cancellable_is_cancelled (attempt->cancellable))
{ {
g_object_unref (data->task); g_object_unref (data->task);
connection_attempt_unref (attempt); connection_attempt_unref (attempt);
@ -1701,7 +1718,7 @@ g_socket_client_enumerator_callback (GObject *object,
ConnectionAttempt *attempt; ConnectionAttempt *attempt;
GError *error = NULL; GError *error = NULL;
if (task_completed_or_cancelled (data->task)) if (task_completed_or_cancelled (data))
{ {
g_object_unref (data->task); g_object_unref (data->task);
return; return;
@ -1718,6 +1735,7 @@ g_socket_client_enumerator_callback (GObject *object,
} }
g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL); g_socket_client_emit_event (data->client, G_SOCKET_CLIENT_COMPLETE, data->connectable, NULL);
data->completed = TRUE;
if (!error) if (!error)
{ {
if (data->last_error) if (data->last_error)
@ -1835,6 +1853,7 @@ g_socket_client_connect_async (GSocketClient *client,
*/ */
data->task = g_task_new (client, cancellable, callback, user_data); data->task = g_task_new (client, cancellable, callback, user_data);
g_task_set_check_cancellable (data->task, FALSE); /* We handle this manually */
g_task_set_source_tag (data->task, g_socket_client_connect_async); g_task_set_source_tag (data->task, g_socket_client_connect_async);
g_task_set_task_data (data->task, data, (GDestroyNotify)g_socket_client_async_connect_data_free); g_task_set_task_data (data->task, data, (GDestroyNotify)g_socket_client_async_connect_data_free);

View File

@ -87,7 +87,21 @@ on_timer (GCancellable *cancel)
} }
static void static void
test_happy_eyeballs_cancel (void) on_event (GSocketClient *client,
GSocketClientEvent event,
GSocketConnectable *connectable,
GIOStream *connection,
gboolean *got_completed_event)
{
if (event == G_SOCKET_CLIENT_COMPLETE)
{
*got_completed_event = TRUE;
g_assert_null (connection);
}
}
static void
test_happy_eyeballs_cancel_delayed (void)
{ {
GSocketClient *client; GSocketClient *client;
GSocketService *service; GSocketService *service;
@ -95,6 +109,10 @@ test_happy_eyeballs_cancel (void)
guint16 port; guint16 port;
GMainLoop *loop; GMainLoop *loop;
GCancellable *cancel; GCancellable *cancel;
gboolean got_completed_event = FALSE;
/* This just tests that cancellation works as expected, still emits the completed signal,
* and never returns a connection */
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
@ -107,8 +125,45 @@ test_happy_eyeballs_cancel (void)
cancel = g_cancellable_new (); cancel = g_cancellable_new ();
g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop); g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop);
g_timeout_add (1, (GSourceFunc) on_timer, cancel); g_timeout_add (1, (GSourceFunc) on_timer, cancel);
g_signal_connect (client, "event", G_CALLBACK (on_event), &got_completed_event);
g_main_loop_run (loop); g_main_loop_run (loop);
g_assert_true (got_completed_event);
g_main_loop_unref (loop);
g_object_unref (service);
g_object_unref (client);
g_object_unref (cancel);
}
static void
test_happy_eyeballs_cancel_instant (void)
{
GSocketClient *client;
GSocketService *service;
GError *error = NULL;
guint16 port;
GMainLoop *loop;
GCancellable *cancel;
gboolean got_completed_event = FALSE;
/* This tests the same things as above, test_happy_eyeballs_cancel_delayed(), but
* with different timing since it sends an already cancelled cancellable */
loop = g_main_loop_new (NULL, FALSE);
service = g_socket_service_new ();
port = g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL, &error);
g_assert_no_error (error);
g_socket_service_start (service);
client = g_socket_client_new ();
cancel = g_cancellable_new ();
g_cancellable_cancel (cancel);
g_socket_client_connect_to_host_async (client, "localhost", port, cancel, on_connected_cancelled, loop);
g_signal_connect (client, "event", G_CALLBACK (on_event), &got_completed_event);
g_main_loop_run (loop);
g_assert_true (got_completed_event);
g_main_loop_unref (loop); g_main_loop_unref (loop);
g_object_unref (service); g_object_unref (service);
g_object_unref (client); g_object_unref (client);
@ -121,7 +176,9 @@ main (int argc, char *argv[])
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_add_func ("/socket-client/happy-eyeballs/slow", test_happy_eyeballs); g_test_add_func ("/socket-client/happy-eyeballs/slow", test_happy_eyeballs);
g_test_add_func ("/socket-client/happy-eyeballs/cancellation", test_happy_eyeballs_cancel); g_test_add_func ("/socket-client/happy-eyeballs/cancellation/instant", test_happy_eyeballs_cancel_instant);
g_test_add_func ("/socket-client/happy-eyeballs/cancellation/delayed", test_happy_eyeballs_cancel_delayed);
return g_test_run (); return g_test_run ();
} }

View File

@ -150,7 +150,6 @@ if host_machine.system() != 'windows'
'installed_tests_env' : { 'installed_tests_env' : {
'LD_PRELOAD': '@0@/slow-connect-preload.so'.format(installed_tests_execdir), 'LD_PRELOAD': '@0@/slow-connect-preload.so'.format(installed_tests_execdir),
}, },
'suite': ['flaky'],
}, },
'gschema-compile' : {'install' : false}, 'gschema-compile' : {'install' : false},
'trash' : {}, 'trash' : {},