diff --git a/gio/gdbusprivate.c b/gio/gdbusprivate.c
index 5f8f1f4..01973ae 100644
--- a/gio/gdbusprivate.c
+++ b/gio/gdbusprivate.c
@@ -111,12 +111,14 @@ typedef struct
 
   GSimpleAsyncResult *simple;
 
-  gboolean from_mainloop;
+  GSource *source;
 } ReadWithControlData;
 
 static void
 read_with_control_data_free (ReadWithControlData *data)
 {
+  if (data->source)
+    g_source_destroy (data->source);
   g_object_unref (data->socket);
   if (data->cancellable != NULL)
     g_object_unref (data->cancellable);
@@ -156,7 +158,7 @@ _g_socket_read_with_control_messages_ready (GSocket      *socket,
       g_simple_async_result_take_error (data->simple, error);
     }
 
-  if (data->from_mainloop)
+  if (data->source != NULL)
     g_simple_async_result_complete (data->simple);
   else
     g_simple_async_result_complete_in_idle (data->simple);
@@ -192,17 +194,15 @@ _g_socket_read_with_control_messages (GSocket                 *socket,
 
   if (!g_socket_condition_check (socket, G_IO_IN))
     {
-      GSource *source;
-      data->from_mainloop = TRUE;
-      source = g_socket_create_source (data->socket,
-                                       G_IO_IN | G_IO_HUP | G_IO_ERR,
-                                       cancellable);
-      g_source_set_callback (source,
+      data->source = g_socket_create_source (data->socket,
+                                             G_IO_IN | G_IO_HUP | G_IO_ERR,
+                                             cancellable);
+      g_source_set_callback (data->source,
                              (GSourceFunc) _g_socket_read_with_control_messages_ready,
                              data,
                              (GDestroyNotify) read_with_control_data_free);
-      g_source_attach (source, g_main_context_get_thread_default ());
-      g_source_unref (source);
+      g_source_attach (data->source, g_main_context_get_thread_default ());
+      g_source_unref (data->source);
     }
   else
     {
@@ -869,12 +869,14 @@ struct _MessageToWriteData
 
   gsize               total_written;
   GSimpleAsyncResult *simple;
-
+  GSource *source;
 };
 
 static void
 message_to_write_data_free (MessageToWriteData *data)
 {
+  if (data->source)
+    g_source_destroy (data->source);
   _g_dbus_worker_unref (data->worker);
   if (data->message)
     g_object_unref (data->message);
@@ -1014,16 +1016,15 @@ write_message_continue_writing (MessageToWriteData *data)
           /* Handle WOULD_BLOCK by waiting until there's room in the buffer */
           if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
             {
-              GSource *source;
-              source = g_socket_create_source (data->worker->socket,
-                                               G_IO_OUT | G_IO_HUP | G_IO_ERR,
-                                               data->worker->cancellable);
-              g_source_set_callback (source,
+              data->source = g_socket_create_source (data->worker->socket,
+                                                     G_IO_OUT | G_IO_HUP | G_IO_ERR,
+                                                     data->worker->cancellable);
+              g_source_set_callback (data->source,
                                      (GSourceFunc) on_socket_ready,
                                      data,
                                      NULL); /* GDestroyNotify */
-              g_source_attach (source, g_main_context_get_thread_default ());
-              g_source_unref (source);
+              g_source_attach (data->source, g_main_context_get_thread_default ());
+              g_source_unref (data->source);
               g_error_free (error);
               goto out;
             }
