Commit 84a3f136 authored by Martín Ferrari's avatar Martín Ferrari

Handle connection resets in X11_forwarder

parent c6ac9222
...@@ -816,6 +816,25 @@ def _spawn_x11_forwarder(server, xsock, xaddr): ...@@ -816,6 +816,25 @@ def _spawn_x11_forwarder(server, xsock, xaddr):
os._exit(1) os._exit(1)
def _x11_forwarder(server, xsock, xaddr): def _x11_forwarder(server, xsock, xaddr):
def clean(idx, toread, fd):
# silently discards any buffer!
fd1 = fd
fd2 = idx[fd]["wr"]
try:
fd1.close()
except:
pass
try:
fd2.close()
except:
pass
del idx[fd1]
if fd1 in toread:
toread.remove(fd1)
del idx[fd2]
if fd2 in toread:
toread.remove(fd2)
toread = set([server]) toread = set([server])
idx = {} idx = {}
while(True): while(True):
...@@ -847,11 +866,16 @@ def _x11_forwarder(server, xsock, xaddr): ...@@ -847,11 +866,16 @@ def _x11_forwarder(server, xsock, xaddr):
try: try:
s = os.read(fd.fileno(), 4096) s = os.read(fd.fileno(), 4096)
except OSError, e: except OSError, e:
if e.errno != errno.EINTR: if e.errno == errno.ECONNRESET:
raise clean(idx, toread, fd)
continue
elif e.errno == errno.EINTR:
continue continue
else:
raise
if s == "": if s == "":
# fd closed # fd closed for read
toread.remove(fd) toread.remove(fd)
chan["closed"] = True chan["closed"] = True
if not chan["buf"]: if not chan["buf"]:
...@@ -870,18 +894,10 @@ def _x11_forwarder(server, xsock, xaddr): ...@@ -870,18 +894,10 @@ def _x11_forwarder(server, xsock, xaddr):
except OSError, e: except OSError, e:
if e.errno == errno.EINTR: if e.errno == errno.EINTR:
continue continue
if e.errno != errno.EPIPE: if e.errno == errno.EPIPE or e.errno == errno.ECONNRESET:
raise clean(idx, toread, fd)
# broken pipe, discard output and close
try:
chan["rd"].shutdown(socket.SHUT_RD)
chan["wr"].shutdown(socket.SHUT_WR)
except:
pass
toread.remove(chan["rd"])
chan["closed"] = 1
chan["buf"] = None
continue continue
raise
if x < len(chan["buf"][0]): if x < len(chan["buf"][0]):
chan["buf"][0] = chan["buf"][x:] chan["buf"][0] = chan["buf"][x:]
...@@ -900,13 +916,4 @@ def _x11_forwarder(server, xsock, xaddr): ...@@ -900,13 +916,4 @@ def _x11_forwarder(server, xsock, xaddr):
if not chan["closed"] or chan["buf"] or not twin["closed"] \ if not chan["closed"] or chan["buf"] or not twin["closed"] \
or twin["buf"]: or twin["buf"]:
continue continue
try: clean(idx, toread, chan["rd"])
chan["rd"].close()
except:
pass
try:
chan["wr"].close()
except:
pass
del idx[chan["rd"]]
del idx[chan["wr"]]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment