Ruby 1.9.1, Threads and "[BUG] The handle is invalid."

J

John Briggs

Hi!

I am new to this forum so this is my first post.

I have Ruby 1.9.1 on Windows 7 and I am trying to make a program that
will do some jobs in threads.
A simplified version of what I want to do would look like this:

$threads_alowed = 700;
$threads = []

$threads_alowed.times{|thread_id|
$threads[thread_id] = {'thread' => Thread.new{}}
puts "thID #{thread_id} initialized"
}

def do_something(thread_id)
wait = rand(300)
print "\nThread #{thread_id} is doing something for #{wait} seconds"
sleep wait
end

while true do
$threads_alowed.times{|thread_id|
status = $threads[thread_id]['thread'].status
if status == false or status == nil
$threads[thread_id]['thread'] =
Thread.new(thread_id){do_something(thread_id)}
print ","
end
}
print "."
sleep 0.03
end

It seems to work but often I get the following error:


[BUG] The handle is invalid.

ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]

-- control frame ----------
---------------------------
-- Ruby level backtrace
information-----------------------------------------

[NOTE]
You may encounter a bug of Ruby interpreter. Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html


This application has requested the Runtime to terminate it in an unusual
way.
Please contact the application's support team for more information.


and all the script crashes.

I tried to run the same script on CentOS and it is working without
problems, but I can not stick to just one OS. I need this script to work
on every OS that Ruby runs on.

So my question is: Does anybody know how to get rid of this error on
windows or maybe any workaround and what this error mean?
 
C

Chuck Remes

It seems to work but often I get the following error:


[BUG] The handle is invalid.

ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]

-- control frame ----------
---------------------------
-- Ruby level backtrace
information-----------------------------------------

[NOTE]
You may encounter a bug of Ruby interpreter. Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html


This application has requested the Runtime to terminate it in an unusual
way.
Please contact the application's support team for more information.


and all the script crashes.

I tried to run the same script on CentOS and it is working without
problems, but I can not stick to just one OS. I need this script to work
on every OS that Ruby runs on.

So my question is: Does anybody know how to get rid of this error on
windows or maybe any workaround and what this error mean?

You might want to try the release from ruby-lang.org [1]. It is built differently than the version you are running (I assume you got it from rubyinstaller.org). The Ruby from ruby-lang is built using Microsoft's development tools and calls into Win32 APIs directly. The version from rubyinstaller.org is built against MinGW which provides a POSIX emulation layer on top of most Windows APIs. As a result, the mingw version exercises more UNIX code paths than it does Windows code paths in the 1.9.x source.

That said, you should still submit your code example as a bug report against the 1.9.x codebase.

cr

[1] http://www.ruby-lang.org/en/downloads/
 
J

John Briggs

Chuck said:
You might want to try the release from ruby-lang.org [1]. It is built
differently than the version you are running (I assume you got it from
rubyinstaller.org).

Thanks for your reply. Going to give a try right now.
 
C

Chuck Remes

Unfortunately I get the same crash with mswin32 build. :(

Then it's a genuine bug that you should report.

You might also want to give IronRuby and JRuby a try. Both run on Windows.

cr
 
L

Luis Lavena

[...]
So my question is: Does anybody know how to get rid of this error on
windows or maybe any workaround and what this error mean?

You might want to try the release from ruby-lang.org [1]. It is built differently than the version you are running (I assume you got it from rubyinstaller.org). The Ruby from ruby-lang is built using Microsoft's developmenttools and calls into Win32 APIs directly. The version from rubyinstaller.org is built against MinGW which provides a POSIX emulation layer on top of most Windows APIs. As a result, the mingw version exercises more UNIX code paths than it does Windows code paths in the 1.9.x source.

Your statements are incorrect.

You're confusing MinGW with cygwin.

MinGW provides minimal set of GNU tools for Windows (that is the
meaning of MinGW acronym)

It links and compiles against Win32API, there is no emulation layer of
POSIX functionality.

Please research before state that type of answers as it might confuse
and alienate users of Ruby on Windows platform.

As for the original reporter of the issue:

You're trying to "print" to STDOUT from different threads, which will
produce a race conditions and depending other operations you're
performing, highly likely will crash.

Add to that, you're trying to spawn 700 threads, which spawned quickly
will generate overhead and highly likely you're exhausting resources
in the process (since you don't get a backtrace of your test script)

But more concrete, exhausting of resources could be associated with
available descriptors that Ruby 1.9 has been compiled for.
 
J

John Briggs

Luis said:
As for the original reporter of the issue:

You're trying to "print" to STDOUT from different threads, which will
produce a race conditions and depending other operations you're
performing, highly likely will crash.

Add to that, you're trying to spawn 700 threads, which spawned quickly
will generate overhead and highly likely you're exhausting resources
in the process (since you don't get a backtrace of your test script)

But more concrete, exhausting of resources could be associated with
available descriptors that Ruby 1.9 has been compiled for.

Actually I get the crash for both 700 threads and 10 threads. Initially
I wrote the script with 25 threads and the 700 one remained from the
last tests.
Also I tried to put delays between thread spawning but couldn't get rid
of the crash.
And one more thing: I tried to run this script on JRuby and it works
without problems even with 700 threads and without any delays, so the
problem doesn't seem to be in threads amount or resources exhausting.
 
C

Chuck Remes

[...]
So my question is: Does anybody know how to get rid of this error on
windows or maybe any workaround and what this error mean?

You might want to try the release from ruby-lang.org [1]. It is built differently than the version you are running (I assume you got it from rubyinstaller.org). The Ruby from ruby-lang is built using Microsoft's development tools and calls into Win32 APIs directly. The version from rubyinstaller.org is built against MinGW which provides a POSIX emulation layer on top of most Windows APIs. As a result, the mingw version exercises more UNIX code paths than it does Windows code paths in the 1.9.x source.

Your statements are incorrect.

You're confusing MinGW with cygwin.

MinGW provides minimal set of GNU tools for Windows (that is the
meaning of MinGW acronym)

It links and compiles against Win32API, there is no emulation layer of
POSIX functionality.

Please research before state that type of answers as it might confuse
and alienate users of Ruby on Windows platform.

Oops, my mistake. Now I know the correct answer for next time.

cr
 
J

John Briggs

Also able to reach the crash with:

700.times{|i|
Thread.new{}
puts i
}

and even with:

700.times{
Thread.new{}
puts "."
}


but not with:

700.times{
Thread.new{}
}


[Note: The script crashes even when 0 < i < 10]
 
L

Luis Lavena

Actually I get the crash for both 700 threads and 10 threads. Initially
I wrote the script with 25 threads and the 700 one remained from the
last tests.
Also I tried to put delays between thread spawning but couldn't get rid
of the crash.
And one more thing: I tried to run this script on JRuby and it works
without problems even with 700 threads and without any delays, so the
problem doesn't seem to be in threads amount or resources exhausting.

Invalid handle might be associated with handles allocated by Ruby to
keep track of the native threads.

JRuby implements native threads but in a different way than Ruby 1.9

It will be worth waiting for answers from Ruby-Core regarding your bug
report for Ruby 1.9 project.
 

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

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top