THRIFT-170: Buffered transports leave data in write buffer on failed flush

Reviewed By: mcslee



git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@704714 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/py/src/transport/TTransport.py b/lib/py/src/transport/TTransport.py
index ab0f98d..b7268f5 100644
--- a/lib/py/src/transport/TTransport.py
+++ b/lib/py/src/transport/TTransport.py
@@ -146,9 +146,11 @@
     self.__wbuf.write(buf)
 
   def flush(self):
-    self.__trans.write(self.__wbuf.getvalue())
-    self.__trans.flush()
+    out = self.__wbuf.getvalue()
+    # reset wbuf before write/flush to preserve state on underlying failure
     self.__wbuf = StringIO()
+    self.__trans.write(out)
+    self.__trans.flush()
 
   # Implement the CReadableTransport interface.
   @property
@@ -275,6 +277,8 @@
       return self.__trans.flush()
     wout = self.__wbuf.getvalue()
     wsz = len(wout)
+    # reset wbuf before write/flush to preserve state on underlying failure
+    self.__wbuf = StringIO()
     # N.B.: Doing this string concatenation is WAY cheaper than making
     # two separate calls to the underlying socket object. Socket writes in
     # Python turn out to be REALLY expensive, but it seems to do a pretty
@@ -282,4 +286,3 @@
     buf = pack("!i", wsz) + wout
     self.__trans.write(buf)
     self.__trans.flush()
-    self.__wbuf = StringIO()