[tracker/wip/carlosg/bus-new-race] libtracker-sparql: Fix race condition in tracker_sparql_connection_bus_new()




commit 3f12e6cdd1a1db46ed1bbfa7ae83cd7e5d28bb2a
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Mar 14 12:20:24 2021 +0100

    libtracker-sparql: Fix race condition in tracker_sparql_connection_bus_new()
    
    When commit cad0aefa0b added tracker_sparql_connection_bus_new_async(), it
    made the sync variant run over the async variant for simplicity, using a
    nested main loop that takes control until the asynchronous method returns.
    
    However, we were using either of the thread default context, or the main
    default one, in case of this call happening on a separate thread, this main
    context may be iterating somewhere else, so the asynchronous method might
    return before we've blocked on the main loop.
    
    We want to lock the bus connection creation on this thread, so create a
    new main context that is temporarily made the thread default, this will
    ensure the asynchronous call happens in our own thread, under our own
    main loop.

 src/libtracker-sparql/tracker-backend.vala | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
---
diff --git a/src/libtracker-sparql/tracker-backend.vala b/src/libtracker-sparql/tracker-backend.vala
index 01b95d0b5..ae6313118 100644
--- a/src/libtracker-sparql/tracker-backend.vala
+++ b/src/libtracker-sparql/tracker-backend.vala
@@ -30,10 +30,13 @@ public static Tracker.Sparql.Connection tracker_sparql_connection_remote_new (st
 public static Tracker.Sparql.Connection tracker_sparql_connection_bus_new (string service, string? 
object_path, DBusConnection? conn) throws Tracker.Sparql.Error, IOError, DBusError, GLib.Error {
        Tracker.get_debug_flags ();
 
-       var context = GLib.MainContext.ref_thread_default();
+       var context = new GLib.MainContext ();
        var loop = new GLib.MainLoop(context);
        GLib.Error? error = null;
        Tracker.Sparql.Connection? sparql_conn = null;
+
+       context.push_thread_default ();
+
        tracker_sparql_connection_bus_new_async.begin(service, object_path, conn, null, (o, res) => {
                try {
                        sparql_conn = tracker_sparql_connection_bus_new_async.end(res);
@@ -44,6 +47,8 @@ public static Tracker.Sparql.Connection tracker_sparql_connection_bus_new (strin
        });
        loop.run ();
 
+       context.pop_thread_default ();
+
        if (error != null)
                throw error;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]