1535993 Chasing strange db errors. draft
authorISHIKAWA, Chiaki <ishikawa@yk.rim.or.jp>
Mon, 23 May 2022 03:03:50 +0900
changeset 116808 dcdff8b955b36cc0abcd6f64c3fb1b2b4423fc10
parent 116807 9d7a99e81cdbd85d83f8aa05390b0a2d589ce3b7
child 116809 b6752c4bb7495d2efece4baeee0d3ae8f483b8c6
push id15955
push userishikawa@yk.rim.or.jp
push dateSun, 22 May 2022 18:04:18 +0000
treeherdertry-comm-central@beaacca97fbd [default view] [failures only]
bugs1535993
1535993 Chasing strange db errors.
mailnews/base/src/nsMsgDBFolder.cpp
mailnews/db/msgdb/src/nsMsgDatabase.cpp
mailnews/local/src/nsLocalMailFolder.cpp
mailnews/local/src/nsMailboxService.cpp
--- a/mailnews/base/src/nsMsgDBFolder.cpp
+++ b/mailnews/base/src/nsMsgDBFolder.cpp
@@ -979,18 +979,28 @@ nsresult nsMsgDBFolder::CreateFileForDB(
 
   dbPath.forget(dbFile);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMsgDBFolder::GetMsgDatabase(nsIMsgDatabase** aMsgDatabase) {
   NS_ENSURE_ARG_POINTER(aMsgDatabase);
-  GetDatabase();
-  if (!mDatabase) return NS_ERROR_FAILURE;
+#if DEBUG
+  nsresult rv =
+#endif
+    GetDatabase();
+  if (!mDatabase) {
+#if DEBUG
+    fprintf(stderr,"(debug): nsMsgDBFolder::GetMsgDatabase: mDatabase was nullptr after the call to GetDatabase.\n");
+    fprintf(stderr,"(debug): nsMsgDBFolder::GetMsgDatabase: GetDatabase() returned 0x%0x.\n", (uint32_t) rv);
+#endif
+    return NS_ERROR_FAILURE;
+  }
+
   NS_ADDREF(*aMsgDatabase = mDatabase);
   mDatabase->SetLastUseTime(PR_Now());
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMsgDBFolder::SetMsgDatabase(nsIMsgDatabase* aMsgDatabase) {
   if (mDatabase) {
@@ -4633,16 +4643,22 @@ NS_IMETHODIMP nsMsgDBFolder::EnableNotif
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 NS_IMETHODIMP nsMsgDBFolder::GetMessageHeader(nsMsgKey msgKey,
                                               nsIMsgDBHdr** aMsgHdr) {
   NS_ENSURE_ARG_POINTER(aMsgHdr);
   nsCOMPtr<nsIMsgDatabase> database;
   nsresult rv = GetMsgDatabase(getter_AddRefs(database));
+#if DEBUG
+  if (NS_FAILED(rv)) {
+    fprintf(stderr,"msgKey = %d\n", msgKey);
+    NS_ASSERTION(NS_SUCCEEDED(rv), "GetMsgDatabase(getter_AddRefs(database)) returned failure.");
+  }
+#endif
   NS_ENSURE_SUCCESS(rv, rv);
   return (database) ? database->GetMsgHdrForKey(msgKey, aMsgHdr)
                     : NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP nsMsgDBFolder::GetDescendants(
     nsTArray<RefPtr<nsIMsgFolder>>& aDescendants) {
   aDescendants.Clear();
--- a/mailnews/db/msgdb/src/nsMsgDatabase.cpp
+++ b/mailnews/db/msgdb/src/nsMsgDatabase.cpp
@@ -1771,36 +1771,45 @@ NS_IMETHODIMP nsMsgDatabase::GetMsgHdrFo
   // up in trace logs, we just remember the most recent time any db was used,
   // which should be close enough for our purposes.
   m_lastUseTime = gLastUseTime;
 
 #ifdef DEBUG_bienvenu1
   NS_ASSERTION(m_folder, "folder should be set");
 #endif
 
+#ifdef DEBUG
+  NS_ASSERTION( ! (  (!pmsgHdr || !m_mdbAllMsgHeadersTable || !m_mdbStore) ), "failed condition in GetMsgHdrForKey.");  
+#endif
   if (!pmsgHdr || !m_mdbAllMsgHeadersTable || !m_mdbStore)
     return NS_ERROR_NULL_POINTER;
 
   *pmsgHdr = NULL;
   err = GetHdrFromUseCache(key, pmsgHdr);
-  if (NS_SUCCEEDED(err) && *pmsgHdr) return err;
+  // Found it in the cache return.
+  if (NS_SUCCEEDED(err) && *pmsgHdr) return NS_OK;
 
   rowObjectId.mOid_Id = key;
   rowObjectId.mOid_Scope = m_hdrRowScopeToken;
   err = m_mdbAllMsgHeadersTable->HasOid(GetEnv(), &rowObjectId, &hasOid);
+#if DEBUG
+  NS_ASSERTION(NS_SUCCEEDED(err), "HasOid call failed.");
+#endif
   if (NS_SUCCEEDED(err) /* && hasOid */) {
     nsIMdbRow* hdrRow;
     err = m_mdbStore->GetRow(GetEnv(), &rowObjectId, &hdrRow);
 
     if (NS_SUCCEEDED(err)) {
       if (!hdrRow) {
         err = NS_ERROR_NULL_POINTER;
       } else {
         //        NS_ASSERTION(hasOid, "we had oid, right?");
         err = CreateMsgHdr(hdrRow, key, pmsgHdr);
+
+        NS_ASSERTION(NS_SUCCEEDED(err), "CreateMsgHdr failed.");
       }
     }
   }
 
   return err;
 }
 
 NS_IMETHODIMP nsMsgDatabase::DeleteMessage(nsMsgKey key,
--- a/mailnews/local/src/nsLocalMailFolder.cpp
+++ b/mailnews/local/src/nsLocalMailFolder.cpp
@@ -224,19 +224,31 @@ nsresult nsMsgLocalMailFolder::GetDataba
   nsCOMPtr<nsIMsgDatabase> msgDB;
   return GetDatabaseWOReparse(getter_AddRefs(msgDB));
 }
 
 // we treat failure as null db returned
 NS_IMETHODIMP nsMsgLocalMailFolder::GetDatabaseWOReparse(
     nsIMsgDatabase** aDatabase) {
   NS_ENSURE_ARG(aDatabase);
-  if (m_parsingFolder) return NS_MSG_ERROR_FOLDER_SUMMARY_OUT_OF_DATE;
-
+
+  if (m_parsingFolder) {
+#if DEBUG
+    fprintf(stderr,
+            "(debug): nsMsgLocalMailFolder::GetDatabaseWOReparse(nsIMsgDatabase **aDatabase) "
+            " - folder summary out of date.\n");
+#endif
+    return NS_MSG_ERROR_FOLDER_SUMMARY_OUT_OF_DATE;
+  }
   nsresult rv = NS_OK;
+#if DEBUG
+  fprintf(stderr,
+          "{debug}: nsMsgLocalMailFolder::GetDatabaseWOReparse(nsIMsgDatabase **aDatabase) "
+          "- mDdatabase = 0x%p\n", (void *) mDatabase);
+#endif
   if (!mDatabase) {
     rv = OpenDatabase();
     if (mDatabase) {
       // we save mDatabase since UpdateNewMessages() may set it to null.
       nsCOMPtr<nsIMsgDatabase> database = mDatabase;
       mDatabase->AddListener(this);
       UpdateNewMessages();
 #if DEBUG
@@ -827,47 +839,109 @@ NS_IMETHODIMP nsMsgLocalMailFolder::SetP
 }
 
 NS_IMETHODIMP nsMsgLocalMailFolder::GetName(nsAString& aName) {
   ReadDBFolderInfo(false);
   return nsMsgDBFolder::GetName(aName);
 }
 
 nsresult nsMsgLocalMailFolder::OpenDatabase() {
+#if DEBUG
+  fprintf(stderr, "(debug): mDatabase = 0x%p on etry to  nsMsgLocalMailFolder::OpenDatabase()\n",
+          (void *) mDatabase);
+#endif
+
   nsresult rv;
   nsCOMPtr<nsIMsgDBService> msgDBService =
       do_GetService(NS_MSGDB_SERVICE_CONTRACTID, &rv);
+#if DEBUG
+  if (NS_FAILED(rv)) {
+    fprintf(stderr, "{debug}: nsMsgLocalMailFolder::OpenDatabase() msgDBService = do_GetService(NS_MSGDB_SERVICE_CONTRACTID, &rv); failed.\n");
+  }
+#endif
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIFile> file;
   rv = GetFilePath(getter_AddRefs(file));
+#if DEBUG
+  if (NS_FAILED(rv)) {
+    fprintf(stderr,
+            "{debug}: nsMsgLocalMailFolder::OpenDatabase() GetFilePath failed.\n");
+  }
+#endif
+
+  NS_ENSURE_SUCCESS(rv, rv);
 
   rv = msgDBService->OpenFolderDB(this, true, getter_AddRefs(mDatabase));
+#if DEBUG
+  fprintf(stderr,
+          "(debug): nsMsgLocalMailFolder::OpenDatabase() "
+          "rv = msgDBService->OpenFolderDB(this, true, getter_AddRefs(mDatabase)):\n"
+          "rv = 0x%08lx %s\n",
+          (unsigned long) rv,
+          rv == NS_MSG_ERROR_FOLDER_SUMMARY_MISSING
+          ? "(= NS_MSG_ERROR_FOLDER_SUMMARY_MISSING)" : "");
+#endif
   if (rv == NS_MSG_ERROR_FOLDER_SUMMARY_MISSING) {
     // check if we're a real folder by looking at the parent folder.
     nsCOMPtr<nsIMsgFolder> parent;
     GetParent(getter_AddRefs(parent));
     if (parent) {
       // This little dance creates an empty .msf file and then checks
       // if the db is valid - this works if the folder is empty, which
       // we don't have a direct way of checking.
       nsCOMPtr<nsIMsgDatabase> db;
       rv = msgDBService->CreateNewDB(this, getter_AddRefs(db));
       if (db) {
         UpdateSummaryTotals(true);
         db->Close(true);
         mDatabase = nullptr;
         db = nullptr;
         rv = msgDBService->OpenFolderDB(this, false, getter_AddRefs(mDatabase));
-        if (NS_FAILED(rv)) mDatabase = nullptr;
+        if (NS_FAILED(rv)) {
+#if DEBUG
+            fprintf(stderr,
+                    "(debug): nsMsgLocalMailFolder::OpenDatabase() "
+                    "rv = msgDBService->OpenFolderDB(this, false, getter_AddRefs(mDatabase)) failed;\n"
+                    "rv = %08lx\nSetting mDatabase to nullptr.\n", (unsigned long) rv);
+#endif
+          mDatabase = nullptr;
+        }
+      } 
+      else // !db
+      {
+          fprintf(stderr,
+                  "(debug): nsMsgLocalMailFolder::OpenDatabase() "
+                  "rv = msgDBService->CreateNewDB(this, getter_AddRefs(db)); db was null\n"
+                  "rv = %08lx\n\n", (unsigned long)rv);
       }
+    } 
+    else // !parent
+    {
+#if DEBUG
+      fprintf(stderr,
+              "(debug): nsMsgLocalMailFolder::OpenDatabase() parent was null after  GetParent(getter_AddRefs(parent)\n");
+#endif
     }
-  } else if (NS_FAILED(rv))
+  } else if (NS_FAILED(rv)) {
+#if DEBUG
+    fprintf(stderr,
+            "(debug): nsMsgLocalMailFolder::OpenDatabase() "
+            "rv = msgDBService->OpenFolderDB(this, true, getter_AddRefs(mDatabase)) failed;\n"
+            "and it was not NS_MSG_ERROR_FOLDER_SUMMARY_MISSING.\n"
+            "rv = %08lx\nSetting mDatabase to nullptr.\n", (unsigned long)rv);
+#endif
     mDatabase = nullptr;
-
+  }
+
+#if DEBUG
+  if(NS_FAILED(rv))
+    fprintf(stderr,"(debug): nsMsgLocalMailFolder::OpenDatabase() returned rv=%08lx\n",
+            (unsigned long) rv);
+#endif
   return rv;
 }
 
 NS_IMETHODIMP
 nsMsgLocalMailFolder::GetDBFolderInfoAndDB(nsIDBFolderInfo** folderInfo,
                                            nsIMsgDatabase** db) {
   if (!db || !folderInfo || !mPath || mIsServer)
     return NS_ERROR_NULL_POINTER;  // ducarroz: should we use
--- a/mailnews/local/src/nsMailboxService.cpp
+++ b/mailnews/local/src/nsMailboxService.cpp
@@ -600,15 +600,26 @@ nsMailboxService::MessageURIToMsgHdr(con
                                      nsIMsgDBHdr** _retval) {
   NS_ENSURE_ARG_POINTER(_retval);
 
   nsresult rv = NS_OK;
 
   nsCOMPtr<nsIMsgFolder> folder;
   nsMsgKey msgKey;
 
+#if DEBUG
+  fprintf(stderr,"(debug): nsMailboxService::MessageURIToMsgHdr:uri=%s\n", PromiseFlatCString(uri).get());
+#endif
+
   rv = DecomposeMailboxURI(uri, getter_AddRefs(folder), &msgKey);
   NS_ENSURE_SUCCESS(rv, rv);
 
+#if DEBUG
+  fprintf(stderr,"(debug): msgKey=%d\n", msgKey);
+#endif
+
   rv = folder->GetMessageHeader(msgKey, _retval);
+
+  NS_ASSERTION(NS_SUCCEEDED(rv), "GetMessageHeader(msgKey, _retval) returned failure");
+
   NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }