Skip to content
  • Philip Withnall's avatar
    gmain: Allow GSource methods to be called from a finalize() callback · 281e3010
    Philip Withnall authored
    
    
    Temporarily increase the ref count of a GSource to 1 while calling its
    finalize() callback, so that the finalize() implementation can call
    GSource methods (like g_source_set_ready_time()) without causing
    critical warnings. It’s safe to call those methods at this point, as the
    source has been destroyed, but nothing has been freed.
    
    This is an indirect way of fixing a race between GCancellable and
    GCancellableSource, whereby the GCancellable::cancelled callback for the
    GCancellableSource is not disconnected until the GCancellableSource’s
    finalize() function is called. Previously, this meant there was a window
    in which the GCancellableSource’s ref count was 0, but the ::cancelled
    callback was still connected, and could legitimately be called as a
    result of another thread calling g_cancellable_cancel() on the
    GCancellable. The callback calls g_source_set_ready_time() on the
    GSource, and there’s no thread-safe way of checking whether the GSource
    has been destroyed. Instead, we have to change GSource so its ref count
    is only decremented to 0 inside the locked section in
    g_source_unref_internal() *after* the finalize() function has been
    called, and hence after the GCancellable::cancelled callback has been
    disconnected. The use of g_cancellable_disconnect() ensures that the
    callback disconnection is thread safe.
    
    Signed-off-by: default avatarPhilip Withnall <withnall@endlessm.com>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781601
    281e3010