Commits

Brian Kearns committed 3b06320

reset mainthreadident after fork (thanks amaury)

  • Participants
  • Parent commits 481f9cd

Comments (0)

Files changed (3)

File pypy/module/thread/gil.py

         return result
 
     def reinit_threads(self, space):
+        "Called in the child process after a fork()"
+        OSThreadLocals.reinit_threads(self, space)
         if self.gil_ready:     # re-initialize the gil if needed
             self._initialize_gil(space)
 

File pypy/module/thread/test/test_fork.py

                 self.timeout_killer(pid, 5)
                 exitcode = os.waitpid(pid, 0)[1]
                 assert exitcode == 0 # if 9, process was killed by timer!
+
+    def test_forked_is_main_thread(self):
+        "Checks that a forked interpreter is the main thread"
+        import os, thread, signal
+
+        if not hasattr(os, 'fork'):
+            skip("No fork on this platform")
+
+        def threadfunction():
+            pid = os.fork()
+            if pid == 0:
+                print 'in child'
+                # signal() only works from the 'main' thread
+                signal.signal(signal.SIGUSR1, signal.SIG_IGN)
+                os._exit(42)
+            else:
+                self.timeout_killer(pid, 5)
+                exitcode = os.waitpid(pid, 0)[1]
+                feedback.append(exitcode)
+
+        feedback = []
+        thread.start_new_thread(threadfunction, ())
+        self.waitfor(lambda: feedback)
+        # if 0, an (unraisable) exception was raised from the forked thread.
+        # if 9, process was killed by timer.
+        # if 42<<8, os._exit(42) was correctly reached.
+        assert feedback == [42<<8]

File pypy/module/thread/threadlocals.py

             thread_is_stopping(self.getvalue())
         finally:
             self.setvalue(None)
+
+    def reinit_threads(self, space):
+        "Called in the child process after a fork()"
+        self._mainthreadident = thread.get_ident()