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 354642 - g_main_context_wakeup not async safe
g_main_context_wakeup not async safe
Status: RESOLVED NOTABUG
Product: glib
Classification: Platform
Component: mainloop
2.12.x
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2006-09-06 15:48 UTC by Gustavo Carneiro
Modified: 2006-09-06 16:30 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Gustavo Carneiro 2006-09-06 15:48:41 UTC
g_main_context_wakeup uses a mutex, therefore it cannot be used by a unix signal handler.
Comment 1 Sebastian Wilhelmi 2006-09-06 16:15:51 UTC
That's correct. Unfortunately there is not much, we can do about it. 

A solution would be to always write into the wakeup pipe and try to empty it before the poll, but there'd be still a race condition.

So if you need to wakeup the poll from within a signal handler, open your own pipe for it, add it to the poll, and write into that in the signal handler. Note though that a filled buffer for the pipe might, depending on fcntl options, cause the signal handler and thus the whole program to hang. Another reason to avoid writing into the pipe without knowing, whether someone is listening.
Comment 2 Matthias Clasen 2006-09-06 16:20:43 UTC
Could we get away without the locking if we change poll_waiting atomically ?
Comment 3 Sebastian Wilhelmi 2006-09-06 16:30:49 UTC
I think, doing it atomically wouldn't help, as we need to test for poll_waiting, write the char and set it all atomically, to make the following asumption in glib/gmain.c work:

   2499   if (!context->poll_waiting)
   2500     {
   2501 #ifndef G_OS_WIN32
   2502       gchar a;
   2503       read (context->wake_up_pipe[0], &a, 1);
   2504 #endif
   2505     }

There certainly is a way to make it work without locks, but getting the logic right would be a real pain and it would surely be less efficient (OK, the latter is not really an issue here, I assume).