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