You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RubyThreadInfo is keyed by ManagedThreadId and never freed: every thread leaks, and freeing it naively lets threads inherit dead threads' state #126
ThreadOps.RubyThreadInfo keeps one entry per CLR thread in a static Dictionary<int, RubyThreadInfo> _mapping keyed by Thread.ManagedThreadId (Src/Libraries/Builtins/ThreadOps.cs, Lookup). Entries are never removed or replaced. That has two consequences:
A leak, today. Every thread the process ever creates — Thread.new threads, fiber worker threads, host threads that touched Ruby — keeps its RubyThreadInfo for the life of the process: the Thread object, the thread-locals dictionary, the AutoResetEvent run signal, Result/Exception, and so on. RubyThreadInfo.Threads (behind Thread.list, ThreadGroup#list, GetRubyThreads) walks all of them under the _mapping lock, so it slows down as the process ages.
A latent correctness bug. .NET recycles ManagedThreadId once a dead thread's Thread object has been collected. IronRuby only avoids that today because _mapping holds every Thread strongly. As soon as entries are dropped or made weak (the obvious leak fix), a new thread whose id was recycled would get the dead thread's RubyThreadInfo from Lookup. It would inherit that thread's locals, name, group, abort_on_exception / report_on_exception, and the fiber flags IsFiberWorker (never cleared, which hides it from Thread.list) and IsIdleFiberWorker. info.Thread would point at the dead thread, and _current would never be cached for it, because Lookup only caches when result._thread == t.
Evidence
IronRuby, 2,000 sequential threads with forced GCs: ids never repeat, because nothing is freed.
The same loop in plain C# on .NET 8 (new Thread(...).Start(); Join();, same GCs) gives threads=2000 distinct_ids=201. The runtime does reuse ids once nothing holds the Thread objects.
The same id-as-identity assumption is in RubyUtils._pendingAsyncExceptions (and, on the PR 125 follow-up branch, _nudgesInFlight). Those entries are cleared when a thread ends, so they are not affected today, but they should move together with any fix.
Suggested direction
Key the info off the Thread object instead of its id: a ConditionalWeakTable<Thread, RubyThreadInfo> for lookup. Keep a separate list of weak references, or of live infos pruned at thread end, for Threads. The [ThreadStatic] _current fast path stays as it is.
Or remove the entry at the end of RubyThreadStart and when a fiber worker retires. Then Lookup must check result._thread == t and replace a stale entry instead of returning it.
Either way, check the other ManagedThreadId-keyed tables (_pendingAsyncExceptions, _nudgesInFlight) for the same assumption.
Found while working on #125. It is not the cause of anything fixed there.
Activity
Merge pull request #164 from Largo/claude/subagents-open-issues-53733d