On Fri, Sep 25, 2009 at 7:35 PM, Nobuyoshi Nakada <nobu / ruby-lang.org> wrote:
> At Sat, 26 Sep 2009 05:18:02 +0900,
> Charles Oliver Nutter wrote in [ruby-core:25769]:
>> Functionally, this works just fine, other than the cost of us spinning
>> up a thread. But there's a larger problem: an Enumerator-created
>> thread has a full lifecycle apart from the caller's thread. As a
>> result the enumerator thread can root objects (preventing them from
>> being GCed), including the Enumerator itself.
>
> Why does the enumerator thread make the Enumerator marked?

Any objects referenced by the enumerator thread will be strongly
referenced, as you'd expect. The current implementation lives within
the Enumerator that started it, causing that Enumerator instance to be
referenced as well, which keeps it from being collected, which keeps
the thread from being killed, etc. It's mostly an implementation issue
to isolate the thread from the enumerator. I think we can do it with a
combination of weak references and finalizers, but it doesn't address
the primary problem: it's spinning up threads for every enumerator you
want to "next" over.

- Charlie