fix assertion looking for connection in imap connection cache, r=standard8, sr=neil 454932
authorDavid Bienvenu <bienvenu@nventure.com>
Fri, 12 Sep 2008 06:47:59 -0700
changeset 319 b9996ad44f8d58f30c4042e603408e0977ece261
parent 318 28daa79edfa11bd67f9098070636ffd16ef9b000
child 320 104de1a05154c19dd9020efa7e52fb935efd828a
push idunknown
push userunknown
push dateunknown
reviewersstandard8, neil
bugs454932
fix assertion looking for connection in imap connection cache, r=standard8, sr=neil 454932
mailnews/imap/src/nsImapIncomingServer.cpp
--- a/mailnews/imap/src/nsImapIncomingServer.cpp
+++ b/mailnews/imap/src/nsImapIncomingServer.cpp
@@ -686,30 +686,27 @@ nsImapIncomingServer::GetImapConnection(
 
   PRInt32 cnt = m_connectionCache.Count();
 
   *aImapConnection = nsnull;
   // iterate through the connection cache for a connection that can handle this url.
   PRBool userCancelled = PR_FALSE;
 
   // loop until we find a connection that can run the url, or doesn't have to wait?
-  for (PRInt32 i = 0; i < cnt && !canRunUrlImmediately && !canRunButBusy; i++)
+  for (PRInt32 i = cnt - 1; i >= 0 && !canRunUrlImmediately && !canRunButBusy; i--)
   {
     connection = m_connectionCache[i];
     if (connection)
     {
-      if (ConnectionTimeOut(connection))
+      PRBool badConnection = ConnectionTimeOut(connection);
+      if (!badConnection)
       {
-        connection = nsnull;
-        i--; cnt--; // if the connection times out, we'll remove it from the array,
-            // so we need to adjust the array index.
-      }
-      else
-      {
-        rv = connection->CanHandleUrl(aImapUrl, &canRunUrlImmediately, &canRunButBusy);
+        badConnection = NS_FAILED(connection->CanHandleUrl(aImapUrl, 
+                                                           &canRunUrlImmediately, 
+                                                           &canRunButBusy));
 #ifdef DEBUG_bienvenu
         nsCAutoString curSelectedFolderName;
         if (connection)    
           connection->GetSelectedMailboxName(getter_Copies(curSelectedFolderName));
         // check that no other connection is in the same selected state.
         if (!curSelectedFolderName.IsEmpty())
         {
           for (PRUint32 j = 0; j < cnt; j++)
@@ -724,22 +721,21 @@ nsImapIncomingServer::GetImapConnection(
                 NS_ASSERTION(!curSelectedFolderName.Equals(otherSelectedFolderName), "two connections selected on same folder");
               }
 
             }
           }
         }
 #endif // DEBUG_bienvenu
       }
-    }
-    if (NS_FAILED(rv))
-    {
+      if (badConnection)
+      {
         connection = nsnull;
-        rv = NS_OK; // don't want to return this error, just don't use the connection
         continue;
+      }
     }
 
     // if this connection is wrong, but it's not busy, check if we should designate
     // it as the free connection.
     if (!canRunUrlImmediately && !canRunButBusy && connection)
     {
         rv = connection->IsBusy(&isBusy, &isInboxConnection);
         if (NS_FAILED(rv))
@@ -758,21 +754,16 @@ nsImapIncomingServer::GetImapConnection(
           }
         }
     }
     // don't leave this loop with connection set if we can't use it!
     if (!canRunButBusy && !canRunUrlImmediately)
       connection = nsnull;
   }
 
-  if (ConnectionTimeOut(connection))
-      connection = nsnull;
-  if (ConnectionTimeOut(freeConnection))
-    freeConnection = nsnull;
-
   nsImapState requiredState;
   aImapUrl->GetRequiredImapState(&requiredState);
   // refresh cnt in case we killed one or more dead connections. This
   // will prevent us from not spinning up a new connection when all
   // connections were dead.
   cnt = m_connectionCache.Count();
   // if we got here and we have a connection, then we should return it!
   if (canRunUrlImmediately && connection)