After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 656387 - GCancellable can be used concurrently
GCancellable can be used concurrently
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: gio
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2011-08-12 09:50 UTC by Stef Walter
Modified: 2011-08-19 09:27 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
gio: GCancellable can be used concurrently (1.40 KB, patch)
2011-08-12 09:53 UTC, Stef Walter
none Details | Review
gio: GCancellable can be used concurrently (9.54 KB, patch)
2011-08-12 14:04 UTC, Stef Walter
none Details | Review
gio: GCancellable can be used concurrently (9.52 KB, patch)
2011-08-12 15:20 UTC, Stef Walter
accepted-commit_now Details | Review

Description Stef Walter 2011-08-12 09:50:13 UTC
I've had a really hard time tracking down why GCancellable can't be used concurrently (including asking Alex).

I can't help but come to the conclusion that it can be used by multiple concurrent operations, contrary to the documentation.

Will update the docs.
Comment 1 Stef Walter 2011-08-12 09:52:45 UTC
Previous discussion: http://www.mail-archive.com/gtk-devel-list@gnome.org/msg10628.html
Comment 2 Stef Walter 2011-08-12 09:53:59 UTC
Created attachment 193680 [details] [review]
gio: GCancellable can be used concurrently

* Update documentation to note that GCancellable can be used
   concurrently by multiple operations.
 * Add documentation to g_cancellable_reset that it will deadlock
   if called from within cancelled handler.
Comment 3 Colin Walters 2011-08-12 10:46:45 UTC
We should actually have a test for it if we're going to claim it works.
Comment 4 Stef Walter 2011-08-12 13:17:54 UTC
Sure. I'll include a test.

But since this is threading, obviously most any test will prove that it *can* work (in the given circumstances), not that it *will* work (under any circumstances).

Alex, if you have a moment to look over this as well, that would be perfect.
Comment 5 Stef Walter 2011-08-12 14:04:22 UTC
Created attachment 193697 [details] [review]
gio: GCancellable can be used concurrently

* Update documentation to note that GCancellable can be used
   concurrently by multiple operations.
 * Add documentation to g_cancellable_reset that it will deadlock
   if called from within cancelled handler.
 * Add test for multiple concurrent operations using the same
   cancellable.
Comment 6 Stef Walter 2011-08-12 14:06:33 UTC
The new test runs 45 different operations (randomly in threads or in mainloop) and then cancels them all, verifies that they all stopped short of their goal, and finished.
Comment 7 Dan Winship 2011-08-12 14:43:11 UTC
Comment on attachment 193697 [details] [review]
gio: GCancellable can be used concurrently

>+ * If this function is called from within a #GCancellable::cancelled
>+ * then this will result in a deadlock.

Missing the word "handler" or "callback".

But if you call this while @cancellable is in use at all then there is a race condition, because if there are two ops using the cancellable, and handler A resets it, then handler B may or may not run, depending on thread scheduling.

So, I think this should just say "if @cancellable is currently in use by a cancellable operation, then the result is undefined", which allows for both race conditions and deadlocks.
Comment 8 Stef Walter 2011-08-12 14:52:05 UTC
Yes, for sure calling g_cancellable_reset() while a cancellable is in use (by even one operation) is completely undefined and broken.

In fact g_cancellable_reset() has such ugly semantics and effects, I really would prefer if it didn't exist at all.

I can make that change, although the word 'deadlock' does really get people's attention.
Comment 9 Stef Walter 2011-08-12 15:20:36 UTC
Created attachment 193706 [details] [review]
gio: GCancellable can be used concurrently

* Update documentation to note that GCancellable can be used
   concurrently by multiple operations.
 * Add documentation to g_cancellable_reset that it will deadlock
   if called from within cancelled handler.
 * Add test for multiple concurrent operations using the same
   cancellable.
Comment 10 Matthias Clasen 2011-08-13 20:39:12 UTC
Review of attachment 193706 [details] [review]:

> * Add documentation to g_cancellable_reset that it will deadlock
> if called from within cancelled handler.

Doesn't seem to be literally true. The added paragraph just speaks about undefined behaviour. 

Other than that, looks good to me.
Comment 11 Alexander Larsson 2011-08-19 08:51:55 UTC
Looks good to me too. please commit
Comment 12 Stef Walter 2011-08-19 09:27:58 UTC
Thanks. Merged.

Changed the commit message as per Matthias' comment.