GNOME Bugzilla – Bug 640738
glib/gtk + twisted main loop is eating up all CPU when a subprocess terminates
Last modified: 2011-02-04 09:50:25 UTC
This is trivial to reproduce (Fedora 14, Ubuntu 10.10, etc), below is a simple test case, for more details see: http://twistedmatrix.com/trac/ticket/4658 {{{ #!/usr/bin/python #import gtk from twisted.internet import gtk2reactor gtk2reactor.install() from twisted.internet import reactor import subprocess subprocess.Popen(['sleep', '5']) #gtk.main() reactor.run() }}} For those that don't know twisted, {{{gtk2reactor.install()}}} ensures that the gtk2 main loop runs when you call {{{reactor.run()}}} Whether it's twisted or pygtk that makes it misbehave (or the combination of the two)... users see my application using 100% CPU and I get flamed. Please help!
Just found that running the reactor with: reactor.run(installSignalHandlers=False) Seems to solve this particular problem, although it may have other undesired side effects.
Consider this pygtk function, particularly the `read` call it contains: static gboolean pygtk_main_watch_check(GSource *source) { PyGILState_STATE state; #ifdef HAVE_PYSIGNAL_SETWAKEUPFD PySignalWatchSource *real_source = (PySignalWatchSource *)source; GPollFD *poll_fd = &real_source->fd; int data_size = 0; if (poll_fd->revents & G_IO_IN) data_size = read(poll_fd->fd, 0, 1); #endif state = pyg_gil_state_ensure(); if (PyErr_CheckSignals() == -1 && gtk_main_level() > 0) { PyErr_SetNone(PyExc_KeyboardInterrupt); gtk_main_quit(); } pyg_gil_state_release(state); return FALSE; } My understanding of read() is that it can *only* fail with EINVAL with 0 as the 2nd argument.
Created attachment 179466 [details] [review] This is the obvious patch. This fixes the CPU usage behavior of the example in the comment above.
Here's an even simpler reproduction of the bug: import signal import gtk signal.signal(signal.SIGCHLD, lambda *args: None) gtk.main() Run it, send it SIGCHLD. CPU usage goes to 100%.
I've tested the fix against pygtk 2.22.0, it works fine. Please push this fix asap. 100% cpu usage is clearly not acceptable to users... I can't modify the title of this bug, but clearly this has nothing to do with twisted (as demonstrated by the simple test case above).
This problem has been fixed in the development version. The fix will be available in the next major software release. Thank you for your bug report.
*** Bug 638780 has been marked as a duplicate of this bug. ***
I have filed bugs with all the distros that ship the buggy version, some of them have already pushed a patch for testing (gentoo), some of them didn't need one (debian) and some of them have continued to silently ignore the problem (fedora, ubuntu). To make it easier to use a patched version of 2.22 (which is used in a lot of places), I have put a copy here: http://winswitch.org/src/pygtk-2.22.1.tar.bz2 (I use this from jhbuild scripts for example) I have named it pygtk-2.22.1 since it would seem that pygtk does not issue minor bugfix releases (information from an email conversation on the mailing list): http://www.mail-archive.com/pygtk@daa.com.au/msg20008.html