From 2b356fac492ed6a260e2f2a53eb40f976eb00b1e Mon Sep 17 00:00:00 2001 From: bajankristof Date: Wed, 19 Jun 2024 10:30:23 +0200 Subject: [PATCH] fix: resolve possible race condition in FFMPEG::Timeout --- lib/ffmpeg/timeout.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/ffmpeg/timeout.rb b/lib/ffmpeg/timeout.rb index 65d9182..0e6f125 100644 --- a/lib/ffmpeg/timeout.rb +++ b/lib/ffmpeg/timeout.rb @@ -11,16 +11,21 @@ def self.start(duration, message = nil) end def pause - @paused = true + @mutex.synchronize { @paused = true } + nil end def resume - @paused = false - tick + @mutex.synchronize do + @last_tick = Time.now + @paused = false + nil + end end def tick - @last_tick = Time.now + @mutex.synchronize { @last_tick = Time.now } + nil end def cancel @@ -33,6 +38,7 @@ def cancel private def initialize(duration, message = nil) + @mutex = Mutex.new @duration = duration @message = message @@ -43,7 +49,7 @@ def initialize(duration, message = nil) end def loop - sleep 0.1 while @paused || Time.now - @last_tick <= @duration + sleep 0.1 while @mutex.synchronize { @paused || Time.now - @last_tick <= @duration } @current_thread.raise(::Timeout::Error, @message || self.class.name) end