Possible ruby bug involving threads and IO

L

Lucas Nussbaum

Hi,

I ran into a problem demonstrated by the following code :

#!/usr/bin/ruby

require 'thread'

Thread::new {
while true do
puts "before read MOTD"
str = IO.read("/etc/motd")
puts "after read MOTD"

puts "before read UPTIME"
str = IO.read("/proc/uptime")
puts "after read UPTIME"
end
}

while true do
end

Ruby hangs on IO.read("/proc/uptime").

Note that it works with linux 2.4 (tested : 2.4.27) but not linux 2.6
(tested : 2.6.9)
It only fails with files on the proc filesystem.

Any ideas ?

Thanks,
 
W

William Morgan

Excerpts from Lucas Nussbaum's mail of 4 Jan 2005 (EST):
Ruby hangs on IO.read("/proc/uptime").

This is probably the same issue described in:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/109668
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/110600

The upshot is that certain files in /proc/ seem to hang on a select()
call, which Ruby uses for I/O within threads. You can do an strace and
watch the difference between doing this from within and from without a
Thread.

My workaround was something along the lines of:
system "cat /proc/#{file} > #{tmpfile}"
f = File.open tmpfile

I don't know that there's a better solution.
 
L

Lucas Nussbaum

Excerpts from Lucas Nussbaum's mail of 4 Jan 2005 (EST):

This is probably the same issue described in:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/109668
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/110600
true

The upshot is that certain files in /proc/ seem to hang on a select()
call, which Ruby uses for I/O within threads. You can do an strace and
watch the difference between doing this from within and from without a
Thread.

My workaround was something along the lines of:
system "cat /proc/#{file} > #{tmpfile}"
f = File.open tmpfile

I don't know that there's a better solution.

IO.popen("/bin/cat /proc/uptime")

But still, that's not very satisfying ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top