On 9/15/06, Geff Geff <boing / boing.com> wrote:
> All,
>
> I'm certainly not a ruby expert by any means.  I'm trying to come up to
> speed as fast as possible on ruby and rails.  My last few months have
> been reading, coding, reading, more reading, more coding, etc.  :)  So I
> was doing some coding to try to come up to speed on ruby.  I really
> think it's one of the most phenominal languages I've ever found.  I
> never got into java, too verbose.  That said I'd like to move on to the
> question.
>
> At this point, I'm trying to track down some sort of memory leak.  In my
> vast amount of digging through google and writing test code, I came
> across the following interesting behavior.  It seems that threads do not
> have a parent child relationships.  So when one kills a thread, ruby
> doesn't actually kill any threads that were started inside that thread.
> To clarify, consider the following:
>
> irb(main):001:0> x=nil ; y = Thread.start { x = Thread.start { sleep 0.5
> until false }  ; sleep 0.5 until false }
> => #<Thread:0xb7abe20c run>
> irb(main):002:0> x.status
> => "sleep"
> irb(main):003:0> y.kill
> => #<Thread:0xb7abe20c dead>
[There are lines missing here...]
> irb(main):007:0> x.status
> => "sleep"
> irb(main):008:0>
>
> Clearly by killing, what I would call the "parent thread", did not kill
> the child thread.  And this would be okay, I think, except that the way
> the timeout class works is by starting a thread and killing it if it
> doesn't complete in the right amount of time.  So if I were to wrap
> anything in "timeout() {}" it's possible that something else calls
> timeout inside that and then the thread is still alive, until some later
> time.  Is this normal?

Reading the timeout code I see that it creates a watcher thread to
provide the timeout.  The block is executed on the current thread and
interruption is provided by raising an exception (from the watcher
tread) when the timeout expires.  At the end of the method there is an
ensure to kill the watcher thread in any case so it would not
encounter the case you describe.

> Can this cause memory leak?

Due do what I described in the previous paragraph nested timeouts
won't cause any kind of leak.

> Am I confused? :)

Yes, I believe you are confused.

By the way, reading the code of the methods you have doubts about can
be a good way to clarify something.

> Thanks much for your help and feedback,
>
> Geff
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


-- 
I'm trying to launch the internet; so I open a terminal and go
"percent sign 'Internet'" at the prompt and it doesn't work. What
gives??!! -- random troll