From 54d9c26b34d70be9a1e5519bf03d727334c52e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 23 Jan 2024 19:04:19 +0100 Subject: [PATCH] gcancellable: Reference the object before locking when doing signal emission On g_cancellable_cancel() we were increasing the GCancellable ref count before emitting the ::cancelled signal, this is a safe thing to do but it was happening while the cancellable was locked, and this may have potentially waken up some toggle notifications. To prevent this, reference the GCancellable just before locking. --- gio/gcancellable.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gio/gcancellable.c b/gio/gcancellable.c index 227d56d2e..abf5878ed 100644 --- a/gio/gcancellable.c +++ b/gio/gcancellable.c @@ -495,11 +495,16 @@ g_cancellable_cancel (GCancellable *cancellable) priv = cancellable->priv; + /* We add a reference before locking, to avoid that potential toggle + * notifications on the object might happen while we're locked. + */ + g_object_ref (cancellable); g_mutex_lock (&priv->mutex); if (!g_atomic_int_compare_and_exchange (&priv->cancelled, FALSE, TRUE)) { g_mutex_unlock (&priv->mutex); + g_object_unref (cancellable); return; } @@ -508,7 +513,6 @@ g_cancellable_cancel (GCancellable *cancellable) if (priv->wakeup) GLIB_PRIVATE_CALL (g_wakeup_signal) (priv->wakeup); - g_object_ref (cancellable); g_signal_emit (cancellable, signals[CANCELLED], 0); if (g_atomic_int_dec_and_test (&priv->cancelled_running))