]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-136470: Correct InterpreterPoolExecutor's default thread name (GH-136472)
authorAN Long <aisk@users.noreply.github.com>
Sun, 20 Jul 2025 23:34:32 +0000 (08:34 +0900)
committerGitHub <noreply@github.com>
Sun, 20 Jul 2025 23:34:32 +0000 (23:34 +0000)
The OS thread name is now correctly prefixed with `InterpreterPoolExecutor` instead of `ThreadPoolExecutor`.

Lib/concurrent/futures/interpreter.py
Lib/test/test_concurrent_futures/test_interpreter_pool.py
Misc/NEWS.d/next/Library/2025-07-10-00-47-37.gh-issue-136470.KlUEUG.rst [new file with mode: 0644]

index cbb60ce80c181355a19e86c85cdda212aa21c962..53c6e757ded2e33303f7ad119b9290ba9e4b1d1e 100644 (file)
@@ -118,5 +118,7 @@ class InterpreterPoolExecutor(_thread.ThreadPoolExecutor):
                 each worker interpreter.
             initargs: A tuple of arguments to pass to the initializer.
         """
+        thread_name_prefix = (thread_name_prefix or
+                              (f"InterpreterPoolExecutor-{self._counter()}"))
         super().__init__(max_workers, thread_name_prefix,
                          initializer, initargs)
index d5c032d01cdf5d6a39bc9b6c34d33ea478cceadc..7241fcc4b1e74d73125313a6844734a75f72d112 100644 (file)
@@ -1,3 +1,4 @@
+import _thread
 import asyncio
 import contextlib
 import io
@@ -498,6 +499,20 @@ class InterpreterPoolExecutorTest(
         self.assertEqual(p.stdout.decode(), '')
         self.assertEqual(p.stderr.decode(), '')
 
+    def test_thread_name_prefix(self):
+        self.assertStartsWith(self.executor._thread_name_prefix,
+                              "InterpreterPoolExecutor-")
+
+    @unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")
+    def test_thread_name_prefix_with_thread_get_name(self):
+        def get_thread_name():
+            import _thread
+            return _thread._get_name()
+
+        # Some platforms (Linux) are using 16 bytes to store the thread name,
+        # so only compare the first 15 bytes (without the trailing \n).
+        self.assertStartsWith(self.executor.submit(get_thread_name).result(),
+                              "InterpreterPoolExecutor-"[:15])
 
 class AsyncioTest(InterpretersMixin, testasyncio_utils.TestCase):
 
diff --git a/Misc/NEWS.d/next/Library/2025-07-10-00-47-37.gh-issue-136470.KlUEUG.rst b/Misc/NEWS.d/next/Library/2025-07-10-00-47-37.gh-issue-136470.KlUEUG.rst
new file mode 100644 (file)
index 0000000..5a0429c
--- /dev/null
@@ -0,0 +1,2 @@
+Correct :class:`concurrent.futures.InterpreterPoolExecutor`'s default thread
+name.