Skip to content

Commit 6a04bbf

Browse files
rafaelfrancatenderlove
authored andcommitted
Fallback to the legacy id when the new id is not found
This will avoid all session to be invalidated.
1 parent dc45a06 commit 6a04bbf

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/rack/session/memcache.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def generate_sid
4848

4949
def get_session(env, sid)
5050
with_lock(env) do
51-
unless sid and session = @pool.get(sid.private_id)
51+
unless sid and session = get_session_with_fallback(sid)
5252
sid, session = generate_sid, {}
5353
unless /^STORED/ =~ @pool.add(sid.private_id, session)
5454
raise "Session collision on '#{sid.inspect}'"
@@ -88,6 +88,11 @@ def with_lock(env)
8888
@mutex.unlock if @mutex.locked?
8989
end
9090

91+
private
92+
93+
def get_session_with_fallback(sid)
94+
@pool.get(sid.private_id) || @pool.get(sid.public_id)
95+
end
9196
end
9297
end
9398
end

test/spec_session_memcache.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,24 @@
235235
ses1.wont_equal ses0
236236
end
237237

238+
it "can read the session with the legacy id" do
239+
pool = Rack::Session::Memcache.new(incrementor)
240+
req = Rack::MockRequest.new(pool)
241+
242+
res0 = req.get("/")
243+
cookie = res0["Set-Cookie"]
244+
session_id = Rack::Session::SessionId.new cookie[session_match, 1]
245+
ses0 = pool.pool.get(session_id.private_id, true)
246+
pool.pool.set(session_id.public_id, ses0, 0, true)
247+
pool.pool.delete(session_id.private_id)
248+
249+
250+
res1 = req.get("/", "HTTP_COOKIE" => cookie)
251+
res1["Set-Cookie"].must_be_nil
252+
res1.body.must_equal '{"counter"=>2}'
253+
pool.pool.get(session_id.private_id, true).wont_be_nil
254+
end
255+
238256
# anyone know how to do this better?
239257
it "cleanly merges sessions when multithreaded" do
240258
skip unless $DEBUG

0 commit comments

Comments
 (0)