Skip to content

Instantly share code, notes, and snippets.

@Fryguy
Last active July 13, 2018 01:11
Show Gist options
  • Save Fryguy/447748a9b99c2d910233bcb7bd922841 to your computer and use it in GitHub Desktop.
Save Fryguy/447748a9b99c2d910233bcb7bd922841 to your computer and use it in GitHub Desktop.
Rails + Threads + constantize
  1. Create a vanilla Rails

    rails _5.0.7_ new vanilla_test --skip-yarn --skip-active-storage --skip-action --skip-sprockets --skip-spring --skip-listen --skip-coffee --skip-javascript --skip-turbolinks --skip-test --skip-system-test --skip-bootsnap
    
  2. Copy the files into the right spots

  3. Run it

    bin/rails r lock_it_up.rb
    

Interesting points:

  • bundle exec rails r doesn't seem to work
  • can't reproduce the lockup even with the slowdown thing
  • Might be related to rails/rails#24028
# app/models/concerns/foo_concern.rb
module FooConcern
def blah
end
end
# app/models/foo_model.rb
class FooModel < ApplicationRecord
# # Some slow operation to allow another thread to get in here.
# puts "#{Thread.current} Start slowdown"
# t1 = Time.now
# until Time.now >= t1 + 3
# end
# puts "#{Thread.current} End slowdown"
include FooConcern
end
puts "Start."
t1 = Thread.new do
#ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
"FooModel".constantize
#end
end
t2 = Thread.new do
#ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
"FooModel".constantize
#end
end
t1.join
t2.join
puts "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment