Skip to content

Commit

Permalink
Fallback to the legacy id when the new id is not found
Browse files Browse the repository at this point in the history
This will avoid all session to be invalidated.
  • Loading branch information
rafaelfranca authored and tenderlove committed Dec 17, 2019
1 parent b9565a9 commit 9fe40c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/rack/session/memcache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def generate_sid

def get_session(env, sid)
with_lock(env) do
unless sid and session = @pool.get(sid.private_id)
unless sid and session = get_session_with_fallback(sid)
sid, session = generate_sid, {}
unless /^STORED/ =~ @pool.add(sid.private_id, session)
raise "Session collision on '#{sid.inspect}'"
Expand Down Expand Up @@ -88,6 +88,11 @@ def with_lock(env)
@mutex.unlock if @mutex.locked?
end

private

def get_session_with_fallback(sid)
@pool.get(sid.private_id) || @pool.get(sid.public_id)
end
end
end
end
18 changes: 18 additions & 0 deletions test/spec_session_memcache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,24 @@
ses1.should.not.equal ses0
end

it "can read the session with the legacy id" do
pool = Rack::Session::Memcache.new(incrementor)
req = Rack::MockRequest.new(pool)

res0 = req.get("/")
cookie = res0["Set-Cookie"]
session_id = Rack::Session::SessionId.new cookie[session_match, 1]
ses0 = pool.pool.get(session_id.private_id, true)
pool.pool.set(session_id.public_id, ses0, 0, true)
pool.pool.delete(session_id.private_id)


res1 = req.get("/", "HTTP_COOKIE" => cookie)
res1["Set-Cookie"].must_be_nil
res1.body.must_equal '{"counter"=>2}'
pool.pool.get(session_id.private_id, true).wont_be_nil
end

# anyone know how to do this better?
it "cleanly merges sessions when multithreaded" do
unless $DEBUG
Expand Down

0 comments on commit 9fe40c6

Please sign in to comment.