org-roam-ui icon indicating copy to clipboard operation
org-roam-ui copied to clipboard

[BUG] desktop.el and org-roam-ui-mode: Cannot bind server socket: Address already in use

Open serycjon opened this issue 2 years ago • 4 comments

Describe the bug When using desktop.el to persist emacs state between sessions, the org-roam-ui-mode causes the loading of the persistent state fail. Each saved buffer has org-roam-ui-mode listed in its minor modes, causing the desktop.el to try to load org-roam-ui-mode for each of them. But all the calls to org-roam-ui-mode (except perhaps for the first one) result in error - "Cannot bind server socket: Address already in use".

To Reproduce

  1. have multiple buffers opened
  2. use org-roam-ui-mode
  3. quit emacs (saves the current session with desktop.el)
  4. start emacs (tries to load the previous session with desktop.el, but fails, because of repeated attempts to start org-roam-ui-mode server)

Additional context Don't known if this is important, but I am using (setq desktop-restore-eager 10), making desktop.el restore 10 buffers immediately and restore the rest of them lazily.

Expected behavior org-roam-ui-mode should fail gratiously (without elisp error, just with a message), when the server socket is already in use. Or maybe even better, it should detect that the socket is actually used by org-roam-ui-mode itself.

** Simple patch for my purposes ** I patched the org-roam-ui-mode to ignore errors. Proper solution would probably still need to let the user know about the issue.

(define-minor-mode
  org-roam-ui-mode
  "Enable org-roam-ui.
This serves the web-build and API over HTTP."
  :lighter " org-roam-ui"
  :global t
  :group 'org-roam-ui
  :init-value nil
  (cond
   (org-roam-ui-mode
   ;;; check if the default keywords actually exist on `orb-preformat-keywords'
   ;;; else add them
    (ignore-errors
      (setq-local httpd-port org-roam-ui-port)
      (setq httpd-root org-roam-ui-app-build-dir)
      (httpd-start)
      (setq org-roam-ui-ws-server
            (websocket-server
             35903
             :host 'local
             :on-open #'org-roam-ui--ws-on-open
             :on-message #'org-roam-ui--ws-on-message
             :on-close #'org-roam-ui--ws-on-close))
      (when org-roam-ui-open-on-start (org-roam-ui-open))))
   (t
    (progn
      (websocket-server-close org-roam-ui-ws-server)
      (httpd-stop)
      (remove-hook 'after-save-hook #'org-roam-ui--on-save)
      (org-roam-ui-follow-mode -1)))))

serycjon avatar Jan 17 '22 15:01 serycjon

Probably even better patch is not to save org-roam-ui-mode and org-roam-ui-follow-mode in desktop.el:

  (add-to-list 'desktop-minor-mode-table
               '(org-roam-ui-mode nil))
  (add-to-list 'desktop-minor-mode-table
               '(org-roam-ui-follow-mode nil))

serycjon avatar Jan 17 '22 16:01 serycjon

Org-roam-ui should make it easy to configure port & host (expose to your local network by binding to 0.0.0.0)

chris-aeviator avatar Jan 25 '22 09:01 chris-aeviator

I don't think that is relevant to this issue. Even if I would set some different port, I believe that the issue is still the same - org-roam-ui tries to bind to that port multiple times, failing after the first try.

serycjon avatar Jan 25 '22 10:01 serycjon

I'm having this issue but also org-roam-ui is not following me despite follow mode being enabled. I disabled desktop saving and having the same issue, both the socket issue and the no-follow issue.

Timmymayes avatar Nov 22 '22 02:11 Timmymayes