[bug#77680,3/4] substitutes: Ignore corrupt cached narinfos.

Message ID 2774fbdd96e93995194d5f56bc3d328dc5905326.1744208418.git.ludo@gnu.org
State New
Headers
Series Avoid 'fdatasync' calls for cache files |

Commit Message

Ludovic Courtès April 9, 2025, 2:23 p.m. UTC
  * guix/substitutes.scm (cached-narinfo): Wrap ‘read’ call in
‘false-if-exception’.  Add catch-all ‘match’ clause.

Change-Id: I2d776f64b6521f778b4ab3f956b35cdef2ddb383
---
 guix/substitutes.scm | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
  

Comments

Christopher Baines April 10, 2025, 10:31 a.m. UTC | #1
Ludovic Courtès <ludo@gnu.org> writes:

> * guix/substitutes.scm (cached-narinfo): Wrap ‘read’ call in
> ‘false-if-exception’.  Add catch-all ‘match’ clause.
>
> Change-Id: I2d776f64b6521f778b4ab3f956b35cdef2ddb383
> ---
>  guix/substitutes.scm | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/guix/substitutes.scm b/guix/substitutes.scm
> index 86b9f5472a..45c165e03e 100644
> --- a/guix/substitutes.scm
> +++ b/guix/substitutes.scm
> @@ -300,7 +300,7 @@ (define (cached-narinfo cache-url path)
>      (lambda ()
>        (call-with-input-file cache-file
>          (lambda (p)
> -          (match (read p)
> +          (match (false-if-exception (read p))
>              (('narinfo ('version 2)
>                         ('cache-uri cache-uri)
>                         ('date date) ('ttl ttl) ('value #f))
> @@ -315,7 +315,8 @@ (define (cached-narinfo cache-url path)
>               (if (obsolete? date now ttl)
>                   (values #f #f)
>                   (values #t (string->narinfo value cache-uri))))
> -            (('narinfo ('version v) _ ...)
> +            (_
> +             ;; Corrupt or incompatible cached entry.
>               (values #f #f))))))
>      (lambda _
>        (values #f #f))))

I believe the particular failure case we're introducing here is that the
cache file is empty, so maybe we could match that specific case here and
handle it silently.

I'm a bit wary of handling any and all cache issues silently since that
could make it harder to spot that the caching is entirely broken.
  

Patch

diff --git a/guix/substitutes.scm b/guix/substitutes.scm
index 86b9f5472a..45c165e03e 100644
--- a/guix/substitutes.scm
+++ b/guix/substitutes.scm
@@ -300,7 +300,7 @@  (define (cached-narinfo cache-url path)
     (lambda ()
       (call-with-input-file cache-file
         (lambda (p)
-          (match (read p)
+          (match (false-if-exception (read p))
             (('narinfo ('version 2)
                        ('cache-uri cache-uri)
                        ('date date) ('ttl ttl) ('value #f))
@@ -315,7 +315,8 @@  (define (cached-narinfo cache-url path)
              (if (obsolete? date now ttl)
                  (values #f #f)
                  (values #t (string->narinfo value cache-uri))))
-            (('narinfo ('version v) _ ...)
+            (_
+             ;; Corrupt or incompatible cached entry.
              (values #f #f))))))
     (lambda _
       (values #f #f))))