GNU bug report logs - #64821
30.0.50; write-region errs when overwriting an already open file

Previous Next

Package: emacs;

Reported by: Ihor Radchenko <yantar92 <at> posteo.net>

Date: Mon, 24 Jul 2023 07:13:02 UTC

Severity: normal

Found in version 30.0.50

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 64821 in the body.
You can then email your comments to 64821 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Mon, 24 Jul 2023 07:13:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ihor Radchenko <yantar92 <at> posteo.net>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Mon, 24 Jul 2023 07:13:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Ihor Radchenko <yantar92 <at> posteo.net>
To: bug-gnu-emacs <at> gnu.org
Subject: 30.0.50; write-region errs when overwriting an already open file
Date: Mon, 24 Jul 2023 07:12:00 +0000
Consider the following reproducer:

1. emacs -Q
2. Evaluate the following in scratch buffer

(with-temp-buffer
  (insert "test")
  (write-region nil nil "/tmp/1.txt"))

3. Open /tmp/1.txt
4. Evaluate the above sexp multiple times
5. Observe

Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  expand-file-name(nil)
  userlock--check-content-unchanged("/tmp/1.txt")
  userlock--ask-user-about-supersession-threat("/tmp/1.txt")
  write-region(nil nil "/tmp/1.txt")
  (progn (insert "test") (write-region nil nil "/tmp/1.txt"))
  (unwind-protect (progn (insert "test") (write-region nil nil "/tmp/1.txt")) (and (buffer-name temp-buffer) (kill-buffer temp-buffer)))
  (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn (insert "test") (write-region nil nil "/tmp/1.txt")) (and (buffer-name temp-buffer) (kill-buffer temp-buffer))))
  (let ((temp-buffer (generate-new-buffer " *temp*" t))) (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn (insert "test") (write-region nil nil "/tmp/1.txt")) (and (buffer-name temp-buffer) (kill-buffer temp-buffer)))))
  (progn (let ((temp-buffer (generate-new-buffer " *temp*" t))) (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn (insert "test") (write-region nil nil "/tmp/1.txt")) (and (buffer-name temp-buffer) (kill-buffer temp-buffer))))))
  elisp--eval-last-sexp(nil)
  eval-last-sexp(nil)
  funcall-interactively(eval-last-sexp nil)
  command-execute(eval-last-sexp)

Expected: No error is thrown.

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.38, cairo version 1.17.8) of 2023-07-21 built on localhost
Repository revision: 7f77120683d6f150e61e11b41d75ba16ee5210a4
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101008
System Description: Gentoo Linux


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
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>




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Mon, 24 Jul 2023 15:12:02 GMT) Full text and rfc822 format available.

Message #8 received at 64821 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Ihor Radchenko <yantar92 <at> posteo.net>
Cc: 64821 <at> debbugs.gnu.org
Subject: Re: bug#64821: 30.0.50;
 write-region errs when overwriting an already open file
Date: Mon, 24 Jul 2023 18:11:30 +0300
> From: Ihor Radchenko <yantar92 <at> posteo.net>
> Date: Mon, 24 Jul 2023 07:12:00 +0000
> 
> 1. emacs -Q
> 2. Evaluate the following in scratch buffer
> 
> (with-temp-buffer
>   (insert "test")
>   (write-region nil nil "/tmp/1.txt"))
> 
> 3. Open /tmp/1.txt
> 4. Evaluate the above sexp multiple times
> 5. Observe
> 
> Debugger entered--Lisp error: (wrong-type-argument stringp nil)
>   expand-file-name(nil)
>   userlock--check-content-unchanged("/tmp/1.txt")
>   userlock--ask-user-about-supersession-threat("/tmp/1.txt")
>   write-region(nil nil "/tmp/1.txt")
>   (progn (insert "test") (write-region nil nil "/tmp/1.txt"))
>   (unwind-protect (progn (insert "test") (write-region nil nil "/tmp/1.txt")) (and (buffer-name temp-buffer) (kill-buffer temp-buffer)))
>   (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn (insert "test") (write-region nil nil "/tmp/1.txt")) (and (buffer-name temp-buffer) (kill-buffer temp-buffer))))
>   (let ((temp-buffer (generate-new-buffer " *temp*" t))) (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn (insert "test") (write-region nil nil "/tmp/1.txt")) (and (buffer-name temp-buffer) (kill-buffer temp-buffer)))))
>   (progn (let ((temp-buffer (generate-new-buffer " *temp*" t))) (save-current-buffer (set-buffer temp-buffer) (unwind-protect (progn (insert "test") (write-region nil nil "/tmp/1.txt")) (and (buffer-name temp-buffer) (kill-buffer temp-buffer))))))
>   elisp--eval-last-sexp(nil)
>   eval-last-sexp(nil)
>   funcall-interactively(eval-last-sexp nil)
>   command-execute(eval-last-sexp)
> 
> Expected: No error is thrown.

