Org-roam fails with "Selecting deleted buffer" error

emacs won’t load my init.el due to an error loading org-roam, though it worked a couple of days ago. I just did a system update, perhaps that’s what triggered it?

Debugger entered--Lisp error: (error "Selecting deleted buffer")
  set-buffer(#<killed buffer>)
  (save-current-buffer (set-buffer (emacsql-buffer connection)) (and (>= (buffer-size) 2) (string= "#\n" (buffer-substring (- (point-max) 2) (point-max)))))
  (progn (save-current-buffer (set-buffer (emacsql-buffer connection)) (and (>= (buffer-size) 2) (string= "#\n" (buffer-substring (- (point-max) 2) (point-max))))))
  (closure (t) (connection) "Return true if the end of the buffer has a properl..." (progn (save-current-buffer (set-buffer (emacsql-buffer connection)) (and (>= (buffer-size) 2) (string= "#\n" (buffer-substring (- ... 2) (point-max)))))))(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10>)
  apply((closure (t) (connection) "Return true if the end of the buffer has a properl..." (progn (save-current-buffer (set-buffer (emacsql-buffer connection)) (and (>= (buffer-size) 2) (string= "#\n" (buffer-substring (- ... 2) (point-max))))))) #<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> nil)
  emacsql-waiting-p(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10>)
  (not (emacsql-waiting-p connection))
  (and (or (null real-timeout) (< (float-time) end)) (not (emacsql-waiting-p connection)))
  (while (and (or (null real-timeout) (< (float-time) end)) (not (emacsql-waiting-p connection))) (let ((saved-match-data (match-data))) (unwind-protect (progn (accept-process-output (emacsql-process connection) real-timeout)) (set-match-data saved-match-data t))))
  (let* ((real-timeout (or timeout emacsql-global-timeout)) (end (if real-timeout (progn (+ (float-time) real-timeout))))) (while (and (or (null real-timeout) (< (float-time) end)) (not (emacsql-waiting-p connection))) (let ((saved-match-data (match-data))) (unwind-protect (progn (accept-process-output (emacsql-process connection) real-timeout)) (set-match-data saved-match-data t)))) (if (emacsql-waiting-p connection) nil (signal 'emacsql-timeout (list "Query timed out" real-timeout))))
  (progn (let* ((real-timeout (or timeout emacsql-global-timeout)) (end (if real-timeout (progn (+ (float-time) real-timeout))))) (while (and (or (null real-timeout) (< (float-time) end)) (not (emacsql-waiting-p connection))) (let ((saved-match-data (match-data))) (unwind-protect (progn (accept-process-output (emacsql-process connection) real-timeout)) (set-match-data saved-match-data t)))) (if (emacsql-waiting-p connection) nil (signal 'emacsql-timeout (list "Query timed out" real-timeout)))))
  (closure (t) (connection &optional timeout) "Block until CONNECTION is waiting for further inpu..." (progn (let* ((real-timeout (or timeout emacsql-global-timeout)) (end (if real-timeout (progn ...)))) (while (and (or (null real-timeout) (< ... end)) (not (emacsql-waiting-p connection))) (let ((saved-match-data ...)) (unwind-protect (progn ...) (set-match-data saved-match-data t)))) (if (emacsql-waiting-p connection) nil (signal 'emacsql-timeout (list "Query timed out" real-timeout))))))(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10>)
  apply((closure (t) (connection &optional timeout) "Block until CONNECTION is waiting for further inpu..." (progn (let* ((real-timeout (or timeout emacsql-global-timeout)) (end (if real-timeout (progn ...)))) (while (and (or (null real-timeout) (< ... end)) (not (emacsql-waiting-p connection))) (let ((saved-match-data ...)) (unwind-protect (progn ...) (set-match-data saved-match-data t)))) (if (emacsql-waiting-p connection) nil (signal 'emacsql-timeout (list "Query timed out" real-timeout)))))) #<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> nil)
  emacsql-wait(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10>)
  (let ((sql-string (apply #'emacsql-compile connection sql args))) (emacsql-clear connection) (emacsql-send-message connection sql-string) (emacsql-wait connection) (emacsql-parse connection))
  (progn (let ((sql-string (apply #'emacsql-compile connection sql args))) (emacsql-clear connection) (emacsql-send-message connection sql-string) (emacsql-wait connection) (emacsql-parse connection)))
  (closure (t) (connection sql &rest args) "\n\n(fn CONNECTION SQL &rest ARGS)" (progn (let ((sql-string (apply #'emacsql-compile connection sql args))) (emacsql-clear connection) (emacsql-send-message connection sql-string) (emacsql-wait connection) (emacsql-parse connection))))(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> [:pragma (= busy-timeout $s1)] 15000)
  apply((closure (t) (connection sql &rest args) "\n\n(fn CONNECTION SQL &rest ARGS)" (progn (let ((sql-string (apply #'emacsql-compile connection sql args))) (emacsql-clear connection) (emacsql-send-message connection sql-string) (emacsql-wait connection) (emacsql-parse connection)))) #<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> ([:pragma (= busy-timeout $s1)] 15000))
  emacsql(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> [:pragma (= busy-timeout $s1)] 15000)
  (let* ((process-connection-type nil) (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) (file (slot-value connection 'file)) (buffer (generate-new-buffer " *emacsql-sqlite*")) (fullfile (if file (expand-file-name file) ":memory:")) (process (start-process "emacsql-sqlite" buffer emacsql-sqlite-executable fullfile))) (let* ((v connection)) (eieio-oset v 'process process)) (let* ((v process)) (set-process-sentinel v #'(lambda (proc _) (kill-buffer (process-buffer proc))))) (emacsql-wait connection) (emacsql connection [:pragma (= busy-timeout $s1)] (/ (* emacsql-global-timeout 1000) 2)) (emacsql-register connection))
  (progn (emacsql-sqlite-ensure-binary) (let* ((process-connection-type nil) (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) (file (slot-value connection 'file)) (buffer (generate-new-buffer " *emacsql-sqlite*")) (fullfile (if file (expand-file-name file) ":memory:")) (process (start-process "emacsql-sqlite" buffer emacsql-sqlite-executable fullfile))) (let* ((v connection)) (eieio-oset v 'process process)) (let* ((v process)) (set-process-sentinel v #'(lambda (proc _) (kill-buffer (process-buffer proc))))) (emacsql-wait connection) (emacsql connection [:pragma (= busy-timeout $s1)] (/ (* emacsql-global-timeout 1000) 2)) (emacsql-register connection)))
  (progn (progn (emacsql-sqlite-ensure-binary) (let* ((process-connection-type nil) (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) (file (slot-value connection 'file)) (buffer (generate-new-buffer " *emacsql-sqlite*")) (fullfile (if file (expand-file-name file) ":memory:")) (process (start-process "emacsql-sqlite" buffer emacsql-sqlite-executable fullfile))) (let* ((v connection)) (eieio-oset v 'process process)) (let* ((v process)) (set-process-sentinel v #'(lambda (proc _) (kill-buffer ...)))) (emacsql-wait connection) (emacsql connection [:pragma (= busy-timeout $s1)] (/ (* emacsql-global-timeout 1000) 2)) (emacsql-register connection))))
  (closure (t) (connection &rest _rest) "\n\n(fn CONNECTION &rest REST)" (progn (progn (emacsql-sqlite-ensure-binary) (let* ((process-connection-type nil) (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) (file (slot-value connection ...)) (buffer (generate-new-buffer " *emacsql-sqlite*")) (fullfile (if file ... ":memory:")) (process (start-process "emacsql-sqlite" buffer emacsql-sqlite-executable fullfile))) (let* ((v connection)) (eieio-oset v 'process process)) (let* ((v process)) (set-process-sentinel v #'...)) (emacsql-wait connection) (emacsql connection [:pragma (= busy-timeout $s1)] (/ (* emacsql-global-timeout 1000) 2)) (emacsql-register connection)))))(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> (:file "~/.emacs.d/org-roam.db"))
  apply((closure (t) (connection &rest _rest) "\n\n(fn CONNECTION &rest REST)" (progn (progn (emacsql-sqlite-ensure-binary) (let* ((process-connection-type nil) (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) (file (slot-value connection ...)) (buffer (generate-new-buffer " *emacsql-sqlite*")) (fullfile (if file ... ":memory:")) (process (start-process "emacsql-sqlite" buffer emacsql-sqlite-executable fullfile))) (let* ((v connection)) (eieio-oset v 'process process)) (let* ((v process)) (set-process-sentinel v #'...)) (emacsql-wait connection) (emacsql connection [:pragma (= busy-timeout $s1)] (/ (* emacsql-global-timeout 1000) 2)) (emacsql-register connection))))) (#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> (:file "~/.emacs.d/org-roam.db")))
  #f(compiled-function (&rest args) #<bytecode -0x1a921b7df21097a0>)(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> (:file "~/.emacs.d/org-roam.db"))
  apply(#f(compiled-function (&rest args) #<bytecode -0x1a921b7df21097a0>) #<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> (:file "~/.emacs.d/org-roam.db"))
  initialize-instance(#<emacsql-sqlite-connection emacsql-sqlite-connection-1559bf72ba10> (:file "~/.emacs.d/org-roam.db"))
  #f(compiled-function (class &rest slots) "Default constructor for CLASS `eieio-default-superclass'.\nSLOTS are the initialization slots used by `initialize-instance'.\nThis static method is called when an object is constructed.\nIt allocates the vector used to represent an EIEIO object, and then\ncalls `initialize-instance' on that object." #<bytecode -0xadfb7d5e44f8b22>)(emacsql-sqlite-connection :file "~/.emacs.d/org-roam.db")
  apply(#f(compiled-function (class &rest slots) "Default constructor for CLASS `eieio-default-superclass'.\nSLOTS are the initialization slots used by `initialize-instance'.\nThis static method is called when an object is constructed.\nIt allocates the vector used to represent an EIEIO object, and then\ncalls `initialize-instance' on that object." #<bytecode -0xadfb7d5e44f8b22>) emacsql-sqlite-connection (:file "~/.emacs.d/org-roam.db"))
  make-instance(emacsql-sqlite-connection :file "~/.emacs.d/org-roam.db")
  (let ((connection (make-instance 'emacsql-sqlite-connection :file file))) (if debug (progn (emacsql-enable-debugging connection))) connection)
  (progn (let ((--cl-keys-- --cl-rest--)) (while --cl-keys-- (cond ((memq (car --cl-keys--) '(:debug :allow-other-keys)) (if (cdr --cl-keys--) nil (error "Missing argument for %s" (car --cl-keys--))) (setq --cl-keys-- (cdr (cdr --cl-keys--)))) ((car (cdr (memq ... --cl-rest--))) (setq --cl-keys-- nil)) (t (error "Keyword argument %s not one of (:debug)" (car --cl-keys--)))))) (let ((connection (make-instance 'emacsql-sqlite-connection :file file))) (if debug (progn (emacsql-enable-debugging connection))) connection))
  (let* ((debug (car (cdr (plist-member --cl-rest-- ':debug))))) (progn (let ((--cl-keys-- --cl-rest--)) (while --cl-keys-- (cond ((memq (car --cl-keys--) '...) (if (cdr --cl-keys--) nil (error "Missing argument for %s" ...)) (setq --cl-keys-- (cdr ...))) ((car (cdr ...)) (setq --cl-keys-- nil)) (t (error "Keyword argument %s not one of (:debug)" (car --cl-keys--)))))) (let ((connection (make-instance 'emacsql-sqlite-connection :file file))) (if debug (progn (emacsql-enable-debugging connection))) connection)))
  emacsql-sqlite("~/.emacs.d/org-roam.db")
  org-roam-db()
  org-roam-db-sync()
  org-roam-db-autosync-mode()
  eval-buffer(#<buffer  *load*> nil "/home/rhombus/.emacs.d/init.el" nil t)  ; Reading at buffer position 2060
  load-with-code-conversion("/home/rhombus/.emacs.d/init.el" "/home/rhombus/.emacs.d/init.el" t t)
  load("/home/rhombus/.emacs.d/init" noerror nomessage)
  startup--load-user-init-file(#f(compiled-function () #<bytecode 0x1eb2de4572631591>) #f(compiled-function () #<bytecode -0x1f3c61addc0b8a75>) t)
  command-line()
  normal-top-level()

None of the standard bits of advice I’ve tried have worked:

Deleting org-roam.db and running org-roam-db-sync fails with the same “Selected deleted buffer” error.

I have updated emacsql, no change.

I have manually built emacsql with make, no change.

Does anyone have any other suggestions? Any advice to help me interpret this error message?

I’d upgrade Emacs to 29.2 and use the bulit-in sqlite option. Have you done this?

No, not consciously. I’ll try that at the next opportunity.

Okay, I’ve installed 29.2 with sqlite, but I’m still getting the same error. Do I need to configure anything to make it work?

I had an obsolete version of emacsql (3.0.0) installed. I’ve removed it now, but it doesn’t make any difference, the error persists.

I thought perhaps I need to uninstall the emacsql-sqlite 3.1.1 package, but the package manager refuses because it’s an org-roam dependency.

Now I’ve tried removing and reinstalling org-roam. It installs emacsql-3.1.1 automatically. Now it compiles the EmacSQL binary upon installing (it did not do that previously, I had to manually build it) but this also made no difference - the error persists.

I don’t understand what is wrong here. If I did, I could investigate more effectively.

Apparently there is supposed to be an emacsql-sqlite-builtin package in melpa, but I don’t see it in my package listing:

It is neither in the full package listing, nor in built-in when I search using paradox.

Okay, I think it is in melpa-preview, and I am only attaching melpa-stable.

Success!

Background: to use the built-in sqlite in emacs 29.2, you need the emacsql-sqlite-builtin package. This is not pulled automatically by org-roam. The latest stable org-roam (2.2.2) also does not seem to be aware of this.

Solution:

Add the melpa repository with this line in init.el:

(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)

Here’s what my packages section looks like now:


;; Packages
(require 'package)
(add-to-list 'package-archives '("elpa" .  "https://elpa.gnu.org/packages/" ) t)
(add-to-list 'package-archives '("melpa-stable" . "http://stable.melpa.org/packages/") t)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)

After doing this, you need to reload init.el and then get the updated package list with M-x package-list-packages.

Now install org-roam from melpa, not melpa-stable. My version is now 20240114.1941.

This will also pull emacsql, but not emacsql-sqlite-builtin. You need this for it to work with the built-in sqlite. Install it. My version is 20240119.2314.

After restarting emacs, the org-roam database is automatically rebuilt, and now it works again!

@nobiot When is the next stable release of org-roam planned? I would prefer to remain on stable releases whenever possible.

1 Like

I do not know. I am not part of the project; just like you. Congratulations on your success :slight_smile:

Hey, thanks for submitting your solution. I did exactly the same but I still see the warning each time I open emacs.

This is how my init.el looks like for packages:

(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(add-to-list 'package-archives '("gnu" . "https://elpa.gnu.org/packages/"))
(package-initialize) ;; Initializes the package infrastructure

And this is what I added for roam:

(use-package org-roam
      :ensure t
      :init
      (setq org-roam-v2-ack t)
      :hook
      (after-init . org-roam-mode)
      :custom
      (org-roam-directory "~/xxx/org/roam")
      :bind (:map org-roam-mode-map
              (("C-c n l" . org-roam)
               ("C-c n f" . org-roam-find-file)
               ("C-c n g" . org-roam-graph))
              :map org-mode-map
              (("C-c n i" . org-roam-insert))
              (("C-c n I" . org-roam-insert-immediate))))

The first step I did was commenting out the :ensure for the use-package org-roam, then I removed org-roam and resintall it using the packages app. I selected the most recent version, but still the error appeared after the reinstallation.

Thanks!

Update: I had to manually uninstall emacsql-sqlite and then reinstall roam … all seems to be working fine now. I wonder if magit is OK. I’ll know next time I need to do a commit :slight_smile: