Hello All, I have this code which makes CPU to hog at 100%. (2.56.4-0ubuntu0.18.04.6 ). If I add G_IO_HUP to g_io_add_watch() then issue is resolved.
While it makes sense for user apps to watch on G_IO_HUP event, why does main_loop() has to go infinite loop and hog the CPU, forever if G_IO_HUP is not registered?
This would affect the whole system if there is a buggy user app. So like to know, was this glib behavior an intentional design or a bug?
Seems that G_IO_HUP is unconditionally added into the poll() FD mask, which causes the wakeup, but then nothing handles it, so it continues to wake poll() up on the next main loop iteration, etc.
You should handle G_IO_HUP using g_io_add_watch().
It’s probably not an intentional design feature of GLib, but it’s not entirely a bug either, since I can’t think of a situation where it would be reasonable for an app to watch G_IO_IN but not G_IO_HUP.
Apart from G_IO_HUP are there other events that get unconditionally added into poll?
It’s probably not an intentional design feature of GLib
—> Getting into an indefinite loop can create more problems. In few other places in glib documentation it is also mentioned that
" It is meaningless to specify [Glib::IO_ERR] or [Glib::IO_HUP] in condition ; these conditions will always be reported output if they are true.
Yes, on Unix, G_IO_ERR or G_IO_NVAL are also unconditionally set. I’ve dug into it a little more, and they’re unconditionally returned by poll(). See man 2 poll:
The field revents is an output parameter, filled by the kernel with the
events that actually occurred. The bits returned in revents can
include any of those specified in events, or one of the values POLLERR,
POLLHUP, or POLLNVAL. (These three bits are meaningless in the events
field, and will be set in the revents field whenever the corresponding
condition is true.)
Best to handle G_IO_ERR and G_IO_HUP then
If GLib were to ignore them if you hadn’t specified them in your g_io_add_watch() call, then there’d be no way for your code to tell that the pipe had been closed (or had errored).
If you think the g_io_add_watch() documentation should mention G_IO_ERR/G_IO_HUP/G_IO_NVAL (or should have any other improvements), please open a merge request