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:
David Zeuthen
2010-08-16 13:43:35 -04:00
parent a6264a3a19
commit 8a3a4596e2
5 changed files with 607 additions and 165 deletions

View File

@@ -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();
}