scripts: substitute: Add back some error handling.

In f50f5751fff4cfc6d5abba9681054569694b7a5c, the way fetch was called within
process-substitution was changed.  As call-with-cached-connection actually
includes important error handling for the opening of a HTTP request, this
change removed some error handling.  This commit adds that back.

Fixes <https://bugs.gnu.org/47157>.

* guix/scripts/substitute.scm (call-with-cached-connection): New procedure.
(with-cached-connection): New syntax rule.
(process-substitution): Retry once for some errors when making HTTP requests
to fetch substitutes.
This commit is contained in:
Christopher Baines 2021-03-15 15:05:08 +00:00
parent 24f1ce9e71
commit c37e3b92ad
No known key found for this signature in database
GPG Key ID: 5E28A33B0B84F577

@ -45,6 +45,7 @@
#:select (uri-abbreviation nar-uri-abbreviation #:select (uri-abbreviation nar-uri-abbreviation
(open-connection-for-uri (open-connection-for-uri
. guix:open-connection-for-uri))) . guix:open-connection-for-uri)))
#:autoload (gnutls) (error/invalid-session)
#:use-module (guix progress) #:use-module (guix progress)
#:use-module ((guix build syscalls) #:use-module ((guix build syscalls)
#:select (set-thread-name)) #:select (set-thread-name))
@ -377,6 +378,32 @@ server certificates."
(drain-input socket) (drain-input socket)
socket)))))))) socket))))))))
(define (call-with-cached-connection uri proc)
(let ((port (open-connection-for-uri/cached uri
#:verify-certificate? #f)))
(catch #t
(lambda ()
(proc port))
(lambda (key . args)
;; If PORT was cached and the server closed the connection in the
;; meantime, we get EPIPE. In that case, open a fresh connection
;; and retry. We might also get 'bad-response or a similar
;; exception from (web response) later on, once we've sent the
;; request, or a ERROR/INVALID-SESSION from GnuTLS.
(if (or (and (eq? key 'system-error)
(= EPIPE (system-error-errno `(,key ,@args))))
(and (eq? key 'gnutls-error)
(eq? (first args) error/invalid-session))
(memq key '(bad-response bad-header bad-header-component)))
(proc (open-connection-for-uri/cached uri
#:verify-certificate? #f
#:fresh? #t))
(apply throw key args))))))
(define-syntax-rule (with-cached-connection uri port exp ...)
"Bind PORT with EXP... to a socket connected to URI."
(call-with-cached-connection uri (lambda (port) exp ...)))
(define* (process-substitution store-item destination (define* (process-substitution store-item destination
#:key cache-urls acl #:key cache-urls acl
deduplicate? print-build-trace?) deduplicate? print-build-trace?)
@ -424,11 +451,11 @@ the current output port."
(call-with-connection-error-handling (call-with-connection-error-handling
uri uri
(lambda () (lambda ()
(http-fetch uri #:text? #f (with-cached-connection uri port
#:open-connection open-connection-for-uri/cached (http-fetch uri #:text? #f
#:keep-alive? #t #:port port
#:buffered? #f #:keep-alive? #t
#:verify-certificate? #f)))))) #:buffered? #f)))))))
(else (else
(leave (G_ "unsupported substitute URI scheme: ~a~%") (leave (G_ "unsupported substitute URI scheme: ~a~%")
(uri->string uri))))) (uri->string uri)))))
@ -715,6 +742,8 @@ if needed, as expected by the daemon's agent."
;;; Local Variables: ;;; Local Variables:
;;; eval: (put 'with-timeout 'scheme-indent-function 1) ;;; eval: (put 'with-timeout 'scheme-indent-function 1)
;;; eval: (put 'with-redirected-error-port 'scheme-indent-function 0) ;;; eval: (put 'with-redirected-error-port 'scheme-indent-function 0)
;;; eval: (put 'with-cached-connection 'scheme-indent-function 2)
;;; eval: (put 'call-with-cached-connection 'scheme-indent-function 1)
;;; End: ;;; End:
;;; substitute.scm ends here ;;; substitute.scm ends here