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 752983 - gapplication: Acquire the main context before running
gapplication: Acquire the main context before running
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2015-07-28 22:11 UTC by Jasper St. Pierre (not reading bugmail)
Modified: 2015-12-16 16:44 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
gapplication: Acquire the main context before running (1.80 KB, patch)
2015-07-28 22:11 UTC, Jasper St. Pierre (not reading bugmail)
none Details | Review
gapplication: Acquire the main context before running (2.25 KB, patch)
2015-08-05 21:35 UTC, Jasper St. Pierre (not reading bugmail)
committed Details | Review
GApplication: Avoid getting the default context repeatidly (1.73 KB, patch)
2015-12-16 16:37 UTC, Xavier Claessens
rejected Details | Review

Description Jasper St. Pierre (not reading bugmail) 2015-07-28 22:11:55 UTC
See patch. This was breaking g_main_context_invoke in one of our apps at Endless.
Comment 1 Jasper St. Pierre (not reading bugmail) 2015-07-28 22:11:58 UTC
Created attachment 308342 [details] [review]
gapplication: Acquire the main context before running

Otherwise, we'll acquire it on every loop iteration, which can leave us
vulnerable to racing another thread for the acquisition of the main
context.

This can break methods like g_main_context_invoke, which try to acquire
a context to figure out if it can invoke the method synchronously or
need to defer to an idle. In these cases, it isn't guaranteed that the
invocation function will be invoked in the default main context,
e.g. the one that GApplication is holding.

This also matches what GMainLoop is doing.
Comment 2 Xavier Claessens 2015-07-29 20:18:04 UTC
Shouldn't you acquire it earlier, before the command-line handling callbacks to be safe?
Comment 3 Jasper St. Pierre (not reading bugmail) 2015-07-29 20:21:34 UTC
Sure, we should probably do that too.
Comment 4 Matthias Clasen 2015-07-30 01:24:56 UTC
What would that be 'safer' against ?
Comment 5 Xavier Claessens 2015-07-30 17:21:59 UTC
Because g_application_register() will happen earlier in g_application_run() and that will do all kind of gdbus stuff with threads. Also user could be doing thread stuff from "handle-local-options" callback.

IMO it would make sense for g_application_run() to acquire the NULL context before calling back any user code, I always assumed it did, tbh.
Comment 6 Matthias Clasen 2015-08-02 18:23:54 UTC
Review of attachment 308342 [details] [review]:

I think this patch should come with a documentation update that clearly states what assumptions one can make about main context ownership wrt to g_application_run
Comment 7 Jasper St. Pierre (not reading bugmail) 2015-08-05 21:35:18 UTC
Created attachment 308821 [details] [review]
gapplication: Acquire the main context before running

Otherwise, we'll acquire it on every loop iteration, which can leave us
vulnerable to racing another thread for the acquisition of the main
context.

This can break methods like g_main_context_invoke, which try to acquire
a context to figure out if it can invoke the method synchronously or
need to defer to an idle. In these cases, it isn't guaranteed that the
invocation function will be invoked in the default main context,
e.g. the one that GApplication is holding.

This also matches what GMainLoop is doing.
Comment 8 desrt's bugzilla bot 2015-08-14 11:18:26 UTC
I agree that this seems nice in practice, but first a question: why were you being harmed by the behaviour of g_main_context_invoke()?
Comment 9 Jasper St. Pierre (not reading bugmail) 2015-08-14 15:45:40 UTC
We were doing work from a separate thread and using g_main_context_invoke to invoke a callback on the main application thread. This callback is a GJS callback, so it absolutely needed to be on the same thread as the application, otherwise, GJS started printing wacky errors and eventually crashed.
Comment 10 Xavier Claessens 2015-09-29 17:38:35 UTC
Oh, while reading again g_application_run() I noticed that it pass NULL to g_main_context_iterate() for every iteration, which cause calling g_main_context_default() each time.

That function starts with a comment:
/* Slow, but safe */

So maybe we want to call it only once in g_application_run(). That match even more g_main_loop_run() because g_main_loop_new(NULL) will call call g_main_context_default() only once as well.
Comment 11 Jasper St. Pierre (not reading bugmail) 2015-12-03 04:09:43 UTC
Ping on this again?
Comment 12 Cosimo Cecchi 2015-12-16 00:40:22 UTC
FWIW we've been shipping with this patch for months on Endless and we haven't seen any negative consequence.
Comment 13 Matthias Clasen 2015-12-16 14:16:23 UTC
Review of attachment 308821 [details] [review]:

ok, its been long enough. I'll make the executive decision.
Comment 14 Matthias Clasen 2015-12-16 14:16:48 UTC
Attachment 308821 [details] pushed as a379a0a - gapplication: Acquire the main context before running
Comment 15 Xavier Claessens 2015-12-16 16:37:49 UTC
Created attachment 317519 [details] [review]
GApplication: Avoid getting the default context repeatidly

This avoids getting a global lock on every main loop iteration.
Comment 16 Xavier Claessens 2015-12-16 16:44:11 UTC
Review of attachment 317519 [details] [review]:

Unrelated to this bug, opened bug #759554 for it.