Skip to content

Commit

Permalink
fix: resolve possible race condition in FFMPEG::Timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
bajankristof committed Jun 19, 2024
1 parent 4fedfad commit 2b356fa
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/ffmpeg/timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +38,7 @@ def cancel
private

def initialize(duration, message = nil)
@mutex = Mutex.new
@duration = duration
@message = message

Expand All @@ -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
Expand Down

0 comments on commit 2b356fa

Please sign in to comment.