Fix race condition that caused segfaults in imap and xmpp as seen on