From: Christopher Baines <mail@cbaines.net>
To: 67245@debbugs.gnu.org
Cc: "Christopher Baines" <guix@cbaines.net>,
"Josselin Poiret" <dev@jpoiret.xyz>,
"Ludovic Courtès" <ludo@gnu.org>,
"Mathieu Othacehe" <othacehe@gnu.org>,
"Ricardo Wurmus" <rekado@elephly.net>,
"Simon Tournier" <zimon.toutoune@gmail.com>,
"Tobias Geerinckx-Rice" <me@tobias.gr>
Subject: [bug#67245] [PATCH v2] store: Add with-store/non-blocking.
Date: Sat, 11 May 2024 17:53:20 +0100 [thread overview]
Message-ID: <e692ef2de80732b3ed87e95a489f053616f4130a.1715446400.git.mail@cbaines.net> (raw)
In-Reply-To: <460cdfa67b473ea2f1593668b2d9d0fd159378d0.1700244314.git.mail@cbaines.net>
For some applications, it's important to establish a non-blocking connection
rather than just making the socket non-blocking after the connection is
established. This is because there is I/O on the socket that will block during
the handshake.
I've noticed this blocking during the handshake causing issues in the build
coordinator for example.
This commit adds a new with-store variant to avoid changing the behaviour of
with-store/open-connection to ensure that this change can't break anything
that depends on the blocking nature of the socket.
* guix/store.scm (open-unix-domain-socket, open-inet-socket): Take
#:non-blocking? and use SOCK_NONBLOCK when calling socket if appropriate.
(connect-to-daemon, open-connection, call-with-store): Take #:non-blocking?
and pass it on.
(with-store/non-blocking): New syntax rule.
Change-Id: I8225762b78448bc1f7b698c8de5d736e13f577bf
---
guix/store.scm | 53 +++++++++++++++++++++++++++++++++++---------------
1 file changed, 37 insertions(+), 16 deletions(-)
diff --git a/guix/store.scm b/guix/store.scm
index a238cb627a..3e8202a43a 100644
--- a/guix/store.scm
+++ b/guix/store.scm
@@ -106,6 +106,7 @@ (define-module (guix store)
port->connection
close-connection
with-store
+ with-store/non-blocking
set-build-options
set-build-options*
valid-path?
@@ -462,12 +463,15 @@ (define-syntax-rule (system-error-to-connection-error file exp ...)
(file file)
(errno errno))))))))
-(define (open-unix-domain-socket file)
+(define* (open-unix-domain-socket file #:key non-blocking?)
"Connect to the Unix-domain socket at FILE and return it. Raise a
-'&store-connection-error' upon error."
+'&store-connection-error' upon error. If NON-BLOCKING?, make the socket
+non-blocking."
(let ((s (with-fluids ((%default-port-encoding #f))
;; This trick allows use of the `scm_c_read' optimization.
- (socket PF_UNIX (logior SOCK_STREAM SOCK_CLOEXEC) 0)))
+ (socket PF_UNIX
+ (logior SOCK_STREAM SOCK_CLOEXEC SOCK_NONBLOCK)
+ 0)))
(a (make-socket-address PF_UNIX file)))
(system-error-to-connection-error file
@@ -478,9 +482,10 @@ (define %default-guix-port
;; Default port when connecting to a daemon over TCP/IP.
44146)
-(define (open-inet-socket host port)
+(define* (open-inet-socket host port #:key non-blocking?)
"Connect to the Unix-domain socket at HOST:PORT and return it. Raise a
-'&store-connection-error' upon error."
+'&store-connection-error' upon error. If NON-BLOCKING?, make the socket
+non-blocking."
(define addresses
(getaddrinfo host
(if (number? port) (number->string port) port)
@@ -495,7 +500,10 @@ (define (open-inet-socket host port)
((ai rest ...)
(let ((s (socket (addrinfo:fam ai)
;; TCP/IP only
- (logior SOCK_STREAM SOCK_CLOEXEC) IPPROTO_IP)))
+ (if non-blocking?
+ (logior SOCK_STREAM SOCK_CLOEXEC SOCK_NONBLOCK)
+ (logior SOCK_STREAM SOCK_CLOEXEC))
+ IPPROTO_IP)))
(catch 'system-error
(lambda ()
@@ -514,9 +522,10 @@ (define (open-inet-socket host port)
(errno (system-error-errno args)))))
(loop rest)))))))))
-(define (connect-to-daemon uri)
+(define* (connect-to-daemon uri #:key non-blocking?)
"Connect to the daemon at URI, a string that may be an actual URI or a file
-name, and return an input/output port.
+name, and return an input/output port. If NON-BLOCKING?, use a non-blocking
+socket when using the file, unix or guix URI schemes.
This is a low-level procedure that does not perform the initial handshake with
the daemon. Use 'open-connection' for that."
@@ -533,11 +542,13 @@ (define (connect-to-daemon uri)
(match (uri-scheme uri)
((or #f 'file 'unix)
(lambda (_)
- (open-unix-domain-socket (uri-path uri))))
+ (open-unix-domain-socket (uri-path uri)
+ #:non-blocking? non-blocking?)))
('guix
(lambda (_)
(open-inet-socket (uri-host uri)
- (or (uri-port uri) %default-guix-port))))
+ (or (uri-port uri) %default-guix-port)
+ #:non-blocking? non-blocking?)))
((? symbol? scheme)
;; Try to dynamically load a module for SCHEME.
;; XXX: Errors are swallowed.
@@ -557,7 +568,8 @@ (define (connect-to-daemon uri)
(connect uri))
(define* (open-connection #:optional (uri (%daemon-socket-uri))
- #:key port (reserve-space? #t) cpu-affinity)
+ #:key port (reserve-space? #t) cpu-affinity
+ non-blocking?)
"Connect to the daemon at URI (a string), or, if PORT is not #f, use it as
the I/O port over which to communicate to a build daemon.
@@ -565,7 +577,9 @@ (define* (open-connection #:optional (uri (%daemon-socket-uri))
space on the file system so that the garbage collector can still operate,
should the disk become full. When CPU-AFFINITY is true, it must be an integer
corresponding to an OS-level CPU number to which the daemon's worker process
-for this connection will be pinned. Return a server object."
+for this connection will be pinned. If NON-BLOCKING?, use a non-blocking
+socket when using the file, unix or guix URI schemes. Return a server
+object."
(define (handshake-error)
(raise (condition
(&store-connection-error (file (or port uri))
@@ -577,7 +591,8 @@ (define* (open-connection #:optional (uri (%daemon-socket-uri))
;; really a connection error.
(handshake-error)))
(let*-values (((port)
- (or port (connect-to-daemon uri)))
+ (or port (connect-to-daemon
+ uri #:non-blocking? non-blocking?)))
((output flush)
(buffering-output-port port
(make-bytevector 8192))))
@@ -657,9 +672,10 @@ (define (close-connection server)
"Close the connection to SERVER."
(close (store-connection-socket server)))
-(define (call-with-store proc)
- "Call PROC with an open store connection."
- (let ((store (open-connection)))
+(define* (call-with-store proc #:key non-blocking?)
+ "Call PROC with an open store connection. Pass NON-BLOCKING? to
+open-connection."
+ (let ((store (open-connection #:non-blocking? non-blocking?)))
(define (thunk)
(parameterize ((current-store-protocol-version
(store-connection-version store)))
@@ -678,6 +694,11 @@ (define-syntax-rule (with-store store exp ...)
automatically close the store when the dynamic extent of EXP is left."
(call-with-store (lambda (store) exp ...)))
+(define-syntax-rule (with-store/non-blocking store exp ...)
+ "Bind STORE to an non-blocking open connection to the store and evaluate
+EXPs; automatically close the store when the dynamic extent of EXP is left."
+ (call-with-store (lambda (store) exp ...) #:non-blocking? #t))
+
(define current-store-protocol-version
;; Protocol version of the store currently used. XXX: This is a hack to
;; communicate the protocol version to the build output port. It's a hack
base-commit: 9288654773a110156e0bb6fc703a9c24f5bfc527
--
2.41.0
next prev parent reply other threads:[~2024-05-11 16:54 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-17 18:05 [bug#67245] [PATCH] store: Use a non-blocking socket for store connections Christopher Baines
2023-11-26 22:16 ` Ludovic Courtès
2023-11-27 9:48 ` Christopher Baines
2023-11-30 21:11 ` Ludovic Courtès
2024-05-12 17:38 ` Christopher Baines
2024-05-11 16:53 ` Christopher Baines [this message]
2024-05-13 12:44 ` [bug#67245] [PATCH v2] store: Add with-store/non-blocking Ludovic Courtès
2024-05-13 19:32 ` bug#67245: " Christopher Baines
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://guix.gnu.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e692ef2de80732b3ed87e95a489f053616f4130a.1715446400.git.mail@cbaines.net \
--to=mail@cbaines.net \
--cc=67245@debbugs.gnu.org \
--cc=dev@jpoiret.xyz \
--cc=guix@cbaines.net \
--cc=ludo@gnu.org \
--cc=me@tobias.gr \
--cc=othacehe@gnu.org \
--cc=rekado@elephly.net \
--cc=zimon.toutoune@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/guix.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).