Bug #21354
closedSymbol#to_proc is not ractor safe
Description
There is caching in Symbol#to_proc (rb_sym_to_proc) that makes the assumption that we're always in the main ractor. With multiple ractors, this caching logic is not ractor-safe in that cached procs created in one ractor can leak into other ractors. In a debug build, this results in a ractor_confirm_belonging assertion failure.
Ruby code that reproduces the issue:
  :inspect.to_proc
  Ractor.new do
    :inspect.to_proc
  end.take
  
        
          
          Updated by luke-gru (Luke Gruber) 6 months ago
          
          ยท Edited
        
        
      
      PR here: https://github.com/ruby/ruby/pull/13380
We are looking at how to make the cache ractor safe but it will be done in a separate PR.
        
          
          Updated by nobu (Nobuyoshi Nakada) 6 months ago
          
          
        
        
      
      - Assignee set to ko1 (Koichi Sasada)
 - Backport changed from 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN to 3.2: REQUIRED, 3.3: REQUIRED, 3.4: REQUIRED
 
        
          
          Updated by Anonymous 6 months ago
          
          
        
        
      
      - Status changed from Open to Closed
 
Applied in changeset git|f6cbf499bc98b851034fffb49fcbb59d495f6f7b.
Fix Symbol#to_proc (rb_sym_to_proc) to be ractor safe
In non-main ractors, don't use sym_proc_cache. It is not thread-safe
to add to this array without a lock and also it leaks procs from one
ractor to another. Instead, we create a new proc each time. If this
results in poor performance we can come up with a solution later.
Fixes [Bug #21354]
        
          
          Updated by byroot (Jean Boussier) 6 months ago
          
          
        
        
      
      - Assignee changed from ko1 (Koichi Sasada) to ractor
 
        
          
          Updated by nagachika (Tomoyuki Chikanaga) 5 months ago
          
          
        
        
      
      - Backport changed from 3.2: REQUIRED, 3.3: REQUIRED, 3.4: REQUIRED to 3.2: REQUIRED, 3.3: DONE, 3.4: REQUIRED
 
ruby_3_3 d5c8fd2043f4425c3fe2a87558dfbd80ebff9911 merged revision(s) f6cbf499bc98b851034fffb49fcbb59d495f6f7b.
        
          
          Updated by k0kubun (Takashi Kokubun) 4 months ago
          
          
        
        
      
      - Backport changed from 3.2: REQUIRED, 3.3: DONE, 3.4: REQUIRED to 3.2: REQUIRED, 3.3: DONE, 3.4: DONE
 
ruby_3_4 82e05dc945e3e2c5ab22be661f6caf6c7436461f merged revision(s) f6cbf499bc98b851034fffb49fcbb59d495f6f7b.