Ensure IMAP flushes data to disk before quitting. Closes #5095.
Merge remote-tracking branch 'drebs/feature/5095_add-semaphore-for-safe-exit' into develop