# Reading from fifo with no writers blocks the whole process

**URL:** <https://www.ruby-forum.com/t/reading-from-fifo-with-no-writers-blocks-the-whole-process/166871>\
**Category:** Ruby\
**Created:** [April 30, 2009, 11:42am UTC](https://www.ruby-forum.com/t/reading-from-fifo-with-no-writers-blocks-the-whole-process/166871 "2009-04-30T11:42:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmwr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de053/32.png) [@fmwr](https://www.ruby-forum.com/u/fmwr)\
**Post date:** [April 30, 2009, 11:42am UTC](https://www.ruby-forum.com/t/reading-from-fifo-with-no-writers-blocks-the-whole-process/166871/1 "2009-04-30T11:42:12Z")

</div>

Hi,

in my program I’m creating a thread, in which I open a FIFO and read  
data from it.

pipe\_listener\_thread = Thread.new(ARGV[0]) do |fifoname|  
while true # loop to reopen fifo, when it ends because of no writers  
fi = File.open(fifoname, “r”)  
while line = fi.gets  
puts “msg: #{line}”  
end  
fi.close  
end  
end

Everything works fine, except that when the thread is waiting to open  
the fifo, whole process - all threads seem to be blocked. Such thing  
doesn’t happen when waiting for data (inner loop) - where the thread  
alone gets blocked.

Please tell, if there’s something wrong with this behavior, or it should  
work that way.

Regs,  
Tomasz W.

---

<div class="post-metadata">

**Author:** ![fmwr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de053/32.png) [@fmwr](https://www.ruby-forum.com/u/fmwr)\
**Post date:** [April 30, 2009, 7:41pm UTC](https://www.ruby-forum.com/t/reading-from-fifo-with-no-writers-blocks-the-whole-process/166871/2 "2009-04-30T19:41:26Z")

</div>

On 30.04.2009 11:42, Tomasz W. wrote:

> end  
> end
> 
> Everything works fine, except that when the thread is waiting to open  
> the fifo, whole process - all threads seem to be blocked. Such thing  
> doesn’t happen when waiting for data (inner loop) - where the thread  
> alone gets blocked.

First of all you should change your code to use the block form of  
File.open. This avoids keeping the file descriptor open in case of  
errors.

You can do even more, see  
[http://blog.rubybestpractices.com/posts/rklemme/001-Using\_blocks\_for\_Robustness.html](http://blog.rubybestpractices.com/posts/rklemme/001-Using_blocks_for_Robustness.html)

> Please tell, if there’s something wrong with this behavior, or it should  
> work that way.

I believe this is perfectly ok. Since IO indicates eof if there is no  
more data in the fifo, it makes sense to block on open as long as there  
is nothing to read. Otherwise your program would mindlessly be looping  
and burning CPU.

I do wonder though why reading from a named fifo ever reaches eof. You  
would expect the reading to continue. OTOH that way you will be aware  
if a program terminates which writes to the fifo and can react on it  
appropriately.

Kind regards

robert
