Skip to content

Instantly share code, notes, and snippets.

@toolmantim
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toolmantim/9597022 to your computer and use it in GitHub Desktop.
Save toolmantim/9597022 to your computer and use it in GitHub Desktop.
Rails sessions + Sinatra sessions = kaboom

A small example showing the incompatability between Rack::Session and ActionDispatch::Request::Session

To test it yourself:

git clone https://gist.github.com/9597022.git rails-sinatra-sessions
cd rails-sinatra-sessions
bundle install
bundle exec puma -e production

In another tab:

curl http://localhost:9292/

And in the Puma console you should have:

NoMethodError (undefined method `each' for #<ActionDispatch::Request::Session:0x7fb8dbe7f838 not yet loaded>):
  rack (1.5.2) lib/rack/session/abstract/id.rb:158:in `stringify_keys'
  rack (1.5.2) lib/rack/session/abstract/id.rb:95:in `update'
  rack (1.5.2) lib/rack/session/abstract/id.rb:258:in `prepare_session'
  rack (1.5.2) lib/rack/session/abstract/id.rb:224:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  rack (1.5.2) lib/rack/nulllogger.rb:9:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  sinatra (1.4.4) lib/sinatra/base.rb:180:in `call'
  sinatra (1.4.4) lib/sinatra/base.rb:2004:in `call'
  sinatra (1.4.4) lib/sinatra/base.rb:1469:in `block in call'
  sinatra (1.4.4) lib/sinatra/base.rb:1778:in `synchronize'
  sinatra (1.4.4) lib/sinatra/base.rb:1469:in `call'
  actionpack (4.0.4) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:674:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/flash.rb:241:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/cookies.rb:486:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.4) lib/active_support/callbacks.rb:373:in `_run__355040913552270865__call__callbacks'
  activesupport (4.0.4) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.4) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.4) lib/rails/rack/logger.rb:22:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.4) lib/rails/engine.rb:511:in `call'
  railties (4.0.4) lib/rails/application.rb:97:in `call'
  railties (4.0.4) lib/rails/railtie/configurable.rb:30:in `method_missing'
  puma (2.8.1) lib/puma/configuration.rb:71:in `call'
  puma (2.8.1) lib/puma/server.rb:490:in `handle_request'
  puma (2.8.1) lib/puma/server.rb:361:in `process_client'
  puma (2.8.1) lib/puma/server.rb:254:in `block in run'
  puma (2.8.1) lib/puma/thread_pool.rb:92:in `call'
  puma (2.8.1) lib/puma/thread_pool.rb:92:in `block in spawn_thread'
require "sinatra/base"
require "action_controller/railtie"
require "logger"
class SinatraApp < Sinatra::Base
enable :sessions # Disabling this will make everything AOK
get "/" do
"Hello"
end
end
# The simplest possible Rails app
class RailsApp < Rails::Application
config.logger = Rails.logger = Logger.new(STDOUT)
config.secret_key_base = "foo"
routes.draw do
# We mount the Sinatra app here, but you could mount it under any path.
#
# Only requests to the Sinatra app show the error.
get "/" => SinatraApp
end
end
run RailsApp
source "https://rubygems.org/"
gem "rails", "~> 4.0"
gem "sinatra", "~> 1.4"
gem "puma"
@srosa
Copy link

srosa commented Apr 16, 2014

Hey toolmantim!

I'm stuck with the same error. Did you find a way of preventing the Rails app from crashing, and still using the session in the Sinatra app?

@abacha
Copy link

abacha commented Apr 29, 2014

+1

@pboling
Copy link

pboling commented Jun 21, 2014

Just hit this exact error in the test-unit suite for a Grape API (Rack) mounted in a Rails 4.1.1 app (in routes.rb). Source: https://github.com/pboling/x-cascade_header_rails/tree/rails4

Just run bin/rake test to repro the error. Looking for a fix now.

@pboling
Copy link

pboling commented Jun 23, 2014

@srosa, @abacha, @toolmantim
I have reported this as a bug on the rails issue tracker:
rails/rails#15843

@mwindholtz
Copy link

+1

@jtoy
Copy link

jtoy commented Sep 13, 2014

+1
did anyone get around this?

@abhinandankothari
Copy link

+1 Rails 4.2

@mperham
Copy link

mperham commented Jul 28, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment