Subject: bug#31981: Fix 'flymake-proc-legacy-flymake' temporary file deletion bug

From: Joel Rosdahl <[email-masked]>
Date: Tue, 27 Aug 2019 22:35:04 +0200

I can reproduce this bug on latest master.

The problem is that if a check command takes a long time, the
following can happen:

1. The user modifies the buffer, making flymake-proc start a process A
which reads from a temporary file T containing the buffer content.
2. Before process A has finished, the user modifies the buffer again,
making flymake-proc start a new process B reading from the same
temporary file T.
3. A is marked as obsolete.
4. When A's sentinel detects that A has died, it runs A's cleanup
function which deletes T.
5. B may fail since T is gone.

Attached is a patch which solves the issue, at least for me: simply
don't run the cleanup function for an obsolete process. This should
work well for checks that use flymake-proc-simple-cleanup or one of
the other cleanup functions defined in flymake-proc.el.

I'm not sure if the fix is appropriate for the general case with a
user-supplied custom cleanup function, though. But I see no other
quick fix for the issue since the name of the temporary file is kept
in a single buffer-local variable and there is no way for the cleanup
function to know that it's being called for an obsolete process.

-- Joel
From fef0fe5ed6cec78e87f29444af8d400bcbd01e72 Mon Sep 17 00:00:00 2001
From: Joel Rosdahl <[email-masked]>
Date: Tue, 27 Aug 2019 22:00:45 +0200
Subject: [PATCH] Fix flymake-proc temporary file deletion bug

* list/progmodes/flymake-proc.el (flymake-proc--process-sentinel):
Don't run cleanup if this is an obsolete process -- deleting the
temporary file will make the the active process fail since it uses the
same file. (Bug#31981)
---
 lisp/progmodes/flymake-proc.el | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/flymake-proc.el b/lisp/progmodes/flymake-proc.el
index 2d5a47a07978..2095de6117e5 100644
--- a/lisp/progmodes/flymake-proc.el
+++ b/lisp/progmodes/flymake-proc.el
@@ -649,7 +649,12 @@ flymake-proc--process-sentinel
                (flymake-log 3 "Output buffer %s kept alive for debugging"
                             output-buffer))
               (t
-               (when (buffer-live-p source-buffer)
+               (when (and (buffer-live-p source-buffer)
+                          ;; Don't run cleanup if this is an obsolete
+                          ;; process -- deleting the temporary file
+                          ;; will make the the active process fail
+                          ;; since it uses the same file (bug#31981):
+                          (not (process-get proc 'flymake-proc--obsolete)))
                  (with-current-buffer source-buffer
                    (let ((cleanup-f (flymake-proc--get-cleanup-function
                                      (buffer-file-name))))
-- 
2.20.1