File tree 1 file changed +19
-30
lines changed
1 file changed +19
-30
lines changed Original file line number Diff line number Diff line change @@ -287,54 +287,43 @@ def maybe_start_timer
287
287
# thread, it'll create a timer on a promise that no one is going to action on going forward.
288
288
# So, that leads to leaking the timer until it times out. To avoid this, we want to
289
289
# check that the future of the promise isn't completed before starting the timer.
290
- # Finally, we cancel a potentially existing timer before scheduling a new one. That's
291
- # just defensive programming -- that scenario shouldn't be possible right now.
292
- if @timeout
293
- @lock . lock
294
- begin
295
- @scheduler . cancel_timer ( @timer ) if @timer
296
- unless @future . completed?
290
+
291
+ return if @timeout . nil?
292
+ return if @future . completed?
293
+
294
+ if @timer . nil?
295
+ @lock . synchronize do
296
+ if @timer . nil?
297
+ return if @future . completed?
297
298
@timer = @scheduler . schedule_timer ( @timeout )
298
- @timer . on_value do
299
- time_out!
300
- end
299
+ @timer . on_value { time_out! }
301
300
end
302
- ensure
303
- @lock . unlock
304
301
end
305
302
end
306
303
end
307
304
308
305
def fulfill ( response )
309
306
super
310
-
311
- if @timeout
312
- @lock . lock
313
- begin
314
- if @timer
315
- @scheduler . cancel_timer ( @timer )
316
- @timer = nil
317
- end
318
- ensure
319
- @lock . unlock
320
- end
321
- end
307
+ maybe_cancel_timer
322
308
end
323
309
324
310
def fail ( cause )
325
311
super
312
+ maybe_cancel_timer
313
+ end
326
314
327
- if @timeout
328
- @lock . lock
329
- begin
315
+ def maybe_cancel_timer
316
+ return if @timeout . nil?
317
+ timer = nil
318
+ if @timer
319
+ @lock . synchronize do
330
320
if @timer
331
- @scheduler . cancel_timer ( @ timer)
321
+ timer = @ timer
332
322
@timer = nil
333
323
end
334
- ensure
335
- @lock . unlock
336
324
end
337
325
end
326
+ @scheduler . cancel_timer ( timer ) if timer
338
327
end
339
328
end
340
329
You can’t perform that action at this time.
0 commit comments