Thanks, should be fixed now on the master branch.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Tue, 25 Jul 2023 08:17:02 GMT) Full text and rfc822 format available.

Message #11 received at 64821 <at> debbugs.gnu.org (full text, mbox):

From: Ihor Radchenko <yantar92 <at> posteo.net>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 64821 <at> debbugs.gnu.org
Subject: Re: bug#64821: 30.0.50; write-region errs when overwriting an
 already open file
Date: Tue, 25 Jul 2023 08:16:23 +0000
Eli Zaretskii <eliz <at> gnu.org> writes:

>> Expected: No error is thrown.
>
> Thanks, should be fixed now on the master branch.

I confirm the fix. Thanks!

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
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>




Reply sent to Eli Zaretskii <eliz <at> gnu.org>:
You have taken responsibility. (Tue, 25 Jul 2023 12:19:02 GMT) Full text and rfc822 format available.

Notification sent to Ihor Radchenko <yantar92 <at> posteo.net>:
bug acknowledged by developer. (Tue, 25 Jul 2023 12:19:03 GMT) Full text and rfc822 format available.

Message #16 received at 64821-done <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Ihor Radchenko <yantar92 <at> posteo.net>
Cc: 64821-done <at> debbugs.gnu.org
Subject: Re: bug#64821: 30.0.50; write-region errs when overwriting an
 already open file
Date: Tue, 25 Jul 2023 15:19:13 +0300
> From: Ihor Radchenko <yantar92 <at> posteo.net>
> Cc: 64821 <at> debbugs.gnu.org
> Date: Tue, 25 Jul 2023 08:16:23 +0000
> 
> Eli Zaretskii <eliz <at> gnu.org> writes:
> 
> >> Expected: No error is thrown.
> >
> > Thanks, should be fixed now on the master branch.
> 
> I confirm the fix. Thanks!

Thanks for testing; closing the bug.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Thu, 27 Jul 2023 15:08:01 GMT) Full text and rfc822 format available.

Message #19 received at 64821 <at> debbugs.gnu.org (full text, mbox):

From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 64821 <at> debbugs.gnu.org, Ihor Radchenko <yantar92 <at> gmail.com>
Subject: bug#64821: 30.0.50; write-region errs when overwriting an already
 open file
Date: Thu, 27 Jul 2023 17:07:26 +0200
[Message part 1 (text/plain, inline)]
The change on master (8955853368) causes filelock-tests to fail; see attached log.
This is on macOS with an out-of-tree build.

[filelock-tests.log (application/octet-stream, attachment)]
[Message part 3 (text/plain, inline)]


Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Thu, 27 Jul 2023 16:31:02 GMT) Full text and rfc822 format available.

Message #22 received at 64821 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Mattias Engdegård <mattias.engdegard <at> gmail.com>
Cc: 64821 <at> debbugs.gnu.org, yantar92 <at> gmail.com
Subject: Re: bug#64821: 30.0.50; write-region errs when overwriting an already
 open file
