diff mercurial/commandserver.py @ 29513:e5b4d79a9140

commandserver: backport handling of forking server from chgserver This is common between chg and vanilla forking server, so move it to commandserver and unify handle(). It would be debatable whether we really need gc.collect() or not, but that is beyond the scope of this series. Maybe we can remove gc.collect() once all resource deallocations are switched to context manager.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 21 May 2016 15:23:21 +0900
parents 538d0003c9e0
children dda6bf886839
line wrap: on
line diff
--- a/mercurial/commandserver.py	Sat May 21 15:18:23 2016 +0900
+++ b/mercurial/commandserver.py	Sat May 21 15:23:21 2016 +0900
@@ -8,7 +8,9 @@
 from __future__ import absolute_import
 
 import errno
+import gc
 import os
+import random
 import struct
 import sys
 import traceback
@@ -338,6 +340,13 @@
 
 class _requesthandler(socketserver.StreamRequestHandler):
     def handle(self):
+        # use a different process group from the master process, making this
+        # process pass kernel "is_current_pgrp_orphaned" check so signals like
+        # SIGTSTP, SIGTTIN, SIGTTOU are not ignored.
+        os.setpgid(0, 0)
+        # change random state otherwise forked request handlers would have a
+        # same state inherited from parent.
+        random.seed()
         ui = self.server.ui
         sv = None
         try:
@@ -364,6 +373,9 @@
                 cerr = channeledoutput(self.wfile, 'e')
             traceback.print_exc(file=cerr)
             raise
+        finally:
+            # trigger __del__ since ForkingMixIn uses os._exit
+            gc.collect()
 
     def _createcmdserver(self):
         ui = self.server.ui