fix: daemon port conflict handling, multi-port TUI connect

- start-daemon: handle ADDRESS-IN-USE-ERROR by trying ports 9105-9115
  instead of crashing. Logs which port is used.
- Add *daemon-port* defvar to track actual listening port
- main: wrap start-daemon in handler-case so the daemon doesn't
  crash if all ports are in use
- connect-daemon (TUI): try ports 9105-9115 with 2s timeout each
  instead of retrying the same port 3 times
- Add debug messages for connection success and disconnection timestamp
This commit is contained in:
2026-05-15 10:56:09 -04:00
parent 5924994202
commit d14ff3a316
3 changed files with 42 additions and 30 deletions

View File

@@ -756,35 +756,31 @@ supplied (e.g. \"/\"), pre-fill the select filter with it."
** Connection
#+BEGIN_SRC lisp :tangle /home/user/.local/share/passepartout/lisp/channel-tui-main.lisp
(defun connect-daemon (&optional (host "127.0.0.1") (port 9105))
(add-msg :system "* Connecting to daemon... *")
(loop for attempt from 1 to 3
for backoff = 0 then 3
do (sleep backoff)
(handler-case
(let ((s (usocket:socket-connect host port :timeout 5)))
(defun connect-daemon (&optional (host "127.0.0.1") (start-port 9105) (end-port 9115))
"Connect to daemon, trying ports START-PORT to END-PORT."
(add-msg :system (format nil "* Connecting to daemon... *"))
(loop for port from start-port to end-port
do (handler-case
(let ((s (usocket:socket-connect host port :timeout 2)))
(setf (st :stream) (usocket:socket-stream s)
(st :connected) t)
(add-msg :system (format nil "* Connected to daemon on port ~d *" port))
(bt:make-thread (lambda () (reader-loop (st :stream)))
:name "tui-reader")
(return-from connect-daemon t))
(usocket:connection-refused-error (c)
(declare (ignore c))
(when (= attempt 3)
(add-msg :system (format nil "* No daemon on port ~a after ~a attempts *"
port attempt))))
(usocket:connection-refused-error ()
(when (= port end-port)
(add-msg :system (format nil "* No daemon on ports ~d-~d *" start-port end-port))))
(error (c)
(add-msg :system (format nil "* Connection attempt ~a failed: ~a *"
attempt c))
(when (= attempt 3)
(add-msg :system "* TIP: run 'passepartout daemon' first *")))))
(when (= port end-port)
(add-msg :system (format nil "* No daemon on ports ~d-~d (error: ~a) *" start-port end-port c))))))
nil)
(defun disconnect-daemon ()
(when (st :stream)
(ignore-errors (close (st :stream)))
(setf (st :stream) nil (st :connected) nil)
(add-msg :system "* Disconnected *")))
(add-msg :system (format nil "* Disconnected [now=~a] *" (now))))
#+END_SRC
** Main Loop