I stumbled upon the following article:
http://burgestrand.se/articles/asynchronous-callbacks-in-ruby-c-extensions.html

I'm wondering if that isn't the issue here. I took a quick look through the generated SWIG code and I don't see any Ruby threads being used for the callback.

---
(It looks like I accidentally replied directly to Klaus rather than the list earlier, so here's a quick catch up:)

I'm using Ruby 1.9.3p448.

I'm already using rb_thread_blocking_region() when making calls from Ruby into native code for long operations. However,I'm not sure why rb_thread_blocking_region() is relevant in this particular case. I'm calling from native code *into* Ruby, not the other way around. That is, I don't have a GIL to release at all.

I've created a sample here: https://dl.dropboxusercontent.com/u/2406449/Swig/SwigRubyHang.7z

It includes a test.rb script, which creates an instance of the SWIG Hang class and then sets a director via set_event_handler().

set_event_handler() creates a native thread (using C++11) that calls back in to Ruby every 2 seconds. As soon as the first callback has been executed, however, the Ruby VM hangs and takes no further user input. Interestingly, SWIG is still able to continue executing callbacks; it's just that all other Ruby execution has stopped (I can't even Ctrl+C the process)




On Wed, Oct 9, 2013 at 11:16 AM, Klaus Kaempf <kkaempf@suse.de> wrote:
* Soren Dreijer <dreijer+list@echobit.net> [Oct 09. 2013 18:50]:
>
> The call correctly comes in to Ruby. However, once the call returns, the
> Ruby VM appears to be in a funky state and I'm no longer able to execute
> other commands on the console. Interestingly, the C++ code is still able to
> call in to Ruby via the interface; it's just like everything else in the
> Ruby VM has stopped working.

Which Ruby version are you using ?

>
> Am I misunderstanding how directors work in Ruby? Is it perhaps wrong for
> me to execute the callback on a non-Ruby thread?

Internal thread handling changed between Ruby 1.8 and 1.9. Ruby 2.0
changed the interface once more.

For Ruby 1.9 'rb_thread_blocking_region' is the magic function to call.
See e.g.
http://www.spacevatican.org/2012/7/5/whos-afraid-of-the-big-bad-lock/
for more information.

See https://bugs.ruby-lang.org/issues/8660 for Ruby 2.0

Hth,

Klaus
--
SUSE LINUX Products GmbH, GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer, HRB 16746 (AG Nürnberg)
Maxfeldstraße 5, 90409 Nürnberg, Germany