Subject: bug#36340: [PATCH 1/2] bug#36340: Fixes suspendable-ports networking disabled

From: Michael Gran <[email-masked]>
Date: Sun, 9 Mar 2025 16:55:22 -0700

When guile is built with --disable-networking, (ice-9 suspendable-ports)
will attempt to re-export non-existent accept and socket functions.

* module/ice-9/suspendable-ports.scm (accept, connect): set to #f when
  (guile) module does not have accept or connect
  (guile-port-bindings): new variable
  (port-bindings): don't include accept or connect when not defined
---
 module/ice-9/suspendable-ports.scm | 46 ++++++++++++++++++------------
 1 file changed, 27 insertions(+), 19 deletions(-)

diff --git a/module/ice-9/suspendable-ports.scm b/module/ice-9/suspendable-ports.scm
index 9fac1df62..00fd26049 100644
--- a/module/ice-9/suspendable-ports.scm
+++ b/module/ice-9/suspendable-ports.scm
@@ -727,30 +727,38 @@
       (flush-output port))))
 
 (define accept
-  (let ((%accept (@ (guile) accept)))
-    (lambda* (port #:optional (flags 0))
-      (let lp ()
-        (or (%accept port flags)
-            (begin
-              (wait-for-readable port)
-              (lp)))))))
+  (let ((%accept (false-if-exception (@ (guile) accept))))
+    (if %accept
+        (lambda* (port #:optional (flags 0))
+          (let lp ()
+            (or (%accept port flags)
+                (begin
+                  (wait-for-readable port)
+                  (lp)))))
+        #f)))
 
 (define connect
-  (let ((%connect (@ (guile) connect)))
-    (lambda (port sockaddr . args)
-      (unless (apply %connect port sockaddr args)
-        ;; Clownshoes semantics; see connect(2).
-        (wait-for-writable port)
-        (let ((err (getsockopt port SOL_SOCKET SO_ERROR)))
-          (unless (zero? err)
-            (scm-error 'system-error "connect" "~A"
-                       (list (strerror err)) #f)))))))
+  (let ((%connect (false-if-exception (@ (guile) connect))))
+    (if %connect
+        (lambda (port sockaddr . args)
+          (unless (apply %connect port sockaddr args)
+            ;; Clownshoes semantics; see connect(2).
+            (wait-for-writable port)
+            (let ((err (getsockopt port SOL_SOCKET SO_ERROR)))
+              (unless (zero? err)
+                (scm-error 'system-error "connect" "~A"
+                           (list (strerror err)) #f)))))
+        #f)))
 
 (define saved-port-bindings #f)
+(define guile-port-bindings
+  (append
+   '(read-char peek-char force-output close-port)
+   (if accept '(accept) '())
+   (if connect '(connect) '())))
+
 (define port-bindings
-  '(((guile)
-     read-char peek-char force-output close-port
-     accept connect)
+  `(((guile) ,@guile-port-bindings)
     ((ice-9 binary-ports)
      get-u8 lookahead-u8 get-bytevector-n get-bytevector-n!
      get-bytevector-some get-bytevector-some!
-- 
2.48.1