ae11a9
diff -up thunderbird-60.2.1/comm/ldap/xpcom/public/nsILDAPOperation.idl.1460871-ldap-query thunderbird-60.2.1/comm/ldap/xpcom/public/nsILDAPOperation.idl
ae11a9
--- thunderbird-60.2.1/comm/ldap/xpcom/public/nsILDAPOperation.idl.1460871-ldap-query	2018-10-01 16:52:39.000000000 +0200
ae11a9
+++ thunderbird-60.2.1/comm/ldap/xpcom/public/nsILDAPOperation.idl	2018-10-04 09:40:04.491575949 +0200
ae11a9
@@ -52,6 +52,10 @@ interface nsILDAPOperation : nsISupports
ae11a9
      * private parameter (anything caller desires)
ae11a9
      */
ae11a9
     attribute nsISupports closure;
ae11a9
+    /**
ae11a9
+     * number of the request for compare that the request is still valid.
ae11a9
+     */
ae11a9
+    attribute unsigned long requestNum;
ae11a9
 
ae11a9
     /**
ae11a9
      * No time and/or size limit specified
ae11a9
diff -up thunderbird-60.2.1/comm/ldap/xpcom/src/nsLDAPOperation.cpp.1460871-ldap-query thunderbird-60.2.1/comm/ldap/xpcom/src/nsLDAPOperation.cpp
ae11a9
--- thunderbird-60.2.1/comm/ldap/xpcom/src/nsLDAPOperation.cpp.1460871-ldap-query	2018-10-01 16:52:39.000000000 +0200
ae11a9
+++ thunderbird-60.2.1/comm/ldap/xpcom/src/nsLDAPOperation.cpp	2018-10-04 09:40:04.491575949 +0200
ae11a9
@@ -400,6 +400,19 @@ convertControlArray(nsIArray *aXpcomArra
ae11a9
     return NS_OK;
ae11a9
 }
ae11a9
 
ae11a9
+  /* attribute unsigned long requestNum; */
ae11a9
+NS_IMETHODIMP nsLDAPOperation::GetRequestNum(uint32_t *aRequestNum)
ae11a9
+{
ae11a9
+    *aRequestNum = mRequestNum;
ae11a9
+    return NS_OK;
ae11a9
+}
ae11a9
+
ae11a9
+NS_IMETHODIMP nsLDAPOperation::SetRequestNum(uint32_t aRequestNum)
ae11a9
+{
ae11a9
+    mRequestNum = aRequestNum;
ae11a9
+    return NS_OK;
ae11a9
+}
ae11a9
+
ae11a9
 NS_IMETHODIMP
