From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:306:2d92::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id 6PzMINO/62SYgQAAauVa8A:P1 (envelope-from ) for ; Sun, 27 Aug 2023 23:27:47 +0200 Received: from aspmx1.migadu.com ([2001:41d0:306:2d92::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id 6PzMINO/62SYgQAAauVa8A (envelope-from ) for ; Sun, 27 Aug 2023 23:27:47 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id F061834414 for ; Sun, 27 Aug 2023 23:27:46 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=JRhdjSk6; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1693171667; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=TS0sIRAmS8DzuUAfjzpDB9wEgWRmHWLyFcv7ZEVJIG0=; b=Y/VZg/auqWvMFoPIhP1J0AlouZHnAsZBXKD/DkHGVTmndyTC/C0NiuYpnZpfZWBdilWTGh gCFIJemCdf0/vFdyx1hVGNTCSEp5O/PtEPElYStuOP/MTyJEOP3iqXfKI+JpbtipYhKjxU Hj6lvproeTUsUavqB2CGUx+M3skHs5f4o6mRg8PLd5Z1HVCxDulsN7Yl7j/zvyZrhnA9u/ hVN1YRz2ygO/zXC7FmjhH19S6XLjP9QYoxzJbCzv8U+RCdDBHB/b++AWUzqBGFKGqcD6wi agmx0GRSw317h5BtP2toFFmYS+M09ulVi5o+B4IFJfjQmzshk6LndmHgnuqQdw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=JRhdjSk6; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=key1; d=yhetil.org; t=1693171667; a=rsa-sha256; cv=none; b=f9mhIyznhOV7PTASSBd+RGXRUCenXjIGokf0tMEK17KqdMrJa5LmAdCfIt0ZSwI47cU0Ei 6WjUQxyeZFBu8Ka+hsl163+kfMmH0J5tloWUsMjt795uNP+AbQy0CoP8zf59WQP+yiV78Z OvL4oFakbl6HaoBFJ3eDCNLanztZrBYGUuSx9IwP6R92AYVnDBU43sf9gkdn64wOo9cdxj iGA7mFlgtLqyM59OiWQnFn+xcQUNIP09LOePv5mr3ql5APnl2nX8q8E9pvtSd2UI+mfnkj 92dAr9v0AVtHm1/4neynS5Lu/oE60aMgvj2ByKNMHS0ZenJfewzG03dHxCE2zg== Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qaNHn-0007N5-Jm; Sun, 27 Aug 2023 17:27:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qaNHl-0007Ma-He for emacs-orgmode@gnu.org; Sun, 27 Aug 2023 17:27:05 -0400 Received: from mail-il1-x130.google.com ([2607:f8b0:4864:20::130]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qaNHU-0000v1-LZ for emacs-orgmode@gnu.org; Sun, 27 Aug 2023 17:26:51 -0400 Received: by mail-il1-x130.google.com with SMTP id e9e14a558f8ab-34ccc0fca24so9939785ab.0 for ; Sun, 27 Aug 2023 14:26:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693171607; x=1693776407; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=TS0sIRAmS8DzuUAfjzpDB9wEgWRmHWLyFcv7ZEVJIG0=; b=JRhdjSk6svTEV0MEfrngtxn7uVc+0Npy+CRF1ZeBKz5XcY9dou8xGApixfbBhEKh1P vx1sBQdcsufb+iW0YTTjd0avWlQlgTQ4OX50Dk2eFvCVECQ91qP6CjQeto7YQfu/+7AC O/3PjEMOfAJX/c4xHhGTld7LnBGHwLa7twnr81IsgaVk4/UiLQZOgbkjs8b0s2ixs4rT FBipgGgPzR9gLhUD1cP5mtZclDlNNdWniHPft+LDR72v65MsyKjveTdf8edoHUDUIVpW +CinnlNO4ipGns4Xg4K7Yhwd3Ydb77cW1vdcPHF8KiUHD718wpeSWlIHrcM9zXp2sfyj 8DPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693171607; x=1693776407; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TS0sIRAmS8DzuUAfjzpDB9wEgWRmHWLyFcv7ZEVJIG0=; b=K/E0A7EwX8mmNuHt5Vry+sbsVmBsN1Kc/T06cbyycZ21D2aJPszdECXpY+kIkLknQN D6Wn1xwRRW8myxQK+ai9HkAhw8//eW+pzLtH08Vr7KBK0reMbNXpb2a1gjcKYcAKd8q0 iHI084Fc4q1uMZ7YVtaaur5TMHVE6+dBluhx6Vmabxc7ain49P0BRhvsykRF6w0mODlk 2OZlPu0bD/bop2WgexGU0sdNXLtoIU+Ch/i0eS1kzlfnjHgJy+890Kgu8UGRcPWT97/u cnwum9zoOxs0iRYH6v5TkhPcjrC79faj4QFVyl/bc/KMddT2OHaUeEXo1dYCAn/oV3K/ F/pg== X-Gm-Message-State: AOJu0YzBEbfgiPT9oThDZmeA5SXdv/Dh22/dUhzG2HMAz/nIz9YtveHG 8681U3RK6ZWJ7PJqY20fpKc= X-Google-Smtp-Source: AGHT+IEXLRsyiAxfg7B9UK0gUqE0c7Ei2/OFu3OQOUunp3VPZTRKsGXnD9CB8e5uBH95oq7hj7HZPA== X-Received: by 2002:a05:6e02:1a4f:b0:34a:c751:28ca with SMTP id u15-20020a056e021a4f00b0034ac75128camr18430329ilv.15.1693171607277; Sun, 27 Aug 2023 14:26:47 -0700 (PDT) Received: from localhost (157-131-78-143.fiber.dynamic.sonic.net. [157.131.78.143]) by smtp.gmail.com with ESMTPSA id t7-20020a63b707000000b00553d42a7cb5sm5238876pgf.68.2023.08.27.14.26.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Aug 2023 14:26:46 -0700 (PDT) From: Jack Kamm To: Ihor Radchenko , Peter Mao Cc: org-mode-email Subject: Re: [BUG] ob-python hangs on second start [9.5.4 (release_9.5.4-763-g06373a @ ~/emacs/org-mode/lisp/)] In-Reply-To: <87ttsnh5bx.fsf@localhost> References: <87ttsnh5bx.fsf@localhost> Date: Sun, 27 Aug 2023 14:26:45 -0700 Message-ID: <87a5uc9nxm.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::130; envelope-from=jackkamm@gmail.com; helo=mail-il1-x130.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx2.migadu.com X-Migadu-Spam-Score: -9.59 X-Spam-Score: -9.59 X-Migadu-Queue-Id: F061834414 X-TUID: J390isigRSVV --=-=-= Content-Type: text/plain Ihor Radchenko writes: > Peter Mao 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. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-ob-python-Fix-hanging-on-second-start.patch >From d5721aa37a399afcd527906e5d9f1b6bce37fdb9 Mon Sep 17 00:00:00 2001 From: Jack Kamm 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 --=-=-=--