sshserver: redirect stdin/stdout early and use duplicated streams
authorYuya Nishihara <yuya@tcha.org>
Sun, 25 Mar 2018 16:47:33 +0900
changeset 37989 bfe8ef6e370e
parent 37988 dc1ed7fe33e4
child 37990 57b0c7221dba
sshserver: redirect stdin/stdout early and use duplicated streams This is what we achieved with hook.redirect(True) plus ui.fout = ui.ferr. The hook.redirect() function can't be completely removed yet since hgweb still depends on it. I'm not sure if it is necessary for WSGI servers. CGI needs it, but does WSGI communicate over stdin/stdout channels?
mercurial/wireprotoserver.py
--- a/mercurial/wireprotoserver.py	Sun Mar 25 16:35:24 2018 +0900
+++ b/mercurial/wireprotoserver.py	Sun Mar 25 16:47:33 2018 +0900
@@ -18,7 +18,6 @@
 from . import (
     encoding,
     error,
-    hook,
     pycompat,
     util,
     wireprototypes,
@@ -785,8 +784,7 @@
     def __init__(self, ui, repo, logfh=None):
         self._ui = ui
         self._repo = repo
-        self._fin = ui.fin
-        self._fout = ui.fout
+        self._fin, self._fout = procutil.protectstdio(ui.fin, ui.fout)
 
         # Log write I/O to stdout and stderr if configured.
         if logfh:
@@ -795,11 +793,10 @@
             ui.ferr = util.makeloggingfileobject(
                 logfh, ui.ferr, 'e', logdata=True)
 
-        hook.redirect(True)
-        ui.fout = repo.ui.fout = ui.ferr
-
     def serve_forever(self):
         self.serveuntil(threading.Event())
+        procutil.restorestdio(self._ui.fin, self._ui.fout,
+                              self._fin, self._fout)
         sys.exit(0)
 
     def serveuntil(self, ev):