Patch attached. It's possible this'll livelock certain glib libraries that want to modify the glib main context while another thread is running. I don't see a good way to fix that, but a bad way would be to apply some heuristic (such as a 10 ms delay between calling select() and assuming it actually was called).