Closed
Description
Bug report
Bug description:
I'm experiencing an issue specifically in python 3.13 regarding Threads in the context of multiprocessing.
The following code is working in python < 3.13
from multiprocessing import Process
from threading import Thread
import time
class MyThread(Thread):
def __init__(self):
Thread.__init__(self)
self.__data = ''
def run(self):
print("Hi from thread")
print(self.__data)
class Aclass():
def __init__(self):
self._t = MyThread()
self._t.daemon = True
def start(self):
self._t.start()
print("thread started")
if __name__ == '__main__':
t = Aclass()
p = Process(target = t.start )
p.start()
time.sleep(2)
After executing the above in python 3.13 I get this output:
File "/usr/lib/python3.13/multiprocessing/process.py", line 313, in _bootstrap
self.run()
~~~~~~~~^^
File "/usr/lib/python3.13/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/kali/impacket/master/impacket/tests/SMB_RPC/test_bug_process_thread.py", line 18, in start
self._t.start()
~~~~~~~~~~~~~^^
File "/usr/lib/python3.13/threading.py", line 973, in start
_start_joinable_thread(self._bootstrap, handle=self._handle,
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
daemon=self.daemon)
^^^^^^^^^^^^^^^^^^^
RuntimeError: thread already started
I managed to workaround this by delaying the Thread instance initialization, moving the Thread.init() call to start() method in the derived class, so everything gets executed in the context of the child process.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
- gh-134381: Improve thread safety in _PyThread_AfterFork by preserving not-started handles #134514
- [3.14] gh-134381: Fix RuntimeError when starting not-yet started Thread after fork (gh-134514) #134596
- [3.13] gh-134381: Fix RuntimeError when starting not-yet started Thread after fork (gh-134514) #134597