From: Jack Kamm <jackkamm@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>, Peter Mao <peter.mao@gmail.com>
Cc: org-mode-email <emacs-orgmode@gnu.org>
Subject: Re: [BUG] ob-python hangs on second start [9.5.4 (release_9.5.4-763-g06373a @ ~/emacs/org-mode/lisp/)]
Date: Sun, 27 Aug 2023 14:26:45 -0700 [thread overview]
Message-ID: <87a5uc9nxm.fsf@gmail.com> (raw)
In-Reply-To: <87ttsnh5bx.fsf@localhost>
[-- Attachment #1: Type: text/plain, Size: 1087 bytes --]
Ihor Radchenko <yantar92@posteo.net> writes:
> Peter Mao <peter.mao@gmail.com> writes:
>
>> Expectation: When running ob-python code blocks, I should be able to
>> kill the python session in the *Python* buffer and run another code
>> block (or the same one).
>>
>> Problem: ob-python works fine on the first execution, but after
>> `exit()`ing the python session, it hangs without executing the code.
>> After a `C-g`, the prompt in the *Python* session shows up, but one then
>> has to re-execute the code block, as none of it has run.
>
> Confirmed.
>
> It looks like `python-shell-first-prompt-hook' does not get triggered
> in the described scenario with exit() and we enter infinite loop in the
> code below.
I think `python-shell-first-prompt-hook' is actually still triggered,
but rather it is `org-babel-comint-wait-for-output' that is hanging
while waiting for startup. However, we can just replace it with a
`sleep-for' instead, because we now wait for
`org-babel-python--initialized' anyways.
See the attached patch. Please let me know if it fixes the problem for
you.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-python-Fix-hanging-on-second-start.patch --]
[-- Type: text/x-patch, Size: 2499 bytes --]
From d5721aa37a399afcd527906e5d9f1b6bce37fdb9 Mon Sep 17 00:00:00 2001
From: Jack Kamm <jackkamm@gmail.com>
Date: Sun, 27 Aug 2023 14:13:15 -0700
Subject: [PATCH] ob-python: Fix hanging on second start
See: https://list.orgmode.org/87ttsnh5bx.fsf@localhost/T/#t
* lisp/ob-python.el (org-babel-python-initiate-session-by-key): Switch
from `org-babel-comint-wait-for-output' to `sleep-for' while waiting
for `org-babel-python--initialized', to prevent hanging on restarted
Python process.
* testing/lisp/test-ob-python.el (test-ob-python/session-restart): New
test for restarting ob-python session process.
---
lisp/ob-python.el | 2 +-
testing/lisp/test-ob-python.el | 19 +++++++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index b9dab91b4..6b216ce89 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -272,7 +272,7 @@ (defun org-babel-python-initiate-session-by-key (&optional session)
;; multiple prompts during initialization.
(with-current-buffer py-buffer
(while (not org-babel-python--initialized)
- (org-babel-comint-wait-for-output py-buffer)))
+ (sleep-for 0 10)))
(setq org-babel-python-buffers
(cons (cons session py-buffer)
(assq-delete-all session org-babel-python-buffers)))
diff --git a/testing/lisp/test-ob-python.el b/testing/lisp/test-ob-python.el
index 82fbca36e..c11e1d0c2 100644
--- a/testing/lisp/test-ob-python.el
+++ b/testing/lisp/test-ob-python.el
@@ -310,6 +310,25 @@ (ert-deftest test-ob-python/async-local-python-shell ()
#+end_src"
(should (org-babel-execute-src-block))))
+(ert-deftest test-ob-python/session-restart ()
+ ;; Disable the test on older Emacs as built-in python.el sometimes
+ ;; fail to initialize session.
+ (skip-unless (version<= "28" emacs-version))
+ (should
+ (equal "success"
+ (progn
+ (org-test-with-temp-text "#+begin_src python :session :results output
+print('start')
+#+end_src"
+ (org-babel-execute-src-block))
+ (let ((proc (python-shell-get-process)))
+ (python-shell-send-string "exit()")
+ (while (accept-process-output proc)))
+ (org-test-with-temp-text "#+begin_src python :session :results output
+print('success')
+#+end_src"
+ (org-babel-execute-src-block))))))
+
(provide 'test-ob-python)
;;; test-ob-python.el ends here
--
2.41.0
next prev parent reply other threads:[~2023-08-27 21:27 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-25 4:44 [BUG] ob-python hangs on second start [9.5.4 (release_9.5.4-763-g06373a @ ~/emacs/org-mode/lisp/)] Peter Mao
2023-08-25 8:52 ` Ihor Radchenko
2023-08-27 21:26 ` Jack Kamm [this message]
2023-08-28 0:29 ` Peter Mao
2023-08-28 9:02 ` Ihor Radchenko
2023-08-31 4:52 ` Jack Kamm
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87a5uc9nxm.fsf@gmail.com \
--to=jackkamm@gmail.com \
--cc=emacs-orgmode@gnu.org \
--cc=peter.mao@gmail.com \
--cc=yantar92@posteo.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.