@@ -89,10 +89,10 @@ def read_channel(self, channel, timeout=0):
89
89
90
90
def readline_channel (self , channel , timeout = None ):
91
91
"""Read a line from a channel."""
92
- if timeout is None :
93
- timeout = float ( "inf" )
92
+ if timeout is not None and timeout < 0 :
93
+ timeout = None
94
94
start = time .time ()
95
- while self . is_open () and time .time () - start < timeout :
95
+ while timeout is None or time .time () - start < timeout :
96
96
if channel in self ._channels :
97
97
data = self ._channels [channel ]
98
98
if self .newline in data :
@@ -104,7 +104,15 @@ def readline_channel(self, channel, timeout=None):
104
104
else :
105
105
del self ._channels [channel ]
106
106
return ret
107
- self .update (timeout = (timeout - time .time () + start ))
107
+
108
+ if not self .is_open ():
109
+ return
110
+
111
+ if timeout is not None :
112
+ # the timeout here should never be negative, because otherwise this method could block indefinitly
113
+ self .update (timeout = max (timeout - time .time () + start , 0 ))
114
+ else :
115
+ self .update (timeout = None )
108
116
109
117
def write_channel (self , channel , data ):
110
118
"""Write data to a channel."""
@@ -190,6 +198,13 @@ def update(self, timeout=0):
190
198
r = poll .poll (timeout )
191
199
poll .unregister (self .sock .sock )
192
200
else :
201
+ # select.select() does not work with negative timeouts, when a negative value is
202
+ # given select.epoll() and select.poll() are blocking until there is an event for
203
+ # the poll object, therefore set the timeout to None in order to have the same
204
+ # behaviour when select.select() is used
205
+ if timeout is not None and timeout < 0 :
206
+ timeout = None
207
+
193
208
r , _ , _ = select .select (
194
209
(self .sock .sock , ), (), (), timeout )
195
210
@@ -220,10 +235,11 @@ def update(self, timeout=0):
220
235
def run_forever (self , timeout = None ):
221
236
"""Wait till connection is closed or timeout reached. Buffer any input
222
237
received during this time."""
223
- if timeout :
238
+ if timeout is not None :
224
239
start = time .time ()
225
240
while self .is_open () and time .time () - start < timeout :
226
- self .update (timeout = (timeout - time .time () + start ))
241
+ # the timeout here should never be negative, because otherwise this method could block indefinitly
242
+ self .update (timeout = max (timeout - time .time () + start , 0 ))
227
243
else :
228
244
while self .is_open ():
229
245
self .update (timeout = None )
0 commit comments