Skip to content

Commit cacdafd

Browse files
committed
switch to an RLock as handle_close is called sometimes during _flush_some
1 parent 5bf34d5 commit cacdafd

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

waitress/channel.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def __init__(
7575
# task_lock used to push/pop requests
7676
self.task_lock = threading.Lock()
7777
# outbuf_lock used to access any outbuf
78-
self.outbuf_lock = threading.Lock()
78+
self.outbuf_lock = threading.RLock()
7979

8080
wasyncore.dispatcher.__init__(self, sock, map=map)
8181

@@ -151,7 +151,7 @@ def handle_write(self):
151151
self.will_close = True
152152

153153
if self.will_close:
154-
self.handle_close(lock=False)
154+
self.handle_close()
155155

156156
def readable(self):
157157
# We might want to create a new task. We can only do this if:
@@ -168,7 +168,7 @@ def handle_read(self):
168168
except socket.error:
169169
if self.adj.log_socket_errors:
170170
self.logger.exception('Socket error')
171-
self.handle_close(lock=True)
171+
self.handle_close()
172172
return
173173
if data:
174174
self.last_activity = time.time()
@@ -274,21 +274,16 @@ def _flush_some(self):
274274

275275
return False
276276

277-
def handle_close(self, lock=True):
277+
def handle_close(self):
278278
# NB: default to True for when asyncore calls this function directly
279-
if lock:
280-
self.outbuf_lock.acquire()
281-
try:
279+
with self.outbuf_lock:
282280
for outbuf in self.outbufs:
283281
try:
284282
outbuf.close()
285283
except:
286284
self.logger.exception(
287285
'Unknown exception while trying to close outbuf')
288286
self.connected = False
289-
finally:
290-
if lock:
291-
self.outbuf_lock.release()
292287
wasyncore.dispatcher.close(self)
293288

294289
def add_channel(self, map=None):

0 commit comments

Comments
 (0)