astahlman / ob-async

Asynchronous src_block execution for org-babel
343 stars 32 forks source link

ob-async does not seem to support ob-ref #73

Closed f4nyc closed 3 years ago

f4nyc commented 3 years ago

Checklist

Expected Behavior

#+NAME: table
#+begin_example
    aaaa
    bbbb
#+end_example

#+begin_src shell   :stdin table :async
    grep a
#+end_src

#+RESULTS:
: aaaa

Actual Behavior

Debugger entered--Lisp error: (error "Reference `table' not found in this buffer")
  signal(error ("Reference `table' not found in this buffer"))
  async-handle-result((lambda (result) (with-current-buffer #<buffer ansible.org> (let ((default-directory "/Users/bytedance/org/")) (save-excursion (cond ((member "none" ...) (message "result silenced")) ((member "silent" ...) (message ...)) (t (goto-char #<marker at 8485 in ansible.org>) (let ... ... ... ...))) (run-hooks 'org-babel-after-execute-hook))))) (async-signal (error "Reference `table' not found in this buffer")) #<buffer *emacs*>)
  async-when-done(#<process emacs> "finished\n")

Troubleshooting Artifacts

So i read the source code of ob-shell to figure out where (error "Reference 'table' not found in this buffer") were produced.

(defun org-babel-execute:shell (body params)
  "Execute a block of Shell commands with Babel.
This function is called by `org-babel-execute-src-block'."
  (let* ((session (org-babel-sh-initiate-session
           (cdr (assq :session params))))
     (stdin (let ((stdin (cdr (assq :stdin params))))
                  (when stdin (org-babel-sh-var-to-string
                               (org-babel-ref-resolve stdin)))))
(defun org-babel-ref-resolve (ref)
  "Resolve the reference REF and return its value."
  (save-window-excursion
    (with-current-buffer (or org-babel-exp-reference-buffer (current-buffer))

The Problem is that org-babel-exp-reference-buffer and (current-buffer) inside async child process are both nil It's pretty odd because ob-async-org-babel-execute-src-block has injected that variable. Then I add some code like below to make sure org-babel-exp-reference-buffer is not nil before calling async-start

(let* ((....)
       (org-babel-exp-reference-buffer (current-buffer))
       (cmd (intern (concat "org-babel-execute:" lang)))
       result)
  ....
    (async-start .... ))

But that's all I got.

error in process sentinel: async-handle-result: Invalid read syntax: "#"
error in process sentinel: Invalid read syntax: "#"

Does anyone know the reason for this error? Apart from saving the buffer to a temporary file, is there a way to inject buffers for async child process?

randomizedthinking commented 3 years ago

I also got the Invalid read syntax: "#" error when using ob-async for shell src blocks. Strangely, it would work in the beginning, then at some points, it stops working. I didn't figure out what conditions triggered the error.

panaak commented 3 years ago

@randomizedthinking I have exactly the same problem. It just stops working after a while.

As mentioned in #75 i'm also using the github version but didn't fix the issue for me.

noorul commented 3 years ago

I am facing this issue

noorul commented 3 years ago

@astahlman Any idea how to fix this?

f4nyc commented 3 years ago

@astahlman Any idea how to fix this?

https://github.com/jwiegley/emacs-async/issues/127 no way to fix it, cause emacs sessions can't share buffers. Saving the buffer to a temporary file is the only way.

f4nyc commented 3 years ago

@astahlman Maybe we could use temporary file to adress this ?

f4nyc commented 3 years ago

@astahlman I just pushed a pr #76 to fix it