]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-138860: Lazy import rlcompleter in pdb to avoid deadlock in… (#139281)
authorTian Gao <gaogaotiantian@hotmail.com>
Wed, 24 Sep 2025 06:07:50 +0000 (14:07 +0800)
committerGitHub <noreply@github.com>
Wed, 24 Sep 2025 06:07:50 +0000 (14:07 +0800)
[3.13] gh-138860: Lazy import rlcompleter in pdb to avoid deadlock in subprocess (GH-139185)
(cherry picked from commit c8624cd36746b17d8f991cde63705e9419e940de)

Lib/pdb.py
Misc/NEWS.d/next/Library/2025-09-20-17-50-31.gh-issue-138860.Y9JXap.rst [new file with mode: 0644]

index cb0a3405c58e55533b1bcc811d24c0ccdcb2daa9..41735f4e249f5da75b4b72c31ce8ffef004acdcf 100755 (executable)
@@ -90,7 +90,6 @@ import linecache
 import _colorize
 
 from contextlib import contextmanager
-from rlcompleter import Completer
 from types import CodeType
 
 
@@ -332,6 +331,15 @@ class Pdb(bdb.Bdb, cmd.Cmd):
             readline.set_completer_delims(' \t\n`@#%^&*()=+[{]}\\|;:\'",<>?')
         except ImportError:
             pass
+
+        # GH-138860
+        # We need to lazy-import rlcompleter to avoid deadlock
+        # We cannot import it during self.complete* methods because importing
+        # rlcompleter for the first time will overwrite readline's completer
+        # So we import it here and save the Completer class
+        from rlcompleter import Completer
+        self.RlCompleter = Completer
+
         self.allow_kbdint = False
         self.nosigint = nosigint
         # Consider these characters as part of the command so when the users type
@@ -986,10 +994,9 @@ class Pdb(bdb.Bdb, cmd.Cmd):
             conv_vars = self.curframe.f_globals.get('__pdb_convenience_variables', {})
             return [f"${name}" for name in conv_vars if name.startswith(text[1:])]
 
-        # Use rlcompleter to do the completion
         state = 0
         matches = []
-        completer = Completer(self.curframe.f_globals | self.curframe_locals)
+        completer = self.RlCompleter(self.curframe.f_globals | self.curframe.f_locals)
         while (match := completer.complete(text, state)) is not None:
             matches.append(match)
             state += 1
diff --git a/Misc/NEWS.d/next/Library/2025-09-20-17-50-31.gh-issue-138860.Y9JXap.rst b/Misc/NEWS.d/next/Library/2025-09-20-17-50-31.gh-issue-138860.Y9JXap.rst
new file mode 100644 (file)
index 0000000..0903eb7
--- /dev/null
@@ -0,0 +1 @@
+Lazy import :mod:`rlcompleter` in :mod:`pdb` to avoid deadlock in subprocess.