Embedding ruby interpreter

S

Steve Hart

Hi
We are looking at upgrading our current version of ruby 1.8.7 to latest
1.9.2-p0, We have been running ruby embedded within a pthread with a
swigged up interface for about 7 years and it has worked fine. On
changing to 1.9 we get a crash on initialistaiion the first time the gc
is called.
This occurs in mark_current_machine_context

Under 1.8.7 we initialised the ruby thread thus

int ScriptThread::run()
{
int addr;
ruby_init();
Init_stack((void *)&addr);
ruby_script("embedded");
ruby_init_loadpath();
Init_gm(); // swigged up interface
.....
}

Under 1.9 I changed this to:

RUBY_GLOBAL_SETUP

int ScriptThread::run()
{
//ruby_sysinit(&argc, &argv); // commented out, no args
available
{
RUBY_INIT_STACK;
ruby_init();
}
Init_gm();
.....
}
Note that our thread is created thru QT using pthreads. We also compile
1.8.7 without pthread support. Our interpreter runs exclusively in this
thread and all VALUE's created in C are properly wrapped and marked.

I have read the threads from
http://www.ruby-forum.com/topic/144747
http://redmine.ruby-lang.org/issues/show/2294
From these it would appear that

a) The ruby initialisation must be done in main
Is this a change from 1.8.7? What if we are dynamically loaded and have
no access to main?
b) The thread should be created using rb_thread_create
Is this true? If so, why?
c) disabling pthread support when compiling ruby has been deprecated.
What does this mean? - Does ruby use threads itself?
I briefly tried to disable the gc and then got a crash
d) Should we apply the patch from 2294?

What is the correct way to do this?
I would greatly appreciate any help anyone could give.
 
R

Robert Klemme

Hi
We are looking at upgrading our current version of ruby 1.8.7 to latest
1.9.2-p0, We have been running ruby embedded within a pthread with a
swigged up interface for about 7 years and it has worked fine. On
changing to 1.9 we get a crash on initialistaiion the first time the gc
is called.
This occurs in mark_current_machine_context

Under 1.8.7 we initialised the ruby thread thus

int ScriptThread::run()
{
=A0 =A0 =A0 =A0int =A0 =A0addr;
=A0 =A0 =A0 =A0ruby_init();
=A0 =A0 =A0 =A0Init_stack((void *)&addr);
=A0 =A0 =A0 =A0ruby_script("embedded");
=A0 =A0 =A0 =A0ruby_init_loadpath();
=A0 =A0 =A0 =A0Init_gm(); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // swig= ged up interface
=A0 =A0 =A0 =A0.....
}

Under 1.9 I changed this to:

RUBY_GLOBAL_SETUP

int ScriptThread::run()
{
=A0 =A0 =A0 =A0//ruby_sysinit(&argc, &argv); // commented out, no args
available
=A0 =A0 =A0 =A0{
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0RUBY_INIT_STACK;
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ruby_init();
=A0 =A0 =A0 =A0}
=A0 =A0 =A0 =A0Init_gm();
=A0 =A0 =A0 =A0.....
}
Note that our thread is created thru QT using pthreads. We also compile
1.8.7 without pthread support. Our interpreter runs exclusively in this
thread and all VALUE's created in C are properly wrapped and marked.

This is no longer true with 1.9. 1.9 uses native threads (although
there's still the GIL which slows it somewhat down). I do not know
how best to interact but I guess you could find hints by looking at
thread creation code.
I have read the threads from
http://www.ruby-forum.com/topic/144747
http://redmine.ruby-lang.org/issues/show/2294
From these it would appear that

a) The ruby initialisation must be done in main
Is this a change from 1.8.7? What if we are dynamically loaded and have
no access to main?
b) The thread should be created using rb_thread_create
Is this true? If so, why?
c) disabling pthread support when compiling ruby has been deprecated.
What does this mean? - Does ruby use threads itself?

See above.
I briefly tried to disable the gc and then got a crash
d) Should we apply the patch from 2294?

What is the correct way to do this?
I would greatly appreciate any help anyone could give.

Others can certainly give more detailed advice but my quick heads up
might send you in the right direction.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
A

Ammar Ali

a) The ruby initialisation must be done in main
Is this a change from 1.8.7? What if we are dynamically loaded and have
no access to main?
b) The thread should be created using rb_thread_create
Is this true? If so, why?
c) disabling pthread support when compiling ruby has been deprecated.
What does this mean? - Does ruby use threads itself?
I briefly tried to disable the gc and then got a crash
d) Should we apply the patch from 2294?

Unfortunately, I never found a solution. In my situation the problem
was exacerbated by the fact that the main program loaded each plug-in
into it's own thread. FWIW, I ended up, due to timing constraints,
converting the dynamic module into an "agent" that communicated with a
separate process, a ruby server written in ruby. Not for every
situation, pretty, or efficient, but it bought me some time.

When I tired the patch in 2294, it delayed the crash but didn't
eliminate it. I see that Suraj has made a few updates since I last
tried it. It is worth a try since it worked for some people. I hope to
try the latest patch soon and update the issue with my findings.

Regards,
Ammar
 

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,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top