Skip to content

Commit 1135332

Browse files
Copilotenzofrnt
andcommitted
Add try-finally protection for lock operations to prevent deadlocks
Co-authored-by: enzofrnt <[email protected]>
1 parent 8857c86 commit 1135332

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

django_eventstream/views.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,16 @@ async def stream(event_request, listener):
207207
# FIXME: reconcile without re-reading from db
208208

209209
lm.lock.acquire()
210-
conflict = False
211-
if len(listener.channel_items) > 0:
212-
# items were queued while reading from the db. toss them and
213-
# read from db again
214-
listener.aevent.clear()
215-
listener.channel_items = {}
216-
conflict = True
217-
lm.lock.release()
210+
try:
211+
conflict = False
212+
if len(listener.channel_items) > 0:
213+
# items were queued while reading from the db. toss them and
214+
# read from db again
215+
listener.aevent.clear()
216+
listener.channel_items = {}
217+
conflict = True
218+
finally:
219+
lm.lock.release()
218220

219221
if conflict:
220222
continue
@@ -232,16 +234,16 @@ async def stream(event_request, listener):
232234
yield body
233235

234236
lm.lock.acquire()
235-
236-
channel_items = listener.channel_items
237-
overflow = listener.overflow
238-
error_data = listener.error
239-
240-
listener.aevent.clear()
241-
listener.channel_items = {}
242-
listener.overflow = False
243-
244-
lm.lock.release()
237+
try:
238+
channel_items = listener.channel_items
239+
overflow = listener.overflow
240+
error_data = listener.error
241+
242+
listener.aevent.clear()
243+
listener.channel_items = {}
244+
listener.overflow = False
245+
finally:
246+
lm.lock.release()
245247

246248
body = ""
247249
for channel, items in channel_items.items():

0 commit comments

Comments
 (0)