ae11a9
 nsLDAPOperation::SearchExt(const nsACString& aBaseDn, int32_t aScope,
ae11a9
                            const nsACString& aFilter,
ae11a9
diff -up thunderbird-60.2.1/comm/ldap/xpcom/src/nsLDAPOperation.h.1460871-ldap-query thunderbird-60.2.1/comm/ldap/xpcom/src/nsLDAPOperation.h
ae11a9
--- thunderbird-60.2.1/comm/ldap/xpcom/src/nsLDAPOperation.h.1460871-ldap-query	2018-10-01 16:52:39.000000000 +0200
ae11a9
+++ thunderbird-60.2.1/comm/ldap/xpcom/src/nsLDAPOperation.h	2018-10-04 09:40:04.491575949 +0200
ae11a9
@@ -36,6 +36,8 @@ class nsLDAPOperation : public nsILDAPOp
ae11a9
      * used to break cycles
ae11a9
      */
ae11a9
     void Clear();
ae11a9
+    // Stores the request number for later check of the operation is still valid
ae11a9
+    int32_t mRequestNum;
ae11a9
 
ae11a9
   private:
ae11a9
     virtual ~nsLDAPOperation();
ae11a9
diff -up thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPDirectoryQuery.cpp.1460871-ldap-query thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPDirectoryQuery.cpp
ae11a9
--- thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPDirectoryQuery.cpp.1460871-ldap-query	2018-10-01 16:52:43.000000000 +0200
ae11a9
+++ thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPDirectoryQuery.cpp	2018-10-04 09:40:55.334670404 +0200
ae11a9
@@ -22,6 +22,8 @@
ae11a9
 
ae11a9
 using namespace mozilla;
ae11a9
 
ae11a9
+extern mozilla::LazyLogModule gLDAPLogModule; // defined in nsLDAPService.cpp
ae11a9
+
ae11a9
 // nsAbLDAPListenerBase inherits nsILDAPMessageListener
ae11a9
 class nsAbQueryLDAPMessageListener : public nsAbLDAPListenerBase
ae11a9
 {
ae11a9
@@ -66,7 +68,6 @@ protected:
ae11a9
 
ae11a9
   bool mFinished;
ae11a9
   bool mCanceled;
ae11a9
-  bool mWaitingForPrevQueryToFinish;
ae11a9
 
ae11a9
   nsCOMPtr<nsIMutableArray> mServerSearchControls;
ae11a9
   nsCOMPtr<nsIMutableArray> mClientSearchControls;
ae11a9
@@ -94,7 +95,6 @@ nsAbQueryLDAPMessageListener::nsAbQueryL
ae11a9
   mResultLimit(resultLimit),
ae11a9
   mFinished(false),
ae11a9
   mCanceled(false),
ae11a9
-  mWaitingForPrevQueryToFinish(false),
ae11a9
   mServerSearchControls(serverSearchControls),
ae11a9
   mClientSearchControls(clientSearchControls)
ae11a9
 {
ae11a9
@@ -116,9 +116,6 @@ nsresult nsAbQueryLDAPMessageListener::C
ae11a9
         return NS_OK;
ae11a9
 
ae11a9
     mCanceled = true;
ae11a9
-    if (!mFinished)
ae11a9
-      mWaitingForPrevQueryToFinish = true;
ae11a9
-
ae11a9
     return NS_OK;
ae11a9
 }
ae11a9
 
ae11a9
@@ -129,6 +126,8 @@ NS_IMETHODIMP nsAbQueryLDAPMessageListen
ae11a9
 
ae11a9
   int32_t messageType;
ae11a9
   rv = aMessage->GetType(&messageType);
ae11a9
+  uint32_t requestNum;
ae11a9
+  mOperation->GetRequestNum(&requestNum);
ae11a9
   NS_ENSURE_SUCCESS(rv, rv);
ae11a9
 
ae11a9
   bool cancelOperation = false;
ae11a9
@@ -137,6 +136,14 @@ NS_IMETHODIMP nsAbQueryLDAPMessageListen
ae11a9
   {
ae11a9
     MutexAutoLock lock (mLock);
ae11a9
 
ae11a9
+    if (requestNum != sCurrentRequestNum) {
ae11a9
+      MOZ_LOG(gLDAPLogModule, mozilla::LogLevel::Debug,
ae11a9
+           ("nsAbQueryLDAPMessageListener::OnLDAPMessage: Ignoring message with "
ae11a9
+            "request num %d, current request num is %d.",
ae11a9
+            requestNum, sCurrentRequestNum));
ae11a9
+      return NS_OK;
ae11a9
+    }
ae11a9
+
ae11a9
     if (mFinished)
ae11a9
       return NS_OK;
ae11a9
 
ae11a9
@@ -166,11 +173,10 @@ NS_IMETHODIMP nsAbQueryLDAPMessageListen
ae11a9
         rv = OnLDAPMessageSearchResult(aMessage);
ae11a9
       break;
ae11a9
     case nsILDAPMessage::RES_SEARCH_ENTRY:
ae11a9
-      if (!mFinished && !mWaitingForPrevQueryToFinish)
ae11a9
+      if (!mFinished)
ae11a9
         rv = OnLDAPMessageSearchEntry(aMessage);
ae11a9
       break;
ae11a9
     case nsILDAPMessage::RES_SEARCH_RESULT:
ae11a9
-      mWaitingForPrevQueryToFinish = false;
ae11a9
       rv = OnLDAPMessageSearchResult(aMessage);
ae11a9
       NS_ENSURE_SUCCESS(rv, rv);
ae11a9
       break;
ae11a9
@@ -207,6 +213,8 @@ nsresult nsAbQueryLDAPMessageListener::D
ae11a9
   rv = mOperation->Init(mConnection, this, nullptr);
ae11a9
   NS_ENSURE_SUCCESS(rv, rv);
ae11a9
 
ae11a9
+  mOperation->SetRequestNum(++sCurrentRequestNum);
ae11a9
+
ae11a9
   nsAutoCString dn;
ae11a9
   rv = mSearchUrl->GetDn(dn);
ae11a9
   NS_ENSURE_SUCCESS(rv, rv);
ae11a9
diff -up thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPListenerBase.cpp.1460871-ldap-query thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPListenerBase.cpp
ae11a9
--- thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPListenerBase.cpp.1460871-ldap-query	2018-10-01 16:52:43.000000000 +0200
ae11a9
+++ thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPListenerBase.cpp	2018-10-04 09:40:04.492575951 +0200
ae11a9
@@ -20,6 +20,8 @@
ae11a9
 
ae11a9
 using namespace mozilla;
ae11a9
 
ae11a9
+uint32_t nsAbLDAPListenerBase::sCurrentRequestNum = 0;
ae11a9
+
ae11a9
 nsAbLDAPListenerBase::nsAbLDAPListenerBase(nsILDAPURL* url,
ae11a9
                                            nsILDAPConnection* connection,
ae11a9
                                            const nsACString &login,
ae11a9
@@ -249,6 +251,7 @@ NS_IMETHODIMP nsAbLDAPListenerBase::OnLD
ae11a9
     InitFailed();
ae11a9
     return rv;
ae11a9
   }
ae11a9
+  mOperation->SetRequestNum(++sCurrentRequestNum);
ae11a9
 
ae11a9
   // Try non-password mechanisms first
ae11a9
   if (mSaslMechanism.EqualsLiteral("GSSAPI"))
ae11a9
diff -up thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPListenerBase.h.1460871-ldap-query thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPListenerBase.h
ae11a9
--- thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPListenerBase.h.1460871-ldap-query	2018-10-01 16:52:43.000000000 +0200
ae11a9
+++ thunderbird-60.2.1/comm/mailnews/addrbook/src/nsAbLDAPListenerBase.h	2018-10-04 09:40:04.492575951 +0200
ae11a9
@@ -47,6 +47,7 @@ protected:
ae11a9
   int32_t mTimeOut;
ae11a9
   bool mBound;
ae11a9
   bool mInitialized;
ae11a9
+  static uint32_t sCurrentRequestNum;
ae11a9
 
ae11a9
   mozilla::Mutex mLock;
ae11a9
 };