imap: improve shutdown