[PATCH] HACK: lib: add brute and ignorant handling of database lock errors

Jani Nikula jani at nikula.org
Thu Jan 1 10:22:03 PST 2015


From: Jani Nikula <jani.nikula at intel.com>

Instead of failing at first try, back off for an increasing number of
seconds, and only fail after three attempts.

One of my notmuch installations that gets plenty of mail hits db
locked exceptions quite often, and even more than before since I
switched to using notmuch insert. These are quite annoying in the
Emacs UI as there is virtually no error handling for tagging, and the
tag change highlighting gets confused.

This is a dirty hack, but for me it's much more preferrable to the the
db locked exceptions. With the retries and increasing back-off timeout
I hardly ever see the exceptions now.

Maybe this inspires someone to fix this for real. ;)
---
 lib/database.cc | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index 3601f9ded307..50063c55969b 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -819,8 +819,23 @@ notmuch_database_open (const char *path,
 	string last_thread_id;
 
 	if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
-	    notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
-							       Xapian::DB_CREATE_OR_OPEN);
+	    /*
+	     * Brute and ignorant handling of database lock errors.
+	     * Instead of failing at first try, back off for an
+	     * increasing number of seconds.
+	     */
+	    for (unsigned int tries = 1; ! notmuch->xapian_db; tries++) {
+		try {
+		    notmuch->xapian_db =
+			new Xapian::WritableDatabase (xapian_path,
+						      Xapian::DB_CREATE_OR_OPEN);
+		} catch (const Xapian::DatabaseLockError &error) {
+		    if (tries > 3)
+			throw;
+
+		    sleep(tries);
+		}
+	    }
 	} else {
 	    notmuch->xapian_db = new Xapian::Database (xapian_path);
 	}
-- 
2.1.4



More information about the notmuch mailing list