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