--- /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 3D7B9431FDD\r
+ for <notmuch@notmuchmail.org>; Mon, 7 Oct 2013 15:33:47 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 X7UnGBxXchRP for <notmuch@notmuchmail.org>;\r
+ Mon, 7 Oct 2013 15:33:43 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-6.mit.edu (dmz-mailsec-scanner-6.mit.edu\r
+ [18.7.68.35])\r
+ by olra.theworths.org (Postfix) with ESMTP id E6F3A431FC2\r
+ for <notmuch@notmuchmail.org>; Mon, 7 Oct 2013 15:33:32 -0700 (PDT)\r
+X-AuditID: 12074423-b7fc98e0000009a2-02-525336bc1925\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+ by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 45.7B.02466.CB633525; Mon, 7 Oct 2013 18:33:32 -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 r97MXTQ8024748; \r
+ Mon, 7 Oct 2013 18:33:30 -0400\r
+Received: from drake.dyndns.org (26-4-172.dynamic.csail.mit.edu [18.26.4.172])\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 r97MXShI028593\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Mon, 7 Oct 2013 18:33:29 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1VTJMm-0006c9-P5; Mon, 07 Oct 2013 18:33:28 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 11/11] emacs: Fix search tagging races\r
+Date: Mon, 7 Oct 2013 18:33:21 -0400\r
+Message-Id: <1381185201-25197-12-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.8.4.rc3\r
+In-Reply-To: <1381185201-25197-1-git-send-email-amdragon@mit.edu>\r
+References: <1381185201-25197-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrMIsWRmVeSWpSXmKPExsUixG6nrrvHLDjI4PF6WYvrN2cyOzB6PFt1\r
+ izmAMYrLJiU1J7MstUjfLoEr40vrIZaCZ3oVOxu+sTQwPlbrYuTkkBAwkfjy5yErhC0mceHe\r
+ erYuRi4OIYF9jBLn/s9nh3A2MEpM3NvGDOEcZpKY2zGTFcKZyyix++4MZpB+NgENiW37lzOC\r
+ 2CIC0hI7784GKuLgYBZQk/jTpQISFhYwk/j9tAOsnEVAVeLHthNgNq+Ao8T+ZV+hzlCSWHhq\r
+ G5jNCRRfsvEiG4gtJOAgMXf/ItYJjPwLGBlWMcqm5Fbp5iZm5hSnJusWJyfm5aUW6Zrp5WaW\r
+ 6KWmlG5iBIeNi/IOxj8HlQ4xCnAwKvHwZhwNChJiTSwrrsw9xCjJwaQkyvvAODhIiC8pP6Uy\r
+ I7E4I76oNCe1+BCjBAezkgivgBFQjjclsbIqtSgfJiXNwaIkznuLwz5ISCA9sSQ1OzW1ILUI\r
+ JivDwaEkwRtmCtQoWJSanlqRlplTgpBm4uAEGc4DNDwZpIa3uCAxtzgzHSJ/ilFRSpx3NkhC\r
+ ACSRUZoH1wuL61eM4kCvCPNuBaniAaYEuO5XQIOZgAbrsgeCDC5JREhJNTDWbumZ+0Lo4Ryv\r
+ 9XM/PPwyW6X4ZGVuv/qbI96yl6vPhnwUXLVkgefUh1IzCtxDX25yWeC2ZY/HtlRL76QTry5v\r
+ 7H+cvPml97GkiA0/Ll7oWHbnx4ubB1VmMkZ9to5etbdi+0RXc8VFDZ9+5a2LYFdqOWmQ2Lc6\r
+ Xi1586ftH5REbsy08PE4cvzpLSWW4oxEQy3mouJEAJSzu07GAgAA\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, 07 Oct 2013 22:33:47 -0000\r
+\r
+This fixes races in thread-local and global tagging in notmuch-search\r
+(e.g., "+", "-", "a", "*", etc.). Previously, these would modify tags\r
+of new messages that arrived after the search. Now they only operate\r
+on the messages that were in the threads when the search was\r
+performed. This prevents surprises like archiving messages that\r
+arrived in a thread after the search results were shown.\r
+\r
+This eliminates `notmuch-search-find-thread-id-region(-search)'\r
+because these functions strongly encouraged racy usage.\r
+\r
+This fixes the two broken tests added by the previous patch.\r
+---\r
+ devel/TODO | 5 -----\r
+ emacs/notmuch.el | 40 +++++++++++++++++++++++++++-------------\r
+ test/emacs | 4 +---\r
+ 3 files changed, 28 insertions(+), 21 deletions(-)\r
+\r
+diff --git a/devel/TODO b/devel/TODO\r
+index f212483..1cf4089 100644\r
+--- a/devel/TODO\r
++++ b/devel/TODO\r
+@@ -14,11 +14,6 @@ sender's name containing ';' which causes emacs to drop a search\r
+ result.) This may require removing the outer array from the current\r
+ "notmuch search --format=json" results.\r
+ \r
+-Fix '*' to work by simply calling '+' or '-' on a region consisting of\r
+-the entire buffer, (this would avoid one race condition---while still\r
+-leaving other race conditions---but could also potentially make '*' a\r
+-very expensive operation).\r
+-\r
+ Add a global keybinding table for notmuch, and then view-specific\r
+ tables that add to it.\r
+ \r
+diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+index 2c7678d..1c64745 100644\r
+--- a/emacs/notmuch.el\r
++++ b/emacs/notmuch.el\r
+@@ -461,14 +461,25 @@ If BARE is set then do not prefix with \"thread:\""\r
+ (let ((thread (plist-get (notmuch-search-get-result) :thread)))\r
+ (when thread (concat (unless bare "thread:") thread))))\r
+ \r
+-(defun notmuch-search-find-thread-id-region (beg end)\r
+- "Return a list of threads for the current region"\r
+- (mapcar (lambda (thread) (concat "thread:" thread))\r
+- (notmuch-search-properties-in-region :thread beg end)))\r
+-\r
+-(defun notmuch-search-find-thread-id-region-search (beg end)\r
+- "Return a search string for threads for the current region"\r
+- (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or "))\r
++(defun notmuch-search-find-stable-query ()\r
++ "Return the stable queries for the current thread.\r
++\r
++This returns a list (MATCHED-QUERY UNMATCHED-QUERY) for the\r
++matched and unmatched messages in the current thread."\r
++ (plist-get (notmuch-search-get-result) :query))\r
++\r
++(defun notmuch-search-find-stable-query-region (beg end &optional only-matched)\r
++ "Return the stable query for the current region.\r
++\r
++If ONLY-MATCHED is non-nil, include only matched messages. If it\r
++is nil, include both matched and unmatched messages."\r
++ (let ((query-list nil) (all (not only-matched)))\r
++ (dolist (queries (notmuch-search-properties-in-region :query beg end))\r
++ (when (first queries)\r
++ (push (first queries) query-list))\r
++ (when (and all (second queries))\r
++ (push (second queries) query-list)))\r
++ (concat "(" (mapconcat 'identity query-list ") or (") ")")))\r
+ \r
+ (defun notmuch-search-find-authors ()\r
+ "Return the authors for the current thread"\r
+@@ -525,9 +536,12 @@ If BARE is set then do not prefix with \"thread:\""\r
+ (setq output (append output (notmuch-search-get-tags pos)))))\r
+ output))\r
+ \r
+-(defun notmuch-search-tag-region (beg end &optional tag-changes)\r
+- "Change tags for threads in the given region."\r
+- (let ((search-string (notmuch-search-find-thread-id-region-search beg end)))\r
++(defun notmuch-search-tag-region (beg end &optional tag-changes only-matched)\r
++ "Change tags for threads in the given region.\r
++\r
++If ONLY-MATCHED is non-nil, only tag matched messages."\r
++ (let ((search-string (notmuch-search-find-stable-query-region\r
++ beg end only-matched)))\r
+ (setq tag-changes (notmuch-tag search-string tag-changes))\r
+ (notmuch-search-foreach-result beg end\r
+ (lambda (pos)\r
+@@ -796,7 +810,7 @@ non-authors is found, assume that all of the authors match."\r
+ \r
+ See `notmuch-tag' for information on the format of TAG-CHANGES."\r
+ (interactive)\r
+- (apply 'notmuch-tag notmuch-search-query-string tag-changes))\r
++ (notmuch-search-tag-region (point-min) (point-max) tag-changes t))\r
+ \r
+ (defun notmuch-search-buffer-title (query)\r
+ "Returns the title for a buffer with notmuch search results."\r
+@@ -899,7 +913,7 @@ the configured default sort order."\r
+ (save-excursion\r
+ (let ((proc (notmuch-start-notmuch\r
+ "notmuch-search" buffer #'notmuch-search-process-sentinel\r
+- "search" "--format=sexp" "--format-version=1"\r
++ "search" "--format=sexp" "--format-version=2"\r
+ (if oldest-first\r
+ "--sort=oldest-first"\r
+ "--sort=newest-first")\r
+diff --git a/test/emacs b/test/emacs\r
+index e0dcd3c..88eb1e5 100755\r
+--- a/test/emacs\r
++++ b/test/emacs\r
+@@ -889,7 +889,7 @@ $PWD/notmuch_fail exited with status 1 (see *Notmuch errors* for more details)\r
+ ---\r
+ [XXX]\r
+ $PWD/notmuch_fail exited with status 1\r
+-command: $PWD/notmuch_fail search --format\=sexp --format-version\=1 --sort\=newest-first tag\:inbox\r
++command: $PWD/notmuch_fail search --format\=sexp --format-version\=2 --sort\=newest-first tag\:inbox\r
+ exit status: 1"\r
+ \r
+ test_begin_subtest "Search handles subprocess warnings"\r
+@@ -923,7 +923,6 @@ This is a warning\r
+ This is another warning"\r
+ \r
+ test_begin_subtest "Search thread tag operations are race-free"\r
+-test_subtest_known_broken\r
+ add_message '[subject]="Search race test"'\r
+ gen_msg_id_1=$gen_msg_id\r
+ generate_message '[in-reply-to]="<'$gen_msg_id_1'>"' \\r
+@@ -937,7 +936,6 @@ output=$(notmuch search --output=messages 'tag:search-thread-race-tag')\r
+ test_expect_equal "$output" "id:$gen_msg_id_1"\r
+ \r
+ test_begin_subtest "Search global tag operations are race-free"\r
+-test_subtest_known_broken\r
+ generate_message '[in-reply-to]="<'$gen_msg_id_1'>"' \\r
+ '[references]="<'$gen_msg_id_1'>"' \\r
+ '[subject]="Re: Search race test"'\r
+-- \r
+1.8.4.rc3\r
+\r