Created attachment 1903 Patch to reduce CPU usage due to busy-wait recv() loops. When the mailwatch process wakes up to poll an IMAP server, it uses large amounts of CPU for 8-10 seconds. This is caused by busy-wait loops that spin calling recv() and gnutls_*_recv() while waiting for network data. Attached is a patch that adds a 50ms usleep() to the busy-wait loops. This drastically reduces CPU usage.
Thing is, I don't understand why this patch is necessary, and that bothers me. The socket is set to non-blocking mode, sure. But the select() call performed before the read() is there to verify that the socket has data ready to be read. If not, it shouldn't even call read(), and if so, read() should return immediately with data.
Created attachment 2014 fix select() return code check Actually, no... the bug is higher up. The sleeps are unnecessary. The while() loop for the select() should also continue running if ret == 0, not just (ret < 0 && EINTR == errno). Does this patch work for you? Seems to be ok here.
I'm just gonna go ahead and check it in. Feel free to reopen if it's still broken.
> Thing is, I don't understand why this patch is necessary, and > that bothers me. A agree, the sleep() shouldn't be necessary -- it was just an easy work-around by somebody who 1) has never used gnutls before and didn't know how the sematics differed from normal read() calls, and 2) hadn't taken the time to figure out the looping logic. I'll give the new code a try sometime in the next week.