Date: Thu, 27 Jul 2023 19:31:30 +0300
> From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
> Date: Thu, 27 Jul 2023 17:07:26 +0200
> Cc: Ihor Radchenko <yantar92 <at> gmail.com>,
>  64821 <at> debbugs.gnu.org
> 
> The change on master (8955853368) causes filelock-tests to fail; see attached log.
> This is on macOS with an out-of-tree build.

Sorry, I cannot reproduce this on any system to which I have access.
Would you mind running the failing test under Edebug and telling which
part in userlock.el signals an error, and why?  Is that this line:

          (with-current-buffer (get-file-buffer (file-truename filename))
            (set-visited-file-modtime))

If so, does it mean file-truename returns nil?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Thu, 27 Jul 2023 17:48:03 GMT) Full text and rfc822 format available.

Message #25 received at 64821 <at> debbugs.gnu.org (full text, mbox):

From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 64821 <at> debbugs.gnu.org, yantar92 <at> gmail.com
Subject: Re: bug#64821: 30.0.50; write-region errs when overwriting an already
 open file
Date: Thu, 27 Jul 2023 19:47:46 +0200
27 juli 2023 kl. 18.31 skrev Eli Zaretskii <eliz <at> gnu.org>:

> Is that this line:
> 
>          (with-current-buffer (get-file-buffer (file-truename filename))
>            (set-visited-file-modtime))

Right, and it's probably because ERT creates temporary files in /var/something but on macOS, /var is a symlink to /private/var. In the lines above, filename (and file-truename) is the "/private/var/..." version.
Thus get-file-buffer won't return anything because buffer-file-name contains the non-true name, "/var/...".

I have no idea if this is the right solution (probably not) but it makes filelock-tests all pass:

