wardencommunity/warden

Rails 5 RuntimeError: can't modify frozen Hash

stevenspiel opened this issue · 11 comments

I just upgraded to Rails 5 and I'm getting an error that stems from this line:

options = env[ENV_SESSION_OPTIONS]
options[:renew] = true if options

env[ENV_SESSION_OPTIONS] is a frozen hash, and so it cannot be modified.

Using version 1.2.7 with Rails 5.0.0 and Ruby 2.3.1

Changing rack version from 2.0.2 to 2.0.1 fixed it. But there must be some other solution.

I have the same problem and @stevenspiel's solution worked. But this is not a proper solution.

md5 commented

See rack/rack#1110 for the change that freezes the DEFAULT_OPTIONS hash in Rack.

See rails/rails#26442 for the Rails 5.1 fix that avoids assigning DEFAULT_OPTIONS to request.session_options in ActionController::TestSession. The fix has been backported to the Rails 5.0 branch, but it has not been included in a 5.0 release.

@md5 Does that mean I can close out this ticket or is it still an issue?

md5 commented

@whithub I believe it's still an issue.

I think the fix for warden is to do something like this:

diff --git a/lib/warden/proxy.rb b/lib/warden/proxy.rb
index b6cc857..86697df 100644
--- a/lib/warden/proxy.rb
+++ b/lib/warden/proxy.rb
@@ -172,7 +172,7 @@ module Warden
 
       if opts[:store] != false && opts[:event] != :fetch
         options = env[ENV_SESSION_OPTIONS]
-        options[:renew] = true if options
+        env[ENV_SESSION_OPTIONS] = options.merge(renew: true) if options
         session_serializer.store(user, scope)
       end

Could this fix be merged? I'm having this issue. =(

md5 commented

@HeidiHansen There isn't a PR. I haven't actually tested my proposed fix since I haven't encountered this problem recently.

Looking at that patch again, it should probably check if the original options are frozen? and re-freeze the merged Hash.

Yeah we could use a hand creating a test case to re-produce the issue. It is proving more time consuming than we'd anticipated. @HeidiHansen @md5 if you guys have the time to get a failure for us to work with.

md5 commented

Opened #161

Thank you @md5 for all your help and patience on this issue!

Still facing the same issue when trying to migrate/initialise database.
FrozenError: can't modify frozen Hash /usr/share/rvm/gems/ruby-2.5.3/gems/activesupport-5.2.4.6/lib/active_support/deprecation/method_wrappers.rb:54:in delete'
`
On rails 5.2.4.3

Getting the following

  • export RAILS_ENV=development
  • RAILS_ENV=development
  • rails db:environment:set RAILS_ENV=development
    rails aborted!
    FrozenError: can't modify frozen Hash
    /usr/share/rvm/gems/ruby-2.5.3/gems/activesupport-5.2.4.6/lib/active_support/deprecation/method_wrappers.rb:54:in delete' /usr/share/rvm/gems/ruby-2.5.3/gems/activesupport-5.2.4.6/lib/active_support/deprecation/method_wrappers.rb:54:in deprecate_methods'
    /usr/share/rvm/gems/ruby-2.5.3/gems/rswag-api-2.13.0/lib/rswag/api.rb:32:in <module:Api>' /usr/share/rvm/gems/ruby-2.5.3/gems/rswag-api-2.13.0/lib/rswag/api.rb:6:in module:Rswag'
    /usr/share/rvm/gems/ruby-2.5.3/gems/rswag-api-2.13.0/lib/rswag/api.rb:5:in <top (required)>' /usr/share/rvm/gems/ruby-2.5.3/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in require'
    /usr/share/rvm/gems/ruby-2.5.3/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in require' /var/www/project_path/config/application.rb:18:in <top (required)>'
    /var/www/project_path/Rakefile:4:in require_relative' /var/www/project_path/Rakefile:4:in <top (required)>'
    /usr/share/rvm/gems/ruby-2.5.3/gems/railties-5.2.4.6/lib/rails/commands/rake/rake_command.rb:22:in block in perform' /usr/share/rvm/gems/ruby-2.5.3/gems/railties-5.2.4.6/lib/rails/commands/rake/rake_command.rb:20:in perform'
    /usr/share/rvm/gems/ruby-2.5.3/gems/railties-5.2.4.6/lib/rails/command.rb:48:in invoke' /usr/share/rvm/gems/ruby-2.5.3/gems/railties-5.2.4.6/lib/rails/commands.rb:18:in <top (required)>'
    /usr/share/rvm/gems/ruby-2.5.3/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in require' /usr/share/rvm/gems/ruby-2.5.3/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in require'
    /var/www/project_path/bin/rails:9:in <top (required)>' /usr/share/rvm/gems/ruby-2.5.3/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in load'
    /usr/share/rvm/gems/ruby-2.5.3/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in call' /usr/share/rvm/gems/ruby-2.5.3/gems/spring-2.1.1/lib/spring/client/command.rb:7:in call'
    /usr/share/rvm/gems/ruby-2.5.3/gems/spring-2.1.1/lib/spring/client.rb:30:in run' /usr/share/rvm/gems/ruby-2.5.3/gems/spring-2.1.1/bin/spring:49:in <top (required)>'
    /usr/share/rvm/gems/ruby-2.5.3/gems/spring-2.1.1/lib/spring/binstub.rb:11:in load' /usr/share/rvm/gems/ruby-2.5.3/gems/spring-2.1.1/lib/spring/binstub.rb:11:in <top (required)>'
    /var/www/project_path/bin/spring:15:in require' /var/www/project_path/bin/spring:15:in <top (required)>'
    bin/rails:3:in load' bin/rails:3:in '

Caused by:
LoadError: cannot load such file -- rswag-api