Skip to content

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

@serycjon

Description

@serycjon

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)))))

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions