@@ -291,14 +291,29 @@ def _disable_socket_timeout(self, socket):
291291 """ Depending on the combination of python version and whether we're
292292 connecting over http or https, we might need to access _sock, which
293293 may or may not exist; or we may need to just settimeout on socket
294- itself, which also may or may not have settimeout on it.
294+ itself, which also may or may not have settimeout on it. To avoid
295+ missing the correct one, we try both.
295296
296- To avoid missing the correct one, we try both.
297+ We also do not want to set the timeout if it is already disabled, as
298+ you run the risk of changing a socket that was non-blocking to
299+ blocking, for example when using gevent.
297300 """
298- if hasattr (socket , "settimeout" ):
299- socket .settimeout (None )
300- if hasattr (socket , "_sock" ) and hasattr (socket ._sock , "settimeout" ):
301- socket ._sock .settimeout (None )
301+ sockets = [socket , getattr (socket , '_sock' , None )]
302+
303+ for s in sockets :
304+ if not hasattr (s , 'settimeout' ):
305+ continue
306+
307+ timeout = - 1
308+
309+ if hasattr (s , 'gettimeout' ):
310+ timeout = s .gettimeout ()
311+
312+ # Don't change the timeout if it is already disabled.
313+ if timeout is None or timeout == 0.0 :
314+ continue
315+
316+ s .settimeout (None )
302317
303318 def _get_result (self , container , stream , res ):
304319 cont = self .inspect_container (container )
0 commit comments