andyedinborough / aenetmail

C# POP/IMAP Mail Client
370 stars 153 forks source link

ImapClient. Search(string criteria, bool uid = true) returned exception "An unexpected error occurred: * 1569 EXISTS" #102

Closed sevzas closed 12 years ago

sevzas commented 12 years ago

It looks like there is a bug that occurs on the following sequence of events when doing an IMAP search:

  1. Search command is sent
  2. A new email arrives into the inbox causing async notification to client in the form of "* 1569 EXISTS"
  3. Search results are sent

The code looks for a result that starts with "SEARCH" here (this is what appears to fail)

  if (!response.StartsWith("* SEARCH", StringComparison.InvariantCultureIgnoreCase) && !IsResultOK(response)) {
    throw new Exception(response);
  }

Ofcourse this is extremely rare (first time I've seen this in two months of use)

Also I believe that this method

private string GetTag() {
  _tag++;
  return string.Format("xm{0:000} ", _tag);
}

should have a bit of syncronization to prevent the same id being handed out to two different commands sent at the same time. Unless this API is not guaranteed to be thread-safe.

Maybe remove "tag++;" and replace :

  return string.Format("xm{0:000} ", _tag);

with

  return string.Format("xm{0:000} ", Interlocked.Increment(ref _tag));
sevzas commented 12 years ago

Maybe a strange coincidence - I got another one of these today:

An unexpected error occurred: * 4 EXPUNGE

andyedinborough commented 12 years ago

There really is no way for the client to thread safe. All asynchronous commands or separate threads should use a dedicated instance of the client.

sevzas commented 12 years ago

I agree that using a dedicated instance of the client on each thread negates the need for changes I suggested to GetTag() BUT it does not address the core problem that I raised in this issue.

I am using one instance of the client on one thread. I apologize if my original problem report is not clear - I will clarify it if necessary. AE.Net.Mail is already an excellent package but if this problem is corrected it will be even more reliable.