Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IEX process finishes before executing the entire code-block and emacs seems to hang #1

Open
dhruv-dave opened this issue Apr 4, 2022 · 7 comments

Comments

@dhruv-dave
Copy link

dhruv-dave commented Apr 4, 2022

Hello, I am facing an issue on my laptop where I have a code block I execute with the :session parameter and emacs seems to hang.
I have narrowed it down to what's causing the issue, but I cannot tell why it's causing the issue. Hoping someone can shed some light on this:

OS:

╰─ cat /etc/os-release 
PRETTY_NAME="Ubuntu 21.10"
NAME="Ubuntu"
VERSION_ID="21.10"
VERSION="21.10 (Impish Indri)"
VERSION_CODENAME=impish
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=impish

EMACS Version:

"GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.23, cairo version 1.16.0)
 of 2021-01-18, modified by Debian"

ORG Mode Version:

Org mode version 9.5.2 (9.5.2-gfbff08 @ /home/me/.emacs.d/elpa/org-9.5.2/)

Using edebug i have narrowed what works for me and what doesn't:

-- code-block

#+begin_src elixir :session testing
  1+1
#+end_src

-- what doesn't work:
I execute the source block (from inside the source block I run C-c C-c), and the buffer *testing* gets created and emacs hangs. I can get the control back by pressing C-g C-g.
Here is what I see in the *testing* buffer:

Interactive Elixir (1.13.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> IEx.configure(colors: [enabled: false])
:ok
iex(2)> 
Process testing finished

At this point, the *testing* buffer has no process running in it and I have to kill the buffer.

-- what works
I have narrowed down to the code causing the issue and "fixed" it. The fix is not ideal, but I am new to emacs and I am not sure why this "fixes" the issue. I changed the following lines in ob-elixir.el. With this change, the code block evaluates in the iex session and the results get printed back into the org file.

(defun org-babel-elixir-evaluate-session (session body)
  "Evaluate BODY in SESSION, i.e, the comint buffer."
  (let* ((buffer (format "*%s*" session))
         (send-wait (lambda ()
                      ;; (comint-send-input t nil) ;; this is causing the iex Process to exit early
                      (comint-send-input nil nil)  ;; setting the optional `no-newline` parameter to `nil` "fixes" the issue.
                      (sleep-for 0 5)))
         (insert-line (lambda (line)
                        (insert line "\n")
                        (funcall send-wait)))
         (lines `("IEx.configure(colors: [enabled: false])"
                  ,body
                  "IEx.configure(colors: [enabled: true])"))
         ;; set results (comint output)
         (results (org-babel-comint-with-output (buffer org-babel-elixir-hline t body)
                    (dolist (line lines)
                      (funcall insert-line line)))))
    ;; return results (convert to strings)
    (prin1-to-string (butlast (nthcdr 2 results) 3))))

Here is what I see in the org file:

#+begin_src elixir :session testing
  1+3
#+end_src

#+RESULTS:
| iex(10)> nil |
| iex(11)> 4   |
| iex(12)> nil |

At this point the *testing* buffer is still active, there is a iex process running in it and I can evaluate any previously evaluated code from the org file.

Also I found that in the function comint-send-input, if the no-newline parameter is set to t, then this is what causes the process to exit early:

;; line 1915
;; here, the `no-newline` is set to `t`
(let ((comint-input-sender-no-newline no-newline))
          (funcall comint-input-sender proc input))

Now the RESULTS section is not ideal, and I can see why the no-newline needs to be set to true.
Also, in the interactive elixir (iex) buffer, if i move the cursor to another position and evaluate the source block, the RESULTS section will print nil.

Please let me know if i can be help debug this issue further.

@lambdart
Copy link
Owner

Thanks for the report, I'll take a look ASAP.

@lambdart
Copy link
Owner

Hi @dhruv-dave, how are you?

I fix the problem (hope so), can you test it? :)

@dhruv-dave
Copy link
Author

@lambdart thank you, I can test it out soon and get back to you!

@lambdart
Copy link
Owner

Maybe is necessary to add more filters to the replace-regex function.

@dhruv-dave
Copy link
Author

@lambdart it solves the issue for me.
I do notice that when i execute the code for the very first time, it creates the session (in my case demo) buffer but doesn't populate the #RESULTS block/drawer. The consecutive execution of the source blocks also doesn't populate the #RESULTS drawer. If i follow the following steps, it works like a charm:

  1. I open the buffer called demo
  2. move the cursor to the end of the buffer
  3. Execute the source block and the the #RESULTS block/drawer gets populated

@lambdart
Copy link
Owner

Thanks, I'll take a look on that 👍

@dhruv-dave
Copy link
Author

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants