There is a slight possibility for a race condition triggered by the following happening:
- go-imapgrab obtains the list of emails known remotely
- Two things happen at the same time:
- go-imapgrab determines which emails have already been downloaded
- some emails are deleted remotely
- go-imapgrab requests the download of emails, but emails are not identified by their UIDs but rather by their index on the IMAP server
For example, assume there is a mailbox with 103 emails, the first 100 of which are already known locally. Then, go-imapgrab retrieves the list of 103 emails from the remote server. Then, someone deletes the 3 first emails, bringing the total count down to 100 again. That happens while go-imapgrab determines that the emails with indices 101 to 103 shall be downloaded. Then, go-imapgrab requests the download of those emails, receiving an error because no such emails are there. There can be other cases where incorrect emails would be downloaded instead.
This looks like a race condition that cannot be avoided (any suggestions to that end are welcome). Instead of avoiding it, we can live with detecting it. Thus, after downloading an email but before storing it on disk, we can perform a check for whether the UID of the email we did download is identical to the one we expected to be downloading. Such a sanity check would require some refactoring of the code base but seems like a viable option.