--- /dev/null
+Return-Path: <davrieb@liegesta.at>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 85C5B431FB6\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Nov 2011 04:23:10 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id A9sXZ9LNqsDT for <notmuch@notmuchmail.org>;\r
+ Sun, 6 Nov 2011 04:23:09 -0800 (PST)\r
+Received: from plum.liegesta.at (plum.liegesta.at [83.169.17.237])\r
+ by olra.theworths.org (Postfix) with ESMTP id B0570429E21\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Nov 2011 04:23:09 -0800 (PST)\r
+Received: from ambiguous-mid.liegesta.at (mk046207255168.a1.net\r
+ [46.207.255.168]) (Authenticated sender: davrieb)\r
+ by plum.liegesta.at (Postfix) with ESMTPA id 7FE1127B8259;\r
+ Sun, 6 Nov 2011 13:23:08 +0100 (CET)\r
+Received: by ambiguous-mid.liegesta.at (Postfix, from userid 1000)\r
+ id 4E0E91FF1E; Sun, 6 Nov 2011 12:23:06 +0100 (CET)\r
+From: David Riebenbauer <davrieb@liegesta.at>\r
+To: Notmuch Mailing List <notmuch@notmuchmail.org>\r
+Subject: [PATCH 1/3] notmuch-deliver: wait for the database to become unlocked\r
+Date: Sun, 6 Nov 2011 12:23:02 +0100\r
+Message-Id: <1320578584-6642-2-git-send-email-davrieb@liegesta.at>\r
+X-Mailer: git-send-email 1.7.7.1\r
+In-Reply-To: <1320578584-6642-1-git-send-email-davrieb@liegesta.at>\r
+References: <1320578584-6642-1-git-send-email-davrieb@liegesta.at>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 06 Nov 2011 12:23:10 -0000\r
+\r
+If notmuch-deliver just gives up on a locked database this can result\r
+in mail bouncing.\r
+\r
+This patch makes it wait a given time and then retry to open the\r
+database. The wait time starts at a millisecond, and doubles every\r
+time the database is still locked, until an absolute maximum time of\r
+10 minutes is reached.\r
+---\r
+ contrib/notmuch-deliver/src/main.c | 25 ++++++++++++++++++++++---\r
+ 1 files changed, 22 insertions(+), 3 deletions(-)\r
+\r
+diff --git a/contrib/notmuch-deliver/src/main.c b/contrib/notmuch-deliver/src/main.c\r
+index f7a4eaa..e30cbac 100644\r
+--- a/contrib/notmuch-deliver/src/main.c\r
++++ b/contrib/notmuch-deliver/src/main.c\r
+@@ -71,6 +71,9 @@\r
+ #define EX_CONFIG 78\r
+ #endif\r
+ \r
++#define MIN_UWAIT 1000 // 1 millisecond\r
++#define MAX_UWAIT 600000000 // 10 minutes\r
++\r
+ static gboolean opt_create, opt_fatal, opt_folder, opt_version;\r
+ static gboolean opt_verbose = FALSE;\r
+ static gchar **opt_tags = NULL;\r
+@@ -428,10 +431,26 @@ main(int argc, char **argv)\r
+ maildir = g_strdup(db_path);\r
+ \r
+ g_debug("Opening notmuch database `%s'", db_path);\r
+- db = notmuch_database_open(db_path, NOTMUCH_DATABASE_MODE_READ_WRITE);\r
++ gint32 wait_time = 0;\r
++ gint32 next_wait = MIN_UWAIT;\r
++ while ((db = notmuch_database_open(db_path, NOTMUCH_DATABASE_MODE_READ_WRITE)) == NULL) {\r
++ g_debug("Retrying to open database in %u microseconds", next_wait);\r
++ usleep(next_wait);\r
++\r
++ next_wait *= 2;\r
++ if (wait_time + next_wait >= MAX_UWAIT) {\r
++ wait_time = next_wait - MAX_UWAIT;\r
++ if (wait_time <= 0) {\r
++ g_free(db_path);\r
++ if (db == NULL)\r
++ return EX_SOFTWARE;\r
++ }\r
++ }\r
++\r
++ wait_time += next_wait;\r
++ }\r
+ g_free(db_path);\r
+- if (db == NULL)\r
+- return EX_SOFTWARE;\r
++\r
+ if (notmuch_database_needs_upgrade(db)) {\r
+ g_message("Upgrading database");\r
+ notmuch_database_upgrade(db, NULL, NULL);\r
+-- \r
+1.7.7.1\r
+\r