--- /dev/null
+Return-Path: <aclements@csail.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 1B31A431FC0\r
+ for <notmuch@notmuchmail.org>; Thu, 2 Oct 2014 12:19:21 -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 h5OeBDRD08fY for <notmuch@notmuchmail.org>;\r
+ Thu, 2 Oct 2014 12:19:17 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-5.mit.edu (dmz-mailsec-scanner-5.mit.edu\r
+ [18.7.68.34])\r
+ by olra.theworths.org (Postfix) with ESMTP id 1D9B1431FB6\r
+ for <notmuch@notmuchmail.org>; Thu, 2 Oct 2014 12:19:17 -0700 (PDT)\r
+X-AuditID: 12074422-f79436d000000c21-28-542da534ddd0\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-5.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id AF.23.03105.435AD245; Thu, 2 Oct 2014 15:19:16 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id s92JJEoM022339; \r
+ Thu, 2 Oct 2014 15:19:14 -0400\r
+Received: from drake.dyndns.org (26-4-182.dynamic.csail.mit.edu [18.26.4.182])\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 s92JJBXw006342\r
+ (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+ Thu, 2 Oct 2014 15:19:13 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+ (envelope-from <aclements@csail.mit.edu>)\r
+ id 1XZluB-0000uZ-Kh; Thu, 02 Oct 2014 15:19:11 -0400\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v4] lib: Simplify close and codify aborting atomic section\r
+Date: Thu, 2 Oct 2014 15:19:08 -0400\r
+Message-Id: <1412277548-3445-1-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <87mw9ee0vf.fsf@csail.mit.edu>\r
+References: <87mw9ee0vf.fsf@csail.mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFjrGIsWRmVeSWpSXmKPExsUixG6nrmuyVDfEYHO/msWN1m5Gi+s3ZzJb\r
+ XFlznt2B2ePZqlvMHlsOvWf22P39J0sAcxSXTUpqTmZZapG+XQJXxuXWXywFR5QqHtxex9jA\r
+ +Fi6i5GTQ0LARKJh2WpWCFtM4sK99WwgtpDAbCaJ5b8ruxi5gOwNjBKvJ61ihUgcZpKYuIYT\r
+ IrGEUWLBz+csIAk2AX2JFWsngRWJCEhL7Lw7G8xmFvCWmP11NTuILSzgJbGt7QIziM0ioCpx\r
+ bcsLMJtXwE1i644j7BBXyEls2P2fEcTmFNCWWPN1FjPEYi2J1s4+9gmM/AsYGVYxyqbkVunm\r
+ JmbmFKcm6xYnJ+blpRbpmurlZpbopaaUbmIEB5eL0g7GnweVDjEKcDAq8fBmNOiECLEmlhVX\r
+ 5h5ilORgUhLl/bFQN0SILyk/pTIjsTgjvqg0J7X4EKMEB7OSCK9wN1CONyWxsiq1KB8mJc3B\r
+ oiTOu+kHX4iQQHpiSWp2ampBahFMVoaDQ0mCd/d8oEbBotT01Iq0zJwShDQTByfIcB6g4THT\r
+ QYYXFyTmFmemQ+RPMSpKifNyzgNKCIAkMkrz4Hph0f+KURzoFWHeaJB2HmDigOt+BTSYCWjw\r
+ O3uwwSWJCCmpBkbJ4sTEXucTLmc7J+k+Kyn0r//oucd6Unvrli0am3//X/F4tvhpyZiTcXKZ\r
+ bpHrSq6rrYgW/JuiwuKh0+q2233ZTGvvd7/+/r/xX6wpILom3TL7vsn0cDfvC3Xnvk/MVeQw\r
+ u9oc1N/UdVXhy6qXszccOVijoJjvI2Xmtl59zaJOu84AbcYLE5RYijMSDbWYi4oTAUuMhoPZ AgAA\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: Thu, 02 Oct 2014 19:19:21 -0000\r
+\r
+From: Austin Clements <amdragon@mit.edu>\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 explicitly abort any\r
+outstanding transaction and then just call Database::close. This\r
+works for both read-only and read/write databases, takes care of\r
+committing changes, unifies the exception handling path, and codifies\r
+aborting outstanding transactions. This is currently the only way to\r
+abort an atomic section (and may remain so, since it would be\r
+difficult to roll back things we may have cached from rolled-back\r
+modifications).\r
+---\r
+ lib/database.cc | 32 +++++++++++++++++---------------\r
+ lib/notmuch.h | 9 ++++++++-\r
+ 2 files changed, 25 insertions(+), 16 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index a3a7cd3..a47a71d 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -903,28 +903,30 @@ 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
+ if (notmuch->xapian_db != NULL) {\r
+ try {\r
++ /* If there's an outstanding transaction, it's unclear if\r
++ * closing the Xapian database commits everything up to\r
++ * that transaction, or may discard committed (but\r
++ * unflushed) transactions. To be certain, explicitly\r
++ * cancel any outstanding transaction before closing. */\r
++ if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE &&\r
++ notmuch->atomic_nesting)\r
++ (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))\r
++ ->cancel_transaction ();\r
++\r
++ /* Close the database. This implicitly flushes\r
++ * outstanding changes. */\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..dae0416 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -281,7 +281,7 @@ notmuch_database_open (const char *path,\r
+ notmuch_database_t **database);\r
+ \r
+ /**\r
+- * Close the given notmuch database.\r
++ * Commit changes and close the given notmuch database.\r
+ *\r
+ * After notmuch_database_close has been called, calls to other\r
+ * functions on objects derived from this database may either behave\r
+@@ -292,6 +292,13 @@ notmuch_database_open (const char *path,\r
+ * notmuch_database_close can be called multiple times. Later calls\r
+ * have no effect.\r
+ *\r
++ * For writable databases, notmuch_database_close commits all changes\r
++ * to disk before closing the database. If the caller is currently in\r
++ * an atomic section (there was a notmuch_database_begin_atomic\r
++ * without a matching notmuch_database_end_atomic), this will discard\r
++ * changes made in that atomic section (but still commit changes made\r
++ * prior to entering the atomic section).\r
++ *\r
+ * Return value:\r
+ *\r
+ * NOTMUCH_STATUS_SUCCESS: Successfully closed the database.\r
+-- \r
+2.1.0\r
+\r