# Ruby + linux /proc/acpi deadlock?

**URL:** <https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112>\
**Category:** ruby-talk\
**Created:** [18 August 2004 12:23 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112 "2004-08-18T12:23:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Morgan](https://avatars.discourse-cdn.com/v4/letter/w/b77776/32.png) [@William\_Morgan](https://rubytalk.org/u/William_Morgan)\
**Post date:** [18 August 2004 12:23 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112/1 "2004-08-18T12:23:55Z")

</div>

Hi Rubyists,

I'm working on a predictive battery monitor in Ruby. One issue I've  
found is that when I read /proc/acpi/battery/BAT1/state and there are  
other Ruby threads running, the read seems to freeze. This doesn't  
happen with other files in /proc/acpi (that I've tried, at least) and  
doesn't happen if there's no threading going on.

Here's some code that hangs on the read for me:

PROC\_FILE = "/proc/acpi/battery/BAT1/state" # freezes  
#PROC\_FILE = "/proc/acpi/power\_resource/PFAN/state" # doesn't freeze  
FREEZE = true # when this is true, the "read" thread freezes for me

def read  
&nbsp;&nbsp;puts "=== starting to read at #{Time.now}"  
&nbsp;&nbsp;IO.foreach(PROC\_FILE) { |l| puts "\> #{l}" }  
&nbsp;&nbsp;puts "=== done reading at #{Time.now}"  
end

Thread.new do  
&nbsp;&nbsp;while true  
&nbsp;&nbsp;&nbsp;&nbsp;puts "zzz..."  
&nbsp;&nbsp;&nbsp;&nbsp;sleep 1  
&nbsp;&nbsp;end  
end if FREEZE

while true  
&nbsp;&nbsp;read  
&nbsp;&nbsp;sleep 1  
end

This might be some weird kernel interaction thing that's specific to my  
buggy BIOS, but I thought I'd check and see if others experienced the  
same behavior.

Thanks,

> **···**
>
> --  
> William \<[wmorgan-ruby-talk@masanjin.net](mailto:wmorgan-ruby-talk@masanjin.net)\>

---

<div class="post-metadata">

**Author:** ![Roeland\_Moors](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@Roeland\_Moors](https://rubytalk.org/u/Roeland_Moors)\
**Post date:** [20 August 2004 11:08 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112/2 "2004-08-20T11:08:08Z")

</div>

> Hi Rubyists,
> 
> I'm working on a predictive battery monitor in Ruby. One issue I've  
> found is that when I read /proc/acpi/battery/BAT1/state and there are  
> other Ruby threads running, the read seems to freeze. This doesn't  
> happen with other files in /proc/acpi (that I've tried, at least) and  
> doesn't happen if there's no threading going on.
> 
> Here's some code that hangs on the read for me:
> 
> PROC\_FILE = "/proc/acpi/battery/BAT1/state" # freezes  
> #PROC\_FILE = "/proc/acpi/power\_resource/PFAN/state" # doesn't freeze

I have the same beavior with these files:  
PROC\_FILE = "/proc/acpi/battery/BAT0/state" # freezes  
PROC\_FILE = "/proc/acpi/power\_resource/PRCF/state" # doesn't freeze

I can't explain it 🙂

> **···**
>
> On Wed, Aug 18, 2004 at 09:23:55PM +0900, William Morgan wrote:
> 
> > FREEZE = true # when this is true, the "read" thread freezes for me
> > 
> > def read  
> > &nbsp;&nbsp;puts "=== starting to read at #{Time.now}"  
> > &nbsp;&nbsp;IO.foreach(PROC\_FILE) { |l| puts "\> #{l}" }  
> > &nbsp;&nbsp;puts "=== done reading at #{Time.now}"  
> > end
> > 
> > Thread.new do  
> > &nbsp;&nbsp;while true  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "zzz..."  
> > &nbsp;&nbsp;&nbsp;&nbsp;sleep 1  
> > &nbsp;&nbsp;end  
> > end if FREEZE
> > 
> > while true  
> > &nbsp;&nbsp;read  
> > &nbsp;&nbsp;sleep 1  
> > end
> > 
> > This might be some weird kernel interaction thing that's specific to my  
> > buggy BIOS, but I thought I'd check and see if others experienced the  
> > same behavior.
> > 
> > Thanks,
> > 
> > --  
> > William \<wmorgan-ruby-talk@masanjin.net\>

---

<div class="post-metadata">

**Author:** ![Martin\_Pirker](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@Martin\_Pirker](https://rubytalk.org/u/Martin_Pirker)\
**Post date:** [23 August 2004 21:05 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112/3 "2004-08-23T21:05:42Z")

</div>

back when I was starting in Ruby one of the first small tries was...  
(fix code first, terminal is eating some chars)

run with no window decoration  
not very pretty, but does the job till today 🙂

Martin

> **···**
>
> William Morgan \<wmorgan-ruby-talk@masanjin.net\> wrote:
> 
> > I'm working on a predictive battery monitor in Ruby.
> 
> --------------------------------------------------------------------------
> 
> $stdout.sync = true
> 
> def resize (w,h) # resize terminal  
> &nbsp;&nbsp;printf("\033[8;#{h};#{w}t")  
> end
> 
> def readme  
> &nbsp;&nbsp;r1 = IO.readlines("/proc/acpi/battery/BAT1/state").grep(/remaining/)  
> &nbsp;&nbsp;r1 = /\s\*(\d\*)\smWh/.match(r1[0])[1].to\_s if r1.size == 1  
> &nbsp;&nbsp;return r1  
> end
> 
> resize (10,1)
> 
> r = IO.readlines("/proc/acpi/battery/BAT1/state")  
> if /present.+no/ === r[0] then  
> printf("\n no batt ")  
> sleep 3  
> exit 1  
> end
> 
> or1 = readme  
> printf("\n%s 000",or1.rjust(5).tr(" ","0"))  
> nr1 = readme
> 
> loop do  
> &nbsp;&nbsp;nr1 = readme  
> &nbsp;&nbsp;if (nr1 != or1)  
> &nbsp;&nbsp;&nbsp;&nbsp;if or1.to\_i \<3000 then  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'xgamma -gamma 0.3' # \<---- fix me!  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep 0.3  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'xgamma -gamma 1.0' # \<---- fix me!  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;printf("\n%s %s",nr1.rjust(5).tr(" ","0"),(or1.to\_i-nr1.to\_i).to\_s.rjust(3).tr(" ","0"))  
> &nbsp;&nbsp;&nbsp;&nbsp;or1 = nr1  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;sleep 15  
> end

---

<div class="post-metadata">

**Author:** ![William\_Morgan](https://avatars.discourse-cdn.com/v4/letter/w/b77776/32.png) [@William\_Morgan](https://rubytalk.org/u/William_Morgan)\
**Post date:** [23 August 2004 20:12 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112/4 "2004-08-23T20:12:05Z")

</div>

Excerpts (reformatted) from Roeland Moors's mail of 20 Aug 2004 (EDT):

> \> Here's some code that hangs on the read for me:  
> \>  
> \> PROC\_FILE = "/proc/acpi/battery/BAT1/state" # freezes  
> \> #PROC\_FILE = "/proc/acpi/power\_resource/PFAN/state" # doesn't freeze
> 
> I have the same beavior with these files:  
> PROC\_FILE = "/proc/acpi/battery/BAT0/state" # freezes  
> PROC\_FILE = "/proc/acpi/power\_resource/PRCF/state" # doesn't freeze
> 
> I can't explain it 🙂

So, guys, is this a bug in Ruby, or what?

(details: ruby 1.8.1 (2004-02-03) [i386-linux] and Linux kernel 2.6.8.1)

> **···**
>
> --  
> William \<wmorgan-ruby-talk@masanjin.net\>

---

<div class="post-metadata">

**Author:** ![William\_Morgan](https://avatars.discourse-cdn.com/v4/letter/w/b77776/32.png) [@William\_Morgan](https://rubytalk.org/u/William_Morgan)\
**Post date:** [26 August 2004 23:36 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112/5 "2004-08-26T23:36:59Z")

</div>

Excerpts (reformatted) from Martin Pirker's mail of 23 Aug 2004 (EDT):

> back when I was starting in Ruby one of the first small tries was...  
> (fix code first, terminal is eating some chars)

Check out [http://tdbatmon.rubyforge.org](http://tdbatmon.rubyforge.org) and you can see what I've done.  
It turns out my particular battery is pretty linear so all my  
fancy-pants prediction is wasted, but it's still kinda cool, IMO.

> **···**
>
> --  
> William \<wmorgan-ruby-talk@masanjin.net\>

---

<div class="post-metadata">

**Author:** ![Roeland\_Moors](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@Roeland\_Moors](https://rubytalk.org/u/Roeland_Moors)\
**Post date:** [23 August 2004 21:17 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112/6 "2004-08-23T21:17:08Z")

</div>

I also have the problem with these files:  
PROC\_FILE = "/proc/acpi/fan/CFAN/state" # freezes  
PROC\_FILE = "/proc/acpi/ac\_adapter/ACAD/state" # doesn't freezes

My system:  
- Linux laptop 2.6.6 #1 Wed Jul 7 20:20:50 CEST 2004 i686 GNU/Linux  
- ruby 1.8.2 (2004-07-29) [i386-linux]  
- Asus L3C  
- Debian testing

> **···**
>
> On Tue, Aug 24, 2004 at 05:12:05AM +0900, William Morgan wrote:
> 
> > Excerpts (reformatted) from Roeland Moors's mail of 20 Aug 2004 (EDT):  
> > \> \> Here's some code that hangs on the read for me:  
> > \> \>  
> > \> \> PROC\_FILE = "/proc/acpi/battery/BAT1/state" # freezes  
> > \> \> #PROC\_FILE = "/proc/acpi/power\_resource/PFAN/state" # doesn't freeze  
> > \>  
> > \> I have the same behavior with these files:  
> > \> PROC\_FILE = "/proc/acpi/battery/BAT0/state" # freezes  
> > \> PROC\_FILE = "/proc/acpi/power\_resource/PRCF/state" # doesn't freeze  
> > \>  
> > \> I can't explain it ☹
> > 
> > So, guys, is this a bug in Ruby, or what?
> > 
> > (details: ruby 1.8.1 (2004-02-03) [i386-linux] and Linux kernel 2.6.8.1)
> 
> --  
> Roeland

---

<div class="post-metadata">

**Author:** ![William\_Morgan](https://avatars.discourse-cdn.com/v4/letter/w/b77776/32.png) [@William\_Morgan](https://rubytalk.org/u/William_Morgan)\
**Post date:** [26 August 2004 23:40 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112/7 "2004-08-26T23:40:47Z")

</div>

Excerpts (reformatted) from Roeland Moors's mail of 23 Aug 2004 (EDT):

> I also have the problem with these files:  
> PROC\_FILE = "/proc/acpi/fan/CFAN/state" # freezes  
> PROC\_FILE = "/proc/acpi/ac\_adapter/ACAD/state" # doesn't freezes
> 
> My system:  
> - Linux laptop 2.6.6 #1 Wed Jul 7 20:20:50 CEST 2004 i686 GNU/Linux  
> - ruby 1.8.2 (2004-07-29) [i386-linux]  
> - Asus L3C  
> - Debian testing

The issue persists with Ruby 1.8.2 preview 2 on my i686 machine with  
kernel 2.6.8.1, so I suggest this is a real-life bug.

Is there a better way of reporting it than just announcing it here?

> **···**
>
> --  
> William \<wmorgan-ruby-talk@masanjin.net\>

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [27 August 2004 01:28 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112/8 "2004-08-27T01:28:16Z")

</div>

Hi,

At Fri, 27 Aug 2004 08:40:47 +0900,  
William Morgan wrote in [ruby-talk:110600]:

> The issue persists with Ruby 1.8.2 preview 2 on my i686 machine with  
> kernel 2.6.8.1, so I suggest this is a real-life bug.
> 
> Is there a better way of reporting it than just announcing it here?

Maybe, strace log?

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![William\_Morgan](https://avatars.discourse-cdn.com/v4/letter/w/b77776/32.png) [@William\_Morgan](https://rubytalk.org/u/William_Morgan)\
**Post date:** [27 August 2004 14:52 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112/9 "2004-08-27T14:52:08Z")

</div>

Excerpts (reformatted) from Nobuyoshi Nakada's mail of 26 Aug 2004 (EDT):

> Maybe, strace log?

Under Ruby 1.8.2 with kernel 2.6.8.1, with this code:

&nbsp;&nbsp;PROC\_FILE = "/proc/acpi/battery/BAT1/state"  
&nbsp;&nbsp;Thread.new { sleep 1000 }  
&nbsp;&nbsp;puts "=== starting to read at #{Time.now}"  
&nbsp;&nbsp;IO.foreach(PROC\_FILE) { }  
&nbsp;&nbsp;puts "=== done reading at #{Time.now}"

I get the following strace output:

&nbsp;&nbsp;[...]  
&nbsp;&nbsp;write(1, "=== starting to read at Fri Aug "..., 53=== starting to read at Fri Aug 27 10:26:58 EDT 2004  
&nbsp;&nbsp;) = 53  
&nbsp;&nbsp;sigprocmask(SIG\_BLOCK, NULL, ) = 0  
&nbsp;&nbsp;open("/proc/acpi/battery/BAT1/state", O\_RDONLY|O\_LARGEFILE) = 3  
&nbsp;&nbsp;sigprocmask(SIG\_BLOCK, NULL, ) = 0  
&nbsp;&nbsp;gettimeofday({1093616818, 153673}, NULL) = 0  
&nbsp;&nbsp;select(4, [3], , , {999, 998330}

and it just hangs on that select. If I remove the Thread.new call, I get  
this strace output:

&nbsp;&nbsp;[...]  
&nbsp;&nbsp;open("/proc/acpi/battery/BAT1/state", O\_RDONLY|O\_LARGEFILE) = 3  
&nbsp;&nbsp;sigprocmask(SIG\_BLOCK, NULL, ) = 0  
&nbsp;&nbsp;fstat64(3, {st\_mode=S\_IFREG|0444, st\_size=0, ...}) = 0  
&nbsp;&nbsp;mmap2(NULL, 4096, PROT\_READ|PROT\_WRITE, MAP\_PRIVATE|MAP\_ANONYMOUS, -1, 0) = 0x402b7000  
&nbsp;&nbsp;read(3, "present: yes\ncap"..., 1024) = 188  
&nbsp;&nbsp;read(3, "", 1024) = 0  
&nbsp;&nbsp;close(3) = 0  
&nbsp;&nbsp;munmap(0x402b7000, 4096) = 0  
&nbsp;&nbsp;gettimeofday({1093616966, 205006}, NULL) = 0  
&nbsp;&nbsp;write(1, "=== done reading at Fri Aug "..., 53=== done reading at Fri Aug 27 10:29:26 EDT 2004  
&nbsp;&nbsp;) = 53

If I change the filename to /proc/acpi/power\_resource/PFAN/state in the  
code with threads, select() is used, but it doesn't block.

So it seems like select() on reading particular files in /proc will  
block. I guess the question is, is this a kernel bug or a kernel  
feature?

> **···**
>
> --  
> William \<wmorgan-ruby-talk@masanjin.net\>

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [28 August 2004 02:14 UTC](https://rubytalk.org/t/ruby-linux-proc-acpi-deadlock/13112/10 "2004-08-28T02:14:06Z")

</div>

Hi,

At Fri, 27 Aug 2004 23:52:08 +0900,  
William Morgan wrote in [ruby-talk:110651]:

> So it seems like select() on reading particular files in /proc will  
> block. I guess the question is, is this a kernel bug or a kernel  
> feature?

Though I'm not a developer of the kernel, it sounds like a bug.

> **···**
>
> --  
> Nobu Nakada
