gobject: Ensure an object has toggle references before notifying it

When an object with toggle reference is notifying a change we just
assume that this is true because of previous checks.
However, while locking, another thread may have removed the toggle
reference causing the waiting thread to abort (as no handler is set at
that point).

To avoid this, once we've got the toggle references mutex lock, check
again if the object has toggle reference, and if it's not the case
anymore just ignore the request.

Add a test that triggers this, it's not 100% happening because this is
of course timing related, but this is very close to the truth.

Fixes: #2394
This commit is contained in:
Marco Trevisan (Treviño)
2021-04-28 21:54:45 +02:00
parent 3f1a1cdb78
commit 468246bb3b
2 changed files with 97 additions and 0 deletions

View File

@@ -3276,6 +3276,16 @@ toggle_refs_notify (GObject *object,
ToggleRefStack tstack, *tstackptr;
G_LOCK (toggle_refs_mutex);
/* If another thread removed the toggle reference on the object, while
* we were waiting here, there's nothing to notify.
* So let's check again if the object has toggle reference and in case return.
*/
if (!OBJECT_HAS_TOGGLE_REF (object))
{
G_UNLOCK (toggle_refs_mutex);
return;
}
tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
tstack = *tstackptr;
G_UNLOCK (toggle_refs_mutex);