rails: Session options are nil. Unable to add sessions and automatic connection switching to an api_only app.
I recently updated sidekiq and I need to use sessions in my api_only rails app as per 6.2.1. I’ve tried to add them back in per the docs, but it doesn’t work. If I set config.api_only = true
to config.api_only = false
then it all works as expected. I opened an issue in the sidekiq repo and I was directed here.
Steps to reproduce
config/application.rb
config.api_only = true
# This also configures session_options for use below
config.session_store :cookie_store, key: '_interslice_session'
# Required for all session management (regardless of session_store)
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore, key: '_interslice_session'
config.middleware.insert_after(ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, key: '_interslice_session')
config.middleware.use config.session_store, config.session_options
Expected behavior
It should load the session cookie config?
Actual behavior
It looks like the sessions aren’t working and the web part of my app won’t boot.
F, [2021-04-12T09:20:34.579998 #33] FATAL -- : [389c9875-8447-43d7-9e18-bde804be0f4a]
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] NoMethodError (undefined method `[]=' for nil:NilClass):
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a]
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/request/session.rb:235:in `load!'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/request/session.rb:230:in `load_for_write!'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/request/session.rb:216:in `merge!'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/request/session.rb:19:in `create'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/session/abstract_store.rb:71:in `prepare_session'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] rack (2.2.3) lib/rack/session/abstract/id.rb:265:in `context'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/cookies.rb:689:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] rack (2.2.3) lib/rack/static.rb:161:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] rack (2.2.3) lib/rack/builder.rb:244:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] sidekiq (6.2.1) lib/sidekiq/web.rb:108:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] sidekiq (6.2.1) lib/sidekiq/web.rb:113:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/routing/mapper.rb:20:in `block in <class:Constraints>'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/routing/mapper.rb:49:in `serve'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/journey/router.rb:50:in `block in serve'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/journey/router.rb:32:in `each'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/journey/router.rb:32:in `serve'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/routing/route_set.rb:842:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/middleware/database_selector.rb:53:in `block in call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/middleware/database_selector/resolver.rb:63:in `block (2 levels) in read_from_replica'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activesupport (6.1.3.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/middleware/database_selector/resolver.rb:62:in `block in read_from_replica'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/connection_handling.rb:383:in `block (2 levels) in with_role_and_shard'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:1025:in `while_preventing_writes'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/connection_handling.rb:381:in `block in with_role_and_shard'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/connection_handling.rb:398:in `swap_connection_handler'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/connection_handling.rb:373:in `with_handler'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/connection_handling.rb:380:in `with_role_and_shard'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/connection_handling.rb:175:in `connected_to'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/middleware/database_selector/resolver.rb:61:in `read_from_replica'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/middleware/database_selector/resolver.rb:39:in `read'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/middleware/database_selector.rb:63:in `select_database'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activerecord (6.1.3.1) lib/active_record/middleware/database_selector.rb:52:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] rack (2.2.3) lib/rack/etag.rb:27:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] rack (2.2.3) lib/rack/conditional_get.rb:27:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] rack (2.2.3) lib/rack/head.rb:12:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activesupport (6.1.3.1) lib/active_support/callbacks.rb:98:in `run_callbacks'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] appsignal (3.0.2) lib/appsignal/rack/rails_instrumentation.rb:32:in `call_with_appsignal_monitoring'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] appsignal (3.0.2) lib/appsignal/rack/rails_instrumentation.rb:17:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] railties (6.1.3.1) lib/rails/rack/logger.rb:37:in `call_app'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] railties (6.1.3.1) lib/rails/rack/logger.rb:26:in `block in call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activesupport (6.1.3.1) lib/active_support/tagged_logging.rb:99:in `block in tagged'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activesupport (6.1.3.1) lib/active_support/tagged_logging.rb:37:in `tagged'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activesupport (6.1.3.1) lib/active_support/tagged_logging.rb:99:in `tagged'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] railties (6.1.3.1) lib/rails/rack/logger.rb:26:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/request_id.rb:26:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] rack (2.2.3) lib/rack/runtime.rb:22:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] activesupport (6.1.3.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] actionpack (6.1.3.1) lib/action_dispatch/middleware/host_authorization.rb:92:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] rack-cors (1.1.1) lib/rack/cors.rb:100:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] railties (6.1.3.1) lib/rails/engine.rb:539:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] puma (5.2.2) lib/puma/configuration.rb:248:in `call'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] puma (5.2.2) lib/puma/request.rb:76:in `block in handle_request'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] puma (5.2.2) lib/puma/thread_pool.rb:337:in `with_force_shutdown'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] puma (5.2.2) lib/puma/request.rb:75:in `handle_request'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] puma (5.2.2) lib/puma/server.rb:431:in `process_client'
web-74b87b954b-tqv78 — [389c9875-8447-43d7-9e18-bde804be0f4a] puma (5.2.2) lib/puma/thread_pool.rb:145:in `block in spawn_thread'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] rack (2.2.3) lib/rack/head.rb:12:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] actionpack (6.1.3.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] activesupport (6.1.3.1) lib/active_support/callbacks.rb:98:in `run_callbacks'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] actionpack (6.1.3.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] actionpack (6.1.3.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] actionpack (6.1.3.1) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] appsignal (3.0.2) lib/appsignal/rack/rails_instrumentation.rb:32:in `call_with_appsignal_monitoring'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] appsignal (3.0.2) lib/appsignal/rack/rails_instrumentation.rb:17:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] actionpack (6.1.3.1) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] actionpack (6.1.3.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] railties (6.1.3.1) lib/rails/rack/logger.rb:37:in `call_app'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] railties (6.1.3.1) lib/rails/rack/logger.rb:26:in `block in call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] activesupport (6.1.3.1) lib/active_support/tagged_logging.rb:99:in `block in tagged'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] activesupport (6.1.3.1) lib/active_support/tagged_logging.rb:37:in `tagged'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] activesupport (6.1.3.1) lib/active_support/tagged_logging.rb:99:in `tagged'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] railties (6.1.3.1) lib/rails/rack/logger.rb:26:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] actionpack (6.1.3.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] actionpack (6.1.3.1) lib/action_dispatch/middleware/request_id.rb:26:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] rack (2.2.3) lib/rack/runtime.rb:22:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] activesupport (6.1.3.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] actionpack (6.1.3.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] actionpack (6.1.3.1) lib/action_dispatch/middleware/host_authorization.rb:92:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] rack-cors (1.1.1) lib/rack/cors.rb:100:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] railties (6.1.3.1) lib/rails/engine.rb:539:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] puma (5.2.2) lib/puma/configuration.rb:248:in `call'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] puma (5.2.2) lib/puma/request.rb:76:in `block in handle_request'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] puma (5.2.2) lib/puma/thread_pool.rb:337:in `with_force_shutdown'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] puma (5.2.2) lib/puma/request.rb:75:in `handle_request'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] puma (5.2.2) lib/puma/server.rb:431:in `process_client'
web-74b87b954b-j8pbj — [564a856e-9bfb-45e9-ba16-a2fa8d4ed391] puma (5.2.2) lib/puma/thread_pool.rb:145:in `block in spawn_thread'
System configuration
Ruby version: 2.7.2 Rails version: 6.1.3.1 Sidekiq version: 6.2.1 Sidekiq Pro version: 5.2.2
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (7 by maintainers)
I had the same issue using rails 6.0.6
For me, this issue was resolved by rearranging the middleware sequence. You can print your middleware sequence by running the
rake middleware
command. With the help of the command, you can check how the middleware is arranged with theconfig.api_only = false
and how it is arranged withconfig.api_only = false
. Then try to match the sequence withconfig.api_only = false
as closely as possible.This error appeared because the Cookies middleware was called too late in the stack. The solution was to move it a few places up the stack.
This code snippet worked for me (put it in the
config/application.rb
file):@rafaelfranca I found the issue.
I created a new rails api app
rails new my_api --api --database=postgresql
These are the only changes I made.
I added session middleware and automatic database switching.
I get the same error
@Elgonzo422 before I updated sidekiq it didn’t require sessions.
This line
config.middleware.use config.session_store, config.session_options
prevents any of my web requests from succeeding, it doesn’t just fail on the sidekiq web ui but everyone of my routes. I only mention sidekiq because I need to use sessions to get the sidekiq web ui to work.Just to be clear. I can remove sidekiq from my app all together and have the following as per the rails docs.
Then I visit
/v1/account/
within my api or even just run my rspec test for this endpoint and I get the error.