spladug commented on a change in pull request #2200:
URL: https://github.com/apache/thrift/pull/2200#discussion_r464523036



##########
File path: lib/py/src/transport/TSocket.py
##########
@@ -74,7 +74,30 @@ def setHandle(self, h):
         self.handle = h
 
     def isOpen(self):
-        return self.handle is not None
+        if self.handle is None:
+            return False
+
+        # this lets us cheaply see if the other end of the socket is still
+        # connected. if disconnected, we'll get EOF back (expressed as zero
+        # bytes of data) otherwise we'll get one byte or an error indicating
+        # we'd have to block for data.
+        #
+        # note that we're not doing this with socket.MSG_DONTWAIT because 1)
+        # it's linux-specific and 2) gevent-patched sockets hide EAGAIN from us
+        # when timeout is non-zero.
+        original_timeout = self.handle.gettimeout()
+        try:
+            self.handle.settimeout(0)
+            try:
+                peeked_bytes = self.handle.recv(1, socket.MSG_PEEK)
+            except (socket.error, OSError) as exc:  # on modern python this is 
just BlockingIOError
+                if exc.errno == errno.EWOULDBLOCK:

Review comment:
       I'll add both and make it swallow the unexpected exception.
   
   FWIW, looks like the most recent OS to have EAGAIN != EWOULDBLOCK  is quite 
old: 
https://stackoverflow.com/questions/7003234/which-systems-define-eagain-and-ewouldblock-as-different-values/7003379#7003379
 but it makes sense to be most portable here.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to