From: Ammar Ali Date: 2010-12-07T11:24:55+09:00 Subject: Re: embedding ruby - segfault after initialization when running rb_gc_start?? On Tue, Dec 7, 2010 at 3:26 AM, Marc Weber wrote: > Excerpts from Ammar Ali's message of Tue Dec 07 01:32:12 +0100 2010: >> Is this a multithreaded environment? If so, it sounds vaguely like the >> problem discussed in the following issue: >>  http://redmine.ruby-lang.org/issues/show/2294 > > Wow. I can reproduce the bug by running the initialization code within a > pthread easily now. > > Does anybody know which is the correct solution? > > I'm protecting ruby by using mutexes - so only one thread will be > evaluating ruby code at any given time. Mutexes won't help with this problem. The issue here is the GC is not able to tell where the top of the stack is. If possible, the best solution is to initialize ruby from main(), that way the GC can figure out where the stack starts. If that's not possible, as in the case of a dynamically loadable module, the patch submitted by Suraj Kurapati in the issue above is worth trying out. Even though it is not guaranteed to work, it worked for some. The patch basically adds a new function to ruby that allows the stack bounds to be updated at runtime. See the fifth note, by Roman Shterenzon, for how to get the stack bounds of a pthread. Regards, Ammar