Ruby 2.0 with tk 8.5 on Raspberry Pi

I’m building ruby-2.0.0-p0 on a Raspberry Pi running Raspbian wheezy,
which essentially is a debian linux variant for the armv6 and Pi. I also
need to have tk 8.5 with it… but I have run into a really strange
problem.

This is how it’s built:

sudo apt-get install tcl8.5-dev
sudo apt-get install tk8.5-dev
pushd /usr/lib/
sudo ln -s tcl8.5/tclConfig.sh ./
sudo ln -s tk8.5/tkConfig.sh ./
popd
tar xzf ruby-2.0.0-p0.tar.gz
cd ruby-2.0.0-p0
./configure --disable-install-doc --prefix=/usr/local/ruby2
make
sudo make install

When I try out “irb” and some simple scripts, the ruby itself seems to
work fine. If I do “wish” that seems to work too. The configure and make
output doesn’t show any problems as far as I can see. But when I do
“require ‘tk’” it just hangs! Hitting ctrl-C shows that it’s hanging
(or looping) in this code:

check a Tcl/Tk interpreter is initialized

until INTERP_THREAD[:interp]

Thread.pass

INTERP_THREAD.run
end

That’s line 1327 in tk.rb (the .run call). Then “exit” doesn’t work, I
have to do a kill -9 to make irb terminate.

And now it gets really weird… In an attempt to figure out what it was
doing I attached an strace to the process… but then it suddenly starts
running as it should, and seems to work fine!

Now I can hardly run it with an strace on it all the time, so it doesn’t
help unfortunately. :stuck_out_tongue: But perhaps it’s clue for someone about what’s
going on?

I have never come across this kind of behavior before, and I have some
experience. (25+ years as a systems programmer on unix/linux). I have
built different ruby versions on countless linux versions without any
problems. I’m at a loss here, no idea what to do about this.

Btw, I have of course tried the Raspberry Pi forums, but no response
yet…

I finally managed to find a way to work around this problem. I hesitate
to call is a “solution”, since I still don’t know exactly why it behaves
this way, or why the workaround works…

It’s clear that there was something fishy going on with the thread
scheduling, so I’ve done lots of testing with threads in ruby on the
RPi, but couldn’t find anything strange there. It seems the issue,
whatever it is, is at least partly in the tk library, or tk binding in
ruby.

Reading tk.rb finally made me suspect the RUN_EVENTLOOP_ON_MAIN_THREAD
setting which seems to be set differently depending on platform.

And lo and behold, changing line 1239 in tk.rb:

RUN_EVENTLOOP_ON_MAIN_THREAD = false

into

RUN_EVENTLOOP_ON_MAIN_THREAD = true

made it work! I know this isn’t the the correct fix, but will do for me
now; this has been a major show-stopper for me. (It really should detect
the Raspberry Pi platform and set it for this only, but I’m not going to
bother about that now.)

One question remains: Why does this has to be different on Raspberry Pi
running wheezy? It’s not needed on Ubuntu or Mac OS X (unless you’re
running and older tk version. Is it something with the tk build on RPi?
Or the pthread libraries on RPi? Or is it something about the arm
architecture?