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