public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: "Alexandra Hájková" <ahajkova@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/2] gdb/python: avoid use of _PyOS_ReadlineTState
Date: Thu,  7 Dec 2023 12:02:40 +0100	[thread overview]
Message-ID: <20231207110240.1853760-2-ahajkova@redhat.com> (raw)
In-Reply-To: <20231207110240.1853760-1-ahajkova@redhat.com>

From: Andrew Burgess <aburgess@redhat.com>

In python/py-gdb-readline.c we make use of _PyOS_ReadlineTState,
however, this variable is no longer public in Python 3.13, and so GDB
no longer builds.

We are making use of _PyOS_ReadlineTState in order to re-acquire the
Python Global Interpreter Lock (GIL).  The _PyOS_ReadlineTState
variable is set in Python's outer readline code prior to calling the
user (GDB) supplied readline callback function, which for us is
gdbpy_readline_wrapper.  The gdbpy_readline_wrapper function is called
without the GIL held.

Instead of using _PyOS_ReadlineTState, I propose that we switch to
calling PyGILState_Ensure() and PyGILState_Release().  These functions
will acquire the GIL based on the current thread.  I think this should
be sufficient; I can't imagine why we'd be running
gdbpy_readline_wrapper on one thread on behalf of a different Python
thread.... that would be unexpected I think.
---
 gdb/python/py-gdb-readline.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/gdb/python/py-gdb-readline.c b/gdb/python/py-gdb-readline.c
index 124cec8055d..699d396dd19 100644
--- a/gdb/python/py-gdb-readline.c
+++ b/gdb/python/py-gdb-readline.c
@@ -56,13 +56,11 @@ gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout,
       if (except.reason == RETURN_QUIT)
 	return NULL;
 
-      /* The thread state is nulled during gdbpy_readline_wrapper,
-	 with the original value saved in the following undocumented
-	 variable (see Python's Parser/myreadline.c and
-	 Modules/readline.c).  */
-      PyEval_RestoreThread (_PyOS_ReadlineTState);
+
+      /* This readline callback is called without the GIL held.  */
+      gdbpy_gil gil;
+
       gdbpy_convert_exception (except);
-      PyEval_SaveThread ();
       return NULL;
     }
 
-- 
2.41.0


  reply	other threads:[~2023-12-07 11:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-07 11:02 [PATCH 1/2] gdb: move gdbpy_gil into python-internal.h Alexandra Hájková
2023-12-07 11:02 ` Alexandra Hájková [this message]
2023-12-08 18:04   ` [PATCH 2/2] gdb/python: avoid use of _PyOS_ReadlineTState Tom Tromey
2023-12-08 18:04 ` [PATCH 1/2] gdb: move gdbpy_gil into python-internal.h Tom Tromey

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=20231207110240.1853760-2-ahajkova@redhat.com \
    --to=ahajkova@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).