This project is archived and is in readonly mode.

#1816 ✓committed
Charles Oliver Nutter

constant_watch_stack must be protected by a mutex

Reported by Charles Oliver Nutter | January 29th, 2009 @ 06:14 AM | in 2.x

For I investigated what looked like an Array being concurrently modified by multiple threads. The backtrace led me to active_support/dependencies.rb and the constant_watch_stack. This stack is an array updated during requires. Concurrent requires can cause the array to become corrupted under parallel Ruby implementations like JRuby.

The user provided the following script to reproduce it. It's not consistent, but with enough runs it does fail under JRuby:

require 'rubygems'
require 'active_record'
require 'thread'
require 'rexml/encoding'

#This will ensure activesupport will override Kernel.require
class MyTable < ActiveRecord::Base
  set_table_name "table"

#build a test case with enough iterations to reproduce the problem.
#Since it's a race condition it'll occur randomly throughout the test
#if you can't reproduce the problem on your computer, try to increase the number of iterations
th = []
100.times do
  th << {
    #this is what REXML is doing in $ruby_home/lib/ruby/1.8/rexml/encoding.rb (line 48)
    require 'rexml/encodings/UTF-8.rb' #active_support will try to load the file
th.each do |t|

I whipped together a patch that adds a mutex alongside constant_watch_stack and uses it to synchronize all mutations and iterations over the stack. The patch is at and is against rails master@e6493eb9b76de73afef2706493efd090dfff4ecc.

Feel free to contact me with questions.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket


Referenced by