v2 _notmuch_message_ensure_metadata exception handling

Subject: v2 _notmuch_message_ensure_metadata exception handling

Date: Sat, 25 Feb 2017 21:35:08 -0400

To: notmuch@notmuchmail.org

Cc:

From: David Bremner


I tried to address most of Jani's comments on v1 [1]. The bad news is
that the test is a bit delicate, it really does need the whole corpus.

I did decide to follow the suggestion to make the _reopen function
private for now. It would be nice if we could hide that complexity
from library users, I guess we'll have to see.


[1]: id:20170225034513.19427-1-david@tethera.net


Interdiff follows.

diff --git a/lib/database.cc b/lib/database.cc
index 1e958b65..ba440d4d 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -1135,7 +1135,7 @@ notmuch_database_close (notmuch_database_t *notmuch)
 }
 
 notmuch_status_t
-notmuch_database_reopen (notmuch_database_t *notmuch)
+_notmuch_database_reopen (notmuch_database_t *notmuch)
 {
     if (notmuch->mode != NOTMUCH_DATABASE_MODE_READ_ONLY)
 	return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;
@@ -1147,8 +1147,8 @@ notmuch_database_reopen (notmuch_database_t *notmuch)
 	    _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
 				   error.get_msg ().c_str ());
 	    notmuch->exception_reported = TRUE;
-	    return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
 	}
+	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
     }
 
     notmuch->view++;
diff --git a/lib/message.cc b/lib/message.cc
index bfb95917..007f1171 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -320,9 +320,8 @@ static void
 _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
 {
     Xapian::TermIterator i, end;
-    notmuch_bool_t success = FALSE;
 
-    if ((field != NULL) && (message->last_view >= message->notmuch->view))
+    if (field && (message->last_view >= message->notmuch->view))
 	return;
 
     const char *thread_prefix = _find_prefix ("thread"),
@@ -338,7 +337,7 @@ _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
      * slightly more costly than looking up individual fields if only
      * one field of the message object is actually used, it's a huge
      * win as more fields are used. */
-    for (int count=0; count < 3 && !success; count++) {
+    for (int count=0; count < 3; count++) {
 	try {
 	    i = message->doc.termlist_begin ();
 	    end = message->doc.termlist_end ();
@@ -408,13 +407,12 @@ _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
 		message->in_reply_to = talloc_strdup (message, "");
 
 	    /* all the way without an exception */
-	    success = TRUE;
+	    break;
 	} catch (const Xapian::DatabaseModifiedError &error) {
-	    notmuch_status_t status = notmuch_database_reopen (message->notmuch);
+	    notmuch_status_t status = _notmuch_database_reopen (message->notmuch);
 	    if (status != NOTMUCH_STATUS_SUCCESS)
 		INTERNAL_ERROR ("unhandled error from notmuch_database_reopen: %s\n",
 				notmuch_status_to_string (status));
-	    success = FALSE;
 	} catch (const Xapian::Error &error) {
 	    INTERNAL_ERROR ("A Xapian exception occurred fetching message metadata: %s\n",
 			    error.get_msg().c_str());
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index 7b35fc5b..8587e86c 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -192,6 +192,9 @@ _notmuch_message_id_compressed (void *ctx, const char *message_id);
 notmuch_status_t
 _notmuch_database_ensure_writable (notmuch_database_t *notmuch);
 
+notmuch_status_t
+_notmuch_database_reopen (notmuch_database_t *notmuch);
+
 void
 _notmuch_database_log (notmuch_database_t *notmuch,
 		       const char *format, ...);
diff --git a/lib/notmuch.h b/lib/notmuch.h
index e9ed01dd..16da8be9 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -363,18 +363,6 @@ notmuch_status_t
 notmuch_database_close (notmuch_database_t *database);
 
 /**
- * Reopen a (read-only) database, synching the database view with that
- * on disk.
- *
- * @returns
- * - NOTMUCH_STATUS_UNSUPPORTED_OPERATION: database is not read only
- * - NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occured trying to
- *   re-open the database.
- */
-notmuch_status_t
-notmuch_database_reopen (notmuch_database_t *database);
-
-/**
  * A callback invoked by notmuch_database_compact to notify the user
  * of the progress of the compaction process.
  */
diff --git a/test/T640-database-modified.sh b/test/T640-database-modified.sh
index 9599417c..516836b0 100755
--- a/test/T640-database-modified.sh
+++ b/test/T640-database-modified.sh
@@ -2,18 +2,22 @@
 test_description="DatabaseModifiedError handling"
 . ./test-lib.sh || exit 1
 
+# add enough messages to trigger the exception
 add_email_corpus
 
 test_begin_subtest "catching DatabaseModifiedError in _notmuch_message_ensure_metadata"
-test_C ${MAIL_DIR} <<'EOF'
+# it seems to need to be an early document to trigger the exception
+first_id=$(notmuch search --output=messages '*'| head -1 | sed s/^id://)
+
+test_C ${MAIL_DIR} <<EOF
 #include <unistd.h>
 #include <stdlib.h>
 #include <notmuch-test.h>
 #include <talloc.h>
+#include <assert.h>
 int
 main (int argc, char **argv)
 {
-    pid_t child;
     const char *path = argv[1];
 
     notmuch_database_t *rw_db, *ro_db;
@@ -23,7 +27,10 @@ main (int argc, char **argv)
     notmuch_tags_t *tags;
 
     EXPECT0 (notmuch_database_open (path, NOTMUCH_DATABASE_MODE_READ_ONLY, &ro_db));
-    EXPECT0 (notmuch_database_find_message (ro_db, "4EFC743A.3060609@april.org", &ro_message));
+    assert(ro_db);
+
+    EXPECT0 (notmuch_database_find_message (ro_db, "${first_id}", &ro_message));
+    assert(ro_message);
 
     EXPECT0 (notmuch_database_open (path, NOTMUCH_DATABASE_MODE_READ_WRITE, &rw_db));
     query = notmuch_query_create(rw_db, "");

Thread: