Subject: Re: bug#75209: 30.0.93; Emacs reader failed to read data in "/home/nlj/.cache/org-persist/gc-lock.eld"

From: Ihor Radchenko <[email-masked]>
Date: Sat, 15 Mar 2025 12:55:22 +0000

Eli Zaretskii <[email-masked]> writes:

> Could the code which writes the file do it in atomic fashion,
> i.e. write a temporary file, then rename the original file, then move
> the new to the original's name, then delete the original file?
>
> (On Posix filesystems moving a file to the original name is an atomic
> operation, but not on MS-Windows, which is why I suggest a slightly
> more complicated procedure.)

See the attached diff.
It should be applied on top of previous patches (or on top of the latest
Org main).

diff --git a/lisp/org-persist.el b/lisp/org-persist.el
index a639699d93..202c5e645b 100644
--- a/lisp/org-persist.el
+++ b/lisp/org-persist.el
@@ -506,7 +506,12 @@ (defun org-persist--write-elisp-file
             (let ((pp-use-max-width nil)) ; Emacs bug#58687
               (pp data (current-buffer)))
           (prin1 data (current-buffer))))
-      (rename-file tmp-file file 'overwrite)
+      (let ((tmp-file-2 (make-temp-file "org-persist-")))
+        ;; Just renaming may still not be atomic on Windows, so we do
+        ;; a bit more complex juggle.
+        (rename-file file tmp-file-2)
+        (rename-file tmp-file file)
+        (delete-file tmp-file-2))
       (org-persist--display-time
        (- (float-time) start-time)
        "Writing to %S" file))))

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>