Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Too many open files" error when using named pipes

I'm running 2 scripts and after a while, I'm getting Too many open files @ error/blob.c/ImageToFile/1832.

Simplified version of the first scripts. It's supposed to read images written to image_pipe, process them, and write them to ocr_pipe for the OCR to read.

# creates 2 named pipes
File.mkfifo(image_pipe) rescue nil
File.mkfifo(ocr_pipe) rescue nil

while image = Image.read(image_pipe)
  # do some stuff with `image`...
end

The second script is using ffmpeg to extract frames from a video, writing them to image_pipe

# image_pipe is the same as the script above.

(14..movie.duration).step(0.5) do
  `/usr/local/bin/ffmpeg [some options...] #{image_pipe}`
end

I think the issue is RMagick opening too many file descriptors when reading the images in the loop of the first script, but I'm not sure how to stop that from happening. The Magick::Image class doesn't have a close method or anything, afaik.

like image 342
Robin Avatar asked Jan 25 '26 03:01

Robin


1 Answers

I did not find the root cause of the issue, but ulferts helped me find a workaround that's acceptable for me.

Instead of letting RMagick open the file itself, we should handle it on our side, and then use .from_blob to create the Magick::Image instance.

while f = File.read(image_pipe)
  image = Image.from_blob(f)
  # ... do stuff with image.
end
like image 151
Robin Avatar answered Jan 26 '26 17:01

Robin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!