--- a/test/src/filelock-tests.el
+++ b/test/src/filelock-tests.el
@@ -38,8 +38,8 @@ filelock-tests--fixture
 Finally, delete the buffer and the test directory."
   (declare (debug (body)))
   `(ert-with-temp-directory temp-dir
-     (let ((name (concat (file-name-as-directory temp-dir)
-                         "userfile"))
+     (let ((name (file-truename (concat (file-name-as-directory temp-dir)
+                                        "userfile")))
            (create-lockfiles t))
        (with-temp-buffer
          (setq buffer-file-name name





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Thu, 27 Jul 2023 19:03:02 GMT) Full text and rfc822 format available.

Message #28 received at 64821 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Mattias Engdegård <mattias.engdegard <at> gmail.com>
Cc: 64821 <at> debbugs.gnu.org, yantar92 <at> gmail.com
Subject: Re: bug#64821: 30.0.50; write-region errs when overwriting an already
 open file
Date: Thu, 27 Jul 2023 22:02:45 +0300
> From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
> Date: Thu, 27 Jul 2023 19:47:46 +0200
> Cc: yantar92 <at> gmail.com,
>  64821 <at> debbugs.gnu.org
> 
> 27 juli 2023 kl. 18.31 skrev Eli Zaretskii <eliz <at> gnu.org>:
> 
> > Is that this line:
> > 
> >          (with-current-buffer (get-file-buffer (file-truename filename))
> >            (set-visited-file-modtime))
> 
> Right, and it's probably because ERT creates temporary files in /var/something but on macOS, /var is a symlink to /private/var. In the lines above, filename (and file-truename) is the "/private/var/..." version.
> Thus get-file-buffer won't return anything because buffer-file-name contains the non-true name, "/var/...".
> 
> I have no idea if this is the right solution (probably not) but it makes filelock-tests all pass:

Thanks, but I think we should make userlock.el robust in the face of
such applications.  How about the patch below instead?

diff --git a/lisp/userlock.el b/lisp/userlock.el
index 96de17d..92fea11 100644
--- a/lisp/userlock.el
+++ b/lisp/userlock.el
@@ -141,8 +141,10 @@ userlock--check-content-unchanged
           ;; modtime in that buffer, to cater to use case where the
           ;; file is about to be written to from some buffer that
           ;; doesn't visit any file, like a temporary buffer.
-          (with-current-buffer (get-file-buffer (file-truename filename))
-            (set-visited-file-modtime))
+          (let ((buf (get-file-buffer (file-truename filename))))
+            (when buf
+              (with-current-buffer buf
+                (set-visited-file-modtime))))
           'unchanged)))))
 
 ;;;###autoload




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Thu, 27 Jul 2023 19:50:02 GMT) Full text and rfc822 format available.

Message #31 received at 64821 <at> debbugs.gnu.org (full text, mbox):

From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 64821 <at> debbugs.gnu.org, yantar92 <at> gmail.com
Subject: Re: bug#64821: 30.0.50; write-region errs when overwriting an already
 open file
Date: Thu, 27 Jul 2023 21:49:12 +0200
27 juli 2023 kl. 21.02 skrev Eli Zaretskii <eliz <at> gnu.org>:

> Thanks, but I think we should make userlock.el robust in the face of
> such applications.  How about the patch below instead?

That prevent the error but the test then fails since the modtime isn't actually changed.

Maybe you can set temporary-file-directory to a directory name with a symlink component and reproduce the problem on your system?





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Fri, 28 Jul 2023 06:41:01 GMT) Full text and rfc822 format available.

Message #34 received at 64821 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Mattias Engdegård <mattias.engdegard <at> gmail.com>
Cc: 64821 <at> debbugs.gnu.org, yantar92 <at> gmail.com
Subject: Re: bug#64821: 30.0.50; write-region errs when overwriting an already
 open file
Date: Fri, 28 Jul 2023 09:40:44 +0300
> From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
> Date: Thu, 27 Jul 2023 21:49:12 +0200
> Cc: yantar92 <at> gmail.com,
>  64821 <at> debbugs.gnu.org
> 
> 27 juli 2023 kl. 21.02 skrev Eli Zaretskii <eliz <at> gnu.org>:
> 
> > Thanks, but I think we should make userlock.el robust in the face of
> > such applications.  How about the patch below instead?
> 
> That prevent the error but the test then fails since the modtime isn't actually changed.

That's okay: it just means we need to install your suggested patch to
the test as well.  Here, done.  Can you test and see that the test now
succeeds for you?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Fri, 28 Jul 2023 09:02:02 GMT) Full text and rfc822 format available.

Message #37 received at 64821 <at> debbugs.gnu.org (full text, mbox):

From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 64821 <at> debbugs.gnu.org, yantar92 <at> gmail.com
Subject: Re: bug#64821: 30.0.50; write-region errs when overwriting an already
 open file
Date: Fri, 28 Jul 2023 11:01:12 +0200
28 juli 2023 kl. 08.40 skrev Eli Zaretskii <eliz <at> gnu.org>:

> That's okay: it just means we need to install your suggested patch to
> the test as well.  Here, done.  Can you test and see that the test now
> succeeds for you?

Yes, it succeeds. Thank you!





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#64821; Package emacs. (Fri, 28 Jul 2023 12:08:01 GMT) Full text and rfc822 format available.

Message #40 received at 64821 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Mattias Engdegård <mattias.engdegard <at> gmail.com>
Cc: 64821 <at> debbugs.gnu.org, yantar92 <at> gmail.com
Subject: Re: bug#64821: 30.0.50; write-region errs when overwriting an already
 open file
Date: Fri, 28 Jul 2023 15:08:26 +0300
> From: Mattias Engdegård <mattias.engdegard <at> gmail.com>
> Date: Fri, 28 Jul 2023 11:01:12 +0200
> Cc: yantar92 <at> gmail.com,
>  64821 <at> debbugs.gnu.org
> 
> 28 juli 2023 kl. 08.40 skrev Eli Zaretskii <eliz <at> gnu.org>:
> 
> > That's okay: it just means we need to install your suggested patch to
> > the test as well.  Here, done.  Can you test and see that the test now
> > succeeds for you?
> 
> Yes, it succeeds. Thank you!

Great, thanks for testing.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sat, 26 Aug 2023 11:24:07 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 299 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.