Xpra: Ticket #203: SSH is killed on Control-C detaching

Please don't kill SSH when detaching on Ctrl+C signal.

SSH have a powerful feature to reuse existing connections. For example if ~/.ssh/config have the following:

host *
    controlmaster auto
    controlpath ~/.ssh/__tmp-ssh-%r@%h:%p

then SSH will create a socket file for first connection to remote host and subsequent connections will reuse the existing connection through the sock file.

When you log off from first "master" session, SSH will not exit until all other connections are closed. If you terminate "master" SSH process then all other connections to the same host will be terminated as well.

In Xpra when I attach to remote session without any prior ssh connections to the remote host a "master" connection is created. If later I have other SSH connections to the same host they are reusing "master" SSH connection initiated by Xpra.

The problem is that all SSH connections are terminated if I detach from "master" session by sending "Control-C" to Xpra client because it kills master SSH connection. This do not happen when I detach using "Disconnect" from Xpra client menu.



Sat, 03 Nov 2012 04:12:24 GMT - Antoine Martin: status changed

ssh may currently receive the Ctrl+C, we would have to prevent that - and doing that would mean that we may make it more difficult to interact with ssh via the terminal (password input for example).

then we have to hook up the signal handler to fire the disconnect code.

do-able but tricky.


Sat, 03 Nov 2012 07:22:19 GMT - Antoine Martin:

please try r2029 and let me know if that works for you

This is more complicated than it seems, and here are some useful pointers I found along the way:


Sat, 03 Nov 2012 10:57:09 GMT - onlyjob:

Well done, this fixes the issue. Thank you.


Sat, 03 Nov 2012 10:58:28 GMT - Antoine Martin: status changed; resolution set


Fri, 08 Mar 2013 01:52:07 GMT - onlyjob: status changed; resolution deleted

Xpra-0.8.8 is exhibit this problem again...

When two or more sessions are attached to the host, terminating first Xpra session with Ctrl+C terminates the other one(s) as well because it closes SSH control connection.


Fri, 08 Mar 2013 03:04:10 GMT - Antoine Martin:

This was caused by r2762, see #260

This probably fixes it:

--- src/xpra/scripts/main.py	(revision 2907)
+++ src/xpra/scripts/main.py	(working copy)
@@ -556,6 +556,17 @@
                 ssh_fail_cb(error_message)
                 raise IOError(error_message)
         def stop_tunnel():
+            if os.name=="posix":
+                #on posix, the tunnel may be shared with other processes
+                #so don't kill it... which may leave it behind after use.
+                #but at least make sure we close all the pipes:
+                for name,fd in {"stdin" : child.stdin, "stdout" : child.stdout, "stderr" : child.stderr}.items():
+                    try:
+                        if fd:
+                            fd.close()
+                    except Exception, e:
+                        print("error closing ssh tunnel %s: %s" % (name, e))
+                return
             try:
                 if child.poll() is None:
                     #only supported on win32 since Python 2.7

Since terminating the ssh process may or may not be the right thing to do, depending on what platform you are on, how ssh is setup, personal preference, etc.. maybe this should even be an option?


Fri, 08 Mar 2013 03:43:40 GMT - Antoine Martin: status changed; resolution set

applied in r2908, will backport to 0.8.x

closing, feel free to re-open if not fixed


Mon, 15 Jul 2013 04:55:57 GMT - Antoine Martin: status changed; resolution deleted

Unfortunately, this has caused a bad regression: this prevents us from using password mode with ssh, see #380

So unless a better solution can be found, I will remove this code.


Thu, 20 Mar 2014 07:45:37 GMT - Antoine Martin: status changed; resolution set

Since it is impossible to have both, r5862 deals with this problem by giving the user the option of having keyboard interaction (--exit-ssh - which is now the default) or having connection sharing (--no-exit-ssh).


Sun, 22 Jul 2018 08:26:32 GMT - Antoine Martin:

Information added here: wiki/SSH.


Sat, 23 Jan 2021 04:48:20 GMT - migration script:

this ticket has been moved to: https://github.com/Xpra-org/xpra/issues/203