Commits

Ronny Pfannschmidt committed 96900be

fix issue 8 - don't kill remote pids on local jython

Comments (0)

Files changed (4)

   so it can send from python2 to python3
 
 - fix issue #9 - propperly terminate the worker threadpools in safe_terminate
+- fix issue #8 - no longer kill remote pids locally on jython ssh gateways
 
 - refactor socketserver, so it can be directly remote_exec'd for starting a socket gateway on a remote
 

execnet/gateway.py

 class Gateway(gateway_base.BaseGateway):
     """ Gateway to a local or remote Python Intepreter. """
 
-    def __init__(self, io, id):
-        super(Gateway, self).__init__(io=io, id=id, _startcount=1)
+    def __init__(self, io, spec):
+        super(Gateway, self).__init__(io=io, id=spec.id, _startcount=1)
+        self.spec = spec
         self._initreceive()
 
     @property

execnet/gateway_bootstrap.py

     io.write((repr(source)+ "\n").encode('ascii'))
 
 
+def fix_pid_for_jython_popen(gw):
+    """
+    fix for jython 2.5.1
+    """
+    spec, io = gw.spec, gw._io
+    if spec.popen:
+        if io.popen.pid is None:
+            io.popen.pid = gw.remote_exec(
+                "import os; channel.send(os.getpid())").receive()
+
+
 def bootstrap(io, spec):
     if spec.popen:
         bootstrap_popen(io, spec)
         bootstrap_socket(io, spec)
     else:
         raise ValueError('unknown gateway type, cant bootstrap')
-    gw = Gateway(io, spec.id)
-    if hasattr(io, 'popen'):
-        # fix for jython 2.5.1
-        if io.popen.pid is None:
-            io.popen.pid = gw.remote_exec(
-                "import os; channel.send(os.getpid())").receive()
+    gw = Gateway(io, spec)
+    fix_pid_for_jython_popen(gw)
     return gw
 
 

testing/test_fixes.py

+from execnet import Group
+from execnet.gateway_bootstrap import fix_pid_for_jython_popen
+
+def test_jython_bootstrap_fix():
+    group = Group()
+    gw = group.makegateway('popen')
+    popen = gw._io.popen
+    real_pid = popen.pid
+    try:
+        # nothing happens when calling it on a normal seyup
+        fix_pid_for_jython_popen(gw)
+        assert popen.pid == real_pid
+
+        # if there is no pid for a popen gw, restore
+        popen.pid = None
+        fix_pid_for_jython_popen(gw)
+        assert popen.pid == real_pid
+
+        # if there is no pid for other gw, ignore - they are remote
+        gw.spec.popen = False
+        popen.pid = None
+        fix_pid_for_jython_popen(gw)
+        assert popen.pid is None
+
+    finally:
+        popen.pid = real_pid
+        group.terminate(timeout=1)