[bug#75588] guix: utils: Delete temporary output files.

Message ID 87wmewdnq1.fsf@fernseed.me
State New
Headers
Series [bug#75588] guix: utils: Delete temporary output files. |

Commit Message

Kierin Bell Jan. 15, 2025, 4:51 p.m. UTC
  * guix/build/utils.scm (call-with-temporary-output-file): Delete and
pass procedure the actual temporary file name, not the template name.

Change-Id: Id8e5da55444195fcee91517cf63ec8ebd20942e5
---

This change will trigger *many* rebuilds, I think because
'call-with-temporary-output-file' is used by 'download-to-store'.

This patch fixes a bug that causes: 1) the procedure passed to
'call-with-temporary-output-file' to always be called with the file name
template string "/guix-file.XXXXXX" rather than the name of the created
temporary file; and 2) the temporary file to persist rather than being
deleted.

Unless I'm missing something, Guix is creating temporary files without
deleting them and nobody is noticing.

Also, more importantly, it's a miracle that nothing has broken because
of issue #1. Apparently, in most applications in the Guix code base, the
procedures passed to this function discard their second argument (the
port) and only use the first argument (what should be the file name).
After a quick search, the only exceptions that I could find are some
unit tests and 'call-with-luks-key-file' in (gnu installer parted). I'm
not sure how this hasn't caused issues there.

I haven't had time to test this yet, because it triggers so many
rebuilds.

 guix/build/utils.scm | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)


base-commit: b696658ee8e0655b17f5d26e024956b5148e36d6
  

Comments

Kierin Bell Jan. 15, 2025, 7:40 p.m. UTC | #1
help-debbugs@gnu.org (GNU bug Tracking System) writes:

So this bug should be canceled! It doesn't actually change the behavior
of the function in any way (though it may making it easier to read for
people not familiar with 'mkstemp!').

I did find a corner-case where temporary files created by
'call-with-temporary-output-file' are not deleted like I'd expect. It
involves using 'execl' to call a process from within
'call-with-temporary-output-file', where the temporary file is not
deleted, at least until after it the new process terminates. Though
confusing, this is probably the right behavior.
  
Ludovic Courtès Jan. 16, 2025, 8:52 a.m. UTC | #2
Hello,

Kierin Bell <fernseed@fernseed.me> skribis:

> This patch fixes a bug that causes: 1) the procedure passed to
> 'call-with-temporary-output-file' to always be called with the file name
> template string "/guix-file.XXXXXX" rather than the name of the created
> temporary file; and 2) the temporary file to persist rather than being
> deleted.
>
> Unless I'm missing something, Guix is creating temporary files without
> deleting them and nobody is noticing.

[…]

> --- a/guix/build/utils.scm
> +++ b/guix/build/utils.scm
> @@ -287,15 +287,16 @@ (define (call-with-temporary-output-file proc)
>  call."
>    (let* ((directory (or (getenv "TMPDIR") "/tmp"))
>           (template  (string-append directory "/guix-file.XXXXXX"))
> -         (out       (mkstemp! template)))
> +         (out       (mkstemp! template))
> +         (filename (port-filename out)))
>      (dynamic-wind
>        (lambda ()
>          #t)
>        (lambda ()
> -        (proc template out))
> +        (proc filename out))
>        (lambda ()
>          (false-if-exception (close out))
> -        (false-if-exception (delete-file template))))))
> +        (false-if-exception (delete-file filename))))))

AFAICS the current code is fine because ‘template’ is modified by
‘mkstemp!’:

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (define template (string-copy "/tmp/guix-example-XXXXXX"))
scheme@(guile-user)> (mkstemp! template)
$15 = #<input-output: /tmp/guix-example-uZ5z4s 13>
scheme@(guile-user)> template
$16 = "/tmp/guix-example-uZ5z4s"
--8<---------------cut here---------------end--------------->8---

All good?

Thanks,
Ludo’.
  
Kierin Bell Jan. 16, 2025, 3:02 p.m. UTC | #3
Hi,

Ludovic Courtès <ludo@gnu.org> writes:

> AFAICS the current code is fine because ‘template’ is modified by
> ‘mkstemp!’:

Yes, the code is fine --- this was a misunderstanding on my part.

I was doing something unusual that was causing temporary files to
persist, and blamed it on 'call-with-temporary-output-file':

--8<---------------cut here---------------start------------->8---
(call-with-temporary-output-file (lambda (fn port) ... (execlp "swaymsg"
"swaymsg" "exec" "--" "touch" "foo")))
--8<---------------cut here---------------end--------------->8---

Sorry for the noise!

Thanks.
  

Patch

diff --git a/guix/build/utils.scm b/guix/build/utils.scm
index 94714bf397..dda8fb9d82 100644
--- a/guix/build/utils.scm
+++ b/guix/build/utils.scm
@@ -287,15 +287,16 @@  (define (call-with-temporary-output-file proc)
 call."
   (let* ((directory (or (getenv "TMPDIR") "/tmp"))
          (template  (string-append directory "/guix-file.XXXXXX"))
-         (out       (mkstemp! template)))
+         (out       (mkstemp! template))
+         (filename (port-filename out)))
     (dynamic-wind
       (lambda ()
         #t)
       (lambda ()
-        (proc template out))
+        (proc filename out))
       (lambda ()
         (false-if-exception (close out))
-        (false-if-exception (delete-file template))))))
+        (false-if-exception (delete-file filename))))))
 
 (define (call-with-ascii-input-file file proc)
   "Open FILE as an ASCII or binary file, and pass the resulting port to