Skip to content

RubyThreadInfo is keyed by ManagedThreadId and never freed: every thread leaks, and freeing it naively lets threads inherit dead threads' state #126

Closed

Description

@Largo

Summary

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:

  1. 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.
  2. 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.

ids = []
2000.times do |i|
  Thread.new { ids << System::Threading::Thread.CurrentThread.ManagedThreadId }.join
  (System::GC.Collect; System::GC.WaitForPendingFinalizers; System::GC.Collect) if i % 200 == 0
end
p threads: ids.size, distinct_ids: ids.uniq.size   # => {threads: 2000, distinct_ids: 2000}

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

  1. added a commit that references this issue on Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions