--- /dev/null
+Return-Path: <amdragon@mit.edu>\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 333DE431FC0\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 08:43:57 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] 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 lvH6LqRsPS2H for <notmuch@notmuchmail.org>;\r
+ Mon, 22 Sep 2014 08:43:51 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-7.mit.edu (dmz-mailsec-scanner-7.mit.edu\r
+ [18.7.68.36])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 92A42431FB6\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 08:43:51 -0700 (PDT)\r
+X-AuditID: 12074424-f79346d000004923-48-542043b60783\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id B8.8B.18723.6B340245; Mon, 22 Sep 2014 11:43:50 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id s8MFhndg028845; \r
+ Mon, 22 Sep 2014 11:43:49 -0400\r
+Received: from drake.dyndns.org (navigator.sea-net.de [185.31.6.22])\r
+ (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s8MFhhkC032098\r
+ (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+ Mon, 22 Sep 2014 11:43:48 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1XW5mA-0005SM-3W; Mon, 22 Sep 2014 11:43:42 -0400\r
+From: Austin Clements <amdragon@mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] lib: Simplify close and codify aborting atomic section\r
+Date: Mon, 22 Sep 2014 11:43:35 -0400\r
+Message-Id: <1411400615-20934-1-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFlrOIsWRmVeSWpSXmKPExsUixG6norvNWSHE4P43c4vrN2cyOzB6PFt1\r
+ izmAMYrLJiU1J7MstUjfLoEr4+P1RpaCWVIV2+/NY2xgnCzaxcjJISFgIrHgWgcLhC0mceHe\r
+ erYuRi4OIYHZTBK3JuxhgXA2Mkp8mdzGCOHsYpLYtqmPGcKZyyixYd1tdpB+NgENid+3FjOB\r
+ 2CIC0hI7785m7WLk4GAWUJP406UCEhYWcJdYcngJ2DoWAVWJtYt3grXyCjhIrHjZyQZxhpzE\r
+ ht3/GScw8i5gZFjFKJuSW6Wbm5iZU5yarFucnJiXl1qka66Xm1mil5pSuokRHAYuKjsYmw8p\r
+ HWIU4GBU4uFd0CQfIsSaWFZcmXuIUZKDSUmU976pQogQX1J+SmVGYnFGfFFpTmrxIUYJDmYl\r
+ Ed4gaaAcb0piZVVqUT5MSpqDRUmcd9MPvhAhgfTEktTs1NSC1CKYrAwHh5IE73snoEbBotT0\r
+ 1Iq0zJwShDQTByfIcB6g4YdBaniLCxJzizPTIfKnGHU51nV+62cSYsnLz0uVEuctASkSACnK\r
+ KM2DmwOL31eM4kBvCfM+BaniAcY+3KRXQEuYgJbcPy4PsqQkESEl1cB4uOWDV3DY/itROiaK\r
+ q9geTXM92BRv07vrrYoNy2tZ7wsRXJUrVonyrp/nurut73v8e29T5a9Pb9/JlAw9YbA0+oxE\r
+ a7vd6iXCnzRdJk9Rj5F0c/wxw8Xr5/0Ato2yuRzHGXdk5Pbuk0/hKXx2sXzhi72RlYbvk6fG\r
+ leb9rd06tf5sk4e78R8lluKMREMt5qLiRABec9EnugIAAA==\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: Mon, 22 Sep 2014 15:43:57 -0000\r
+\r
+In Xapian, closing a database implicitly aborts any outstanding\r
+transaction and commits changes. For historical reasons,\r
+notmuch_database_close had grown to almost, but not quite duplicate\r
+this behavior. Before closing the database, it would explicitly (and\r
+unnecessarily) commit it. However, if there was an outstanding\r
+transaction (ie atomic section), commit would throw a Xapian\r
+exception, which notmuch_database_close would unnecessarily print to\r
+stderr, even though notmuch_database_close would ultimately abort the\r
+transaction anyway when it called close.\r
+\r
+This patch simplifies notmuch_database_close to just call\r
+Database::close. This works for both read-only and read/write\r
+databases, takes care of committing changes, unifies the exception\r
+handling path, and codifies aborting outstanding transactions. This\r
+is currently the only way to abort an atomic section (and may remain\r
+so, since it would be difficult to roll back things we may have cached\r
+from rolled-back modifications).\r
+---\r
+ lib/database.cc | 23 +++++++----------------\r
+ lib/notmuch.h | 5 +++++\r
+ 2 files changed, 12 insertions(+), 16 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index a3a7cd3..1f7ff2a 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -903,28 +903,19 @@ notmuch_database_close (notmuch_database_t *notmuch)\r
+ {\r
+ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
+ \r
+- try {\r
+- if (notmuch->xapian_db != NULL &&\r
+- notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)\r
+- (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();\r
+- } catch (const Xapian::Error &error) {\r
+- status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+- if (! notmuch->exception_reported) {\r
+- fprintf (stderr, "Error: A Xapian exception occurred flushing database: %s\n",\r
+- error.get_msg().c_str());\r
+- }\r
+- }\r
+-\r
+ /* Many Xapian objects (and thus notmuch objects) hold references to\r
+ * the database, so merely deleting the database may not suffice to\r
+- * close it. Thus, we explicitly close it here. */\r
++ * close it. Thus, we explicitly close it here. This will\r
++ * implicitly abort any outstanding transaction and commit changes. */\r
+ if (notmuch->xapian_db != NULL) {\r
+ try {\r
+ notmuch->xapian_db->close();\r
+ } catch (const Xapian::Error &error) {\r
+- /* don't clobber previous error status */\r
+- if (status == NOTMUCH_STATUS_SUCCESS)\r
+- status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
++ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
++ if (! notmuch->exception_reported) {\r
++ fprintf (stderr, "Error: A Xapian exception occurred closing database: %s\n",\r
++ error.get_msg().c_str());\r
++ }\r
+ }\r
+ }\r
+ \r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index fe2340b..5c40c67 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -292,6 +292,11 @@ notmuch_database_open (const char *path,\r
+ * notmuch_database_close can be called multiple times. Later calls\r
+ * have no effect.\r
+ *\r
++ * If the caller is currently in an atomic section (there was a\r
++ * notmuch_database_begin_atomic without a matching\r
++ * notmuch_database_end_atomic), this will abort the atomic section,\r
++ * discarding any modifications made in the atomic section.\r
++ *\r
+ * Return value:\r
+ *\r
+ * NOTMUCH_STATUS_SUCCESS: Successfully closed the database.\r
+-- \r
+2.1.0\r
+\r