mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-02 07:23:41 +02:00
Bug 626748 – Use async methods for writing and handle EAGAIN
If sending a lot of data and/or the other peer is not reading it, then socket buffers can overflow. This is communicated from the kernel by returning EAGAIN. In GIO, it is modelled by g_output_stream_write() and g_socket_send_message() returning G_IO_ERROR_WOULD_BLOCK. It is also problematic that that we're using synchronous IO in the shared GDBus IO thread. It means that one GDBusConnection can lock up others. It turns out that by porting from g_output_stream_write() to g_output_stream_write_async() we fix the EAGAIN issue. For GSocket, we still need to handle things manually (by creating a GSource) as g_socket_send_message() is used. We check the new behavior in Michael's producer/consumer test case (at /gdbus/overflow in gdbus-peer.c) added in the last commit. Also add a test case that sends and receives a 20 MiB message. Also add a new `transport' G_DBUS_DEBUG option so it is easy to inspect partial writes: $ G_DBUS_DEBUG=transport ./gdbus-connection -p /gdbus/connection/large_message [...] ======================================================================== GDBus-debug:Transport: >>>> WROTE 128000 bytes of message with serial 4 and size 20971669 from offset 0 on a GSocketOutputStream ======================================================================== GDBus-debug:Transport: >>>> WROTE 128000 bytes of message with serial 4 and size 20971669 from offset 128000 on a GSocketOutputStream ======================================================================== GDBus-debug:Transport: >>>> WROTE 128000 bytes of message with serial 4 and size 20971669 from offset 256000 on a GSocketOutputStream [...] ======================================================================== GDBus-debug:Transport: >>>> WROTE 43669 bytes of message with serial 4 and size 20971669 from offset 20928000 on a GSocketOutputStream [...] ======================================================================== GDBus-debug:Transport: <<<< READ 16 bytes of message with serial 3 and size 20971620 to offset 0 from a GSocketInputStream ======================================================================== GDBus-debug:Transport: <<<< READ 15984 bytes of message with serial 3 and size 20971620 to offset 16 from a GSocketInputStream ======================================================================== GDBus-debug:Transport: <<<< READ 16000 bytes of message with serial 3 and size 20971620 to offset 16000 from a GSocketInputStream [...] ======================================================================== GDBus-debug:Transport: <<<< READ 144000 bytes of message with serial 3 and size 20971620 to offset 20720000 from a GSocketInputStream ======================================================================== GDBus-debug:Transport: <<<< READ 107620 bytes of message with serial 3 and size 20971620 to offset 20864000 from a GSocketInputStream OK https://bugzilla.gnome.org/show_bug.cgi?id=626748 Signed-off-by: David Zeuthen <davidz@redhat.com>
This commit is contained in:
@@ -917,6 +917,85 @@ test_connection_basic (void)
|
||||
|
||||
/* ---------------------------------------------------------------------------------------------------- */
|
||||
|
||||
/* Message size > 20MiB ... should be enough to make sure the message
|
||||
* is fragmented when shoved across any transport
|
||||
*/
|
||||
#define LARGE_MESSAGE_STRING_LENGTH (20*1024*1024)
|
||||
|
||||
static void
|
||||
large_message_on_name_appeared (GDBusConnection *connection,
|
||||
const gchar *name,
|
||||
const gchar *name_owner,
|
||||
gpointer user_data)
|
||||
{
|
||||
GError *error;
|
||||
gchar *request;
|
||||
const gchar *reply;
|
||||
GVariant *result;
|
||||
guint n;
|
||||
|
||||
request = g_new (gchar, LARGE_MESSAGE_STRING_LENGTH + 1);
|
||||
for (n = 0; n < LARGE_MESSAGE_STRING_LENGTH; n++)
|
||||
request[n] = '0' + (n%10);
|
||||
request[n] = '\0';
|
||||
|
||||
error = NULL;
|
||||
result = g_dbus_connection_call_sync (connection,
|
||||
"com.example.TestService", /* bus name */
|
||||
"/com/example/TestObject", /* object path */
|
||||
"com.example.Frob", /* interface name */
|
||||
"HelloWorld", /* method name */
|
||||
g_variant_new ("(s)", request), /* parameters */
|
||||
G_VARIANT_TYPE ("(s)"), /* return type */
|
||||
G_DBUS_CALL_FLAGS_NONE,
|
||||
-1,
|
||||
NULL,
|
||||
&error);
|
||||
g_assert_no_error (error);
|
||||
g_assert (result != NULL);
|
||||
g_variant_get (result, "(&s)", &reply);
|
||||
g_assert_cmpint (strlen (reply), >, LARGE_MESSAGE_STRING_LENGTH);
|
||||
g_assert (g_str_has_prefix (reply, "You greeted me with '01234567890123456789012"));
|
||||
g_assert (g_str_has_suffix (reply, "6789'. Thanks!"));
|
||||
g_variant_unref (result);
|
||||
|
||||
g_free (request);
|
||||
|
||||
g_main_loop_quit (loop);
|
||||
}
|
||||
|
||||
static void
|
||||
large_message_on_name_vanished (GDBusConnection *connection,
|
||||
const gchar *name,
|
||||
gpointer user_data)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
test_connection_large_message (void)
|
||||
{
|
||||
guint watcher_id;
|
||||
|
||||
session_bus_up ();
|
||||
|
||||
/* this is safe; testserver will exit once the bus goes away */
|
||||
g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
|
||||
|
||||
watcher_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
|
||||
"com.example.TestService",
|
||||
G_BUS_NAME_WATCHER_FLAGS_NONE,
|
||||
large_message_on_name_appeared,
|
||||
large_message_on_name_vanished,
|
||||
NULL, /* user_data */
|
||||
NULL); /* GDestroyNotify */
|
||||
g_main_loop_run (loop);
|
||||
g_bus_unwatch_name (watcher_id);
|
||||
|
||||
session_bus_down ();
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------------------------------------- */
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
@@ -939,5 +1018,6 @@ main (int argc,
|
||||
g_test_add_func ("/gdbus/connection/signals", test_connection_signals);
|
||||
g_test_add_func ("/gdbus/connection/filter", test_connection_filter);
|
||||
g_test_add_func ("/gdbus/connection/flush", test_connection_flush);
|
||||
g_test_add_func ("/gdbus/connection/large_message", test_connection_large_message);
|
||||
return g_test_run();
|
||||
}
|
||||
|
@@ -1250,30 +1250,42 @@ test_credentials (void)
|
||||
|
||||
/* ---------------------------------------------------------------------------------------------------- */
|
||||
|
||||
#if 0 /* def G_OS_UNIX disabled while it fails */
|
||||
#ifdef G_OS_UNIX
|
||||
|
||||
/* Chosen to be big enough to overflow the socket buffer */
|
||||
#define OVERFLOW_NUM_SIGNALS 5000
|
||||
#define OVERFLOW_TIMEOUT_SEC 10
|
||||
|
||||
static gboolean
|
||||
signal_count_cb (GDBusConnection *connection,
|
||||
GDBusMessage *message,
|
||||
gboolean incoming,
|
||||
gpointer user_data)
|
||||
overflow_filter_func (GDBusConnection *connection,
|
||||
GDBusMessage *message,
|
||||
gboolean incoming,
|
||||
gpointer user_data)
|
||||
{
|
||||
volatile int *p = user_data;
|
||||
(*p)++;
|
||||
return TRUE;
|
||||
volatile gint *counter = user_data;
|
||||
*counter += 1;
|
||||
return FALSE; /* don't drop the message */
|
||||
}
|
||||
|
||||
static gboolean
|
||||
overflow_on_500ms_later_func (gpointer user_data)
|
||||
{
|
||||
g_main_loop_quit (loop);
|
||||
return FALSE; /* don't keep the idle */
|
||||
}
|
||||
|
||||
static void
|
||||
test_overflow (void)
|
||||
{
|
||||
gint sv[2], i;
|
||||
gint sv[2];
|
||||
gint n;
|
||||
GSocket *socket;
|
||||
GSocketConnection *socket_connection;
|
||||
GDBusConnection *producer, *consumer;
|
||||
GError *error;
|
||||
gchar *guid;
|
||||
pid_t child;
|
||||
GTimer *timer;
|
||||
volatile int counter = 0;
|
||||
volatile gint n_messages_received;
|
||||
volatile gint n_messages_sent;
|
||||
|
||||
g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
|
||||
|
||||
@@ -1287,14 +1299,16 @@ test_overflow (void)
|
||||
NULL, /* guid */
|
||||
G_DBUS_CONNECTION_FLAGS_NONE,
|
||||
NULL, /* GDBusAuthObserver */
|
||||
NULL,
|
||||
NULL, /* GCancellable */
|
||||
&error);
|
||||
g_dbus_connection_set_exit_on_close (producer, TRUE);
|
||||
g_assert_no_error (error);
|
||||
g_object_unref (socket_connection);
|
||||
n_messages_sent = 0;
|
||||
g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
|
||||
|
||||
/* send enough data that we get an EAGAIN */
|
||||
for (i = 0; i < 1000; i++)
|
||||
for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
|
||||
{
|
||||
error = NULL;
|
||||
g_dbus_connection_emit_signal (producer,
|
||||
@@ -1304,41 +1318,46 @@ test_overflow (void)
|
||||
"Member",
|
||||
g_variant_new ("(s)", "a string"),
|
||||
&error);
|
||||
/* run the main event loop - otherwise GDBusConnection::closed won't be fired */
|
||||
g_main_context_iteration (NULL, FALSE);
|
||||
g_assert_no_error (error);
|
||||
static gint count = 0;
|
||||
g_print ("%d ", count++);
|
||||
}
|
||||
|
||||
/* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
|
||||
* kernel buffers) and verify that n_messages_sent <
|
||||
* OVERFLOW_NUM_SIGNALS
|
||||
*
|
||||
* This is to verify that not all the submitted messages have been
|
||||
* sent to the underlying transport.
|
||||
*/
|
||||
g_timeout_add (500, overflow_on_500ms_later_func, NULL);
|
||||
g_main_loop_run (loop);
|
||||
g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
|
||||
|
||||
/* now suck it all out as a client, and add it up */
|
||||
socket = g_socket_new_from_fd (sv[1], &error);
|
||||
g_assert_no_error (error);
|
||||
socket_connection = g_socket_connection_factory_create_connection (socket);
|
||||
g_assert (socket_connection != NULL);
|
||||
g_object_unref (socket);
|
||||
guid = g_dbus_generate_guid ();
|
||||
consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
|
||||
guid,
|
||||
NULL, /* guid */
|
||||
G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
|
||||
NULL, /* GDBusAuthObserver */
|
||||
NULL,
|
||||
NULL, /* GCancellable */
|
||||
&error);
|
||||
g_dbus_connection_add_filter (consumer, signal_count_cb, &counter, NULL);
|
||||
g_dbus_connection_start_message_processing (consumer);
|
||||
|
||||
g_free (guid);
|
||||
g_assert_no_error (error);
|
||||
g_object_unref (socket_connection);
|
||||
n_messages_received = 0;
|
||||
g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
|
||||
g_dbus_connection_start_message_processing (consumer);
|
||||
|
||||
timer = g_timer_new ();
|
||||
g_timer_start (timer);
|
||||
|
||||
while (counter < 1000 &&
|
||||
g_timer_elapsed (timer, NULL) < 5.0)
|
||||
while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
|
||||
g_main_context_iteration (NULL, FALSE);
|
||||
|
||||
g_assert (counter == 1000);
|
||||
g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
|
||||
g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
|
||||
|
||||
g_timer_destroy (timer);
|
||||
g_object_unref (consumer);
|
||||
@@ -1348,6 +1367,7 @@ test_overflow (void)
|
||||
static void
|
||||
test_overflow (void)
|
||||
{
|
||||
/* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
|
||||
}
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user