From: nobu@... Date: 2006-04-16T15:32:38+09:00 Subject: Re: understanding "[BUG] cross-thread violation on rb_gc()" Hi, At Sun, 16 Apr 2006 10:52:55 +0900, Suraj N. Kurapati wrote in [ruby-talk:188996]: > Now, suppose the embedded Ruby interpreter inside ruby_run() invokes > the garbage collector by calling the rb_gc() function. What happens? > > > There are two situations: > > 1. The garbage collector collects garbage created *only* by the > embedded Ruby interpreter inside ruby_run(). It ignores the garbage > created by the C program (remember that it created a Ruby array of > Ruby strings). No differences between objects created inside ruby_run() and others. The ruby interpreter which is consisted from the core and many class libraries use each other, is also just one of programs using "embedded" interpreter. > 2. The garbage collector tries to collect the Ruby array of Ruby > strings created by the C program. But wait a minute... the C program > which created that garbage is running in a *different* thread than > the embedded Ruby interpreter inside ruby_run()! The point is where the object is stored. Ruby GC scans the current machine stack to detect live objects, from the bottom of it through the current top of it. Note that the bottom is initialized the position of the stack when ruby_init() is called. If you run it from another native thread other than the initial thread, the stack pointer will point another stack, and GC will run out "gaps" between those stacks. > That's my understanding of the error message. If I am correct, then > a solution to this problem would be to prohibit the C program from > using Ruby's C API functions. That way, the C program never creates > any Ruby objects which can be garbage collected by the embedded Ruby > interpreter that runs inside another thread. Incorrect. You have to ensure rb_gc() will run only in the initial thread, and all objects created in your program will be refered from somewhere other than the main stack. -- Nobu Nakada