Fix debug REPL: dup fd for separate input/output ports, guard TUI startup

ober

cbcc1218f9ef864dd97fdd129639b5f1fb6958e0

diff --git a/lib/jcode/core/debug-repl.sls b/lib/jcode/core/debug-repl.sls
index aee7242..2a863f3 100644
--- a/lib/jcode/core/debug-repl.sls
+++ b/lib/jcode/core/debug-repl.sls
@@ -30,6 +30,7 @@
   (def c-getsockname
        (foreign-procedure "getsockname" (int void* void*) int))
   (def c-fcntl (foreign-procedure "fcntl" (int int int) int))
+  (def c-dup (foreign-procedure "dup" (int) int))
   (def c-errno-location
        (let ([mt (symbol->string (machine-type))])
          (if (and (>= (string-length mt) 3)
@@ -91,20 +92,21 @@
        (when (file-exists? *repl-port-file*)
          (guard (e [#t (void)]) (delete-file *repl-port-file*))))
   (def (client-repl fd)
-       (let ([ip (open-fd-input-port
-                   fd
-                   (buffer-mode block)
-                   (make-transcoder
-                     (utf-8-codec)
-                     (eol-style none)
-                     (error-handling-mode replace)))]
-             [op (open-fd-output-port
-                   fd
-                   (buffer-mode line)
-                   (make-transcoder
-                     (utf-8-codec)
-                     (eol-style lf)
-                     (error-handling-mode replace)))])
+       (let* ([write-fd (c-dup fd)]
+              [ip (open-fd-input-port
+                    fd
+                    (buffer-mode block)
+                    (make-transcoder
+                      (utf-8-codec)
+                      (eol-style none)
+                      (error-handling-mode replace)))]
+              [op (open-fd-output-port
+                    write-fd
+                    (buffer-mode line)
+                    (make-transcoder
+                      (utf-8-codec)
+                      (eol-style lf)
+                      (error-handling-mode replace)))])
          (dynamic-wind
            void
            (lambda ()
diff --git a/lib/jcode/ui/tui.sls b/lib/jcode/ui/tui.sls
index 25bf5c3..e0d7d98 100644
--- a/lib/jcode/ui/tui.sls
+++ b/lib/jcode/ui/tui.sls
@@ -99,7 +99,7 @@
        (- (app-state-width state) (app-state-sidebar-width state)))
   (def (tui-main args) (load-config) (session-init-db)
        (init-tools-for-tui) (apply-tui-overrides! args)
-       (let ([repl-port (start-jcode-repl!)]) (void))
+       (try (start-jcode-repl!) (catch (e) (void)))
        (let ([verbose? (and (member "--verbose" args) #t)])
          (when verbose? (open-tui-log!))
          (tui-log "tui-main: starting, args=~a" args)
diff --git a/src/jcode/core/debug-repl.ss b/src/jcode/core/debug-repl.ss
index 5c936e3..180b0a1 100644
--- a/src/jcode/core/debug-repl.ss
+++ b/src/jcode/core/debug-repl.ss
@@ -40,6 +40,7 @@
 (def c-htons       (foreign-procedure "htons" (unsigned-short) unsigned-short))
 (def c-getsockname (foreign-procedure "getsockname" (int void* void*) int))
 (def c-fcntl       (foreign-procedure "fcntl" (int int int) int))
+(def c-dup         (foreign-procedure "dup" (int) int))
 
 (def c-errno-location
   (let ((mt (symbol->string (machine-type))))
@@ -120,12 +121,14 @@
 ;; ---- Client REPL ----
 
 (def (client-repl fd)
-  (let ((ip (open-fd-input-port fd (buffer-mode block)
-              (make-transcoder (utf-8-codec) (eol-style none)
-                (error-handling-mode replace))))
-        (op (open-fd-output-port fd (buffer-mode line)
-              (make-transcoder (utf-8-codec) (eol-style lf)
-                (error-handling-mode replace)))))
+  ;; dup the fd so input and output ports each own a separate descriptor
+  (let* ((write-fd (c-dup fd))
+         (ip (open-fd-input-port fd (buffer-mode block)
+               (make-transcoder (utf-8-codec) (eol-style none)
+                 (error-handling-mode replace))))
+         (op (open-fd-output-port write-fd (buffer-mode line)
+               (make-transcoder (utf-8-codec) (eol-style lf)
+                 (error-handling-mode replace)))))
     (dynamic-wind
       void
       (lambda ()
diff --git a/src/jcode/ui/tui.ss b/src/jcode/ui/tui.ss
index 763f3b5..cc69212 100644
--- a/src/jcode/ui/tui.ss
+++ b/src/jcode/ui/tui.ss
@@ -178,8 +178,8 @@
   (init-tools-for-tui)
   (apply-tui-overrides! args)
   ;; Start debug REPL (auto-port) — connect with: rlwrap nc 127.0.0.1 $(cat ~/.jcode-repl-port)
-  (let ((repl-port (start-jcode-repl!)))
-    (void))
+  (try (start-jcode-repl!)
+    (catch (e) (void)))
   (let ((verbose? (and (member "--verbose" args) #t)))
     (when verbose? (open-tui-log!))
     (tui-log "tui-main: starting, args=~a" args)