Feature request - handle regenerate race condition #1046
Unanswered
htadeusiak
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
So it's kind of a weird bug and I'm not 100% confident I know whats going on, but I have an idea and hoping you guys can help me confirm it.
Problem Statement:
I want to regenerate SID every hour while keeping the same underlying data in the Redis store. However, I also fire 2/3 requests off at the same time when I want to regenerate the SID (which is why I'm having issues).
Note: my implementation is a middleware that just checks if SID has been alive for a certain amount of time on every request. Thus if I fire off 2/3 requests and it is expired, it will try to regenerate each one.
Examples of logs with firing 2 requests off at the same time:
sid_0isExpiredfunction first and because it is expired, it callsreq.session.regenerate()sid_0and moves old data into new sessionsid_1isExpiredwith a never before seensessionID(lets saysid_2). Because its a brand new session cookie is not considered as expired and thus regenerate is not calledsid_1(which references moved data in redis)sid_2sid_1, all requests start working againSo the first issue here is at step 4. I would think req_2 should arrive with
sid_0. But it instead has a completely new SID which has no data tied to it in my store (of course). My only thought would be because I called "regenerate" just beforereq_2gets through the express-session middleware. Thus the middleware thinks that it is expired/doesn't exists and tries generating a new session. (This is the part where I'm most confused and those who know the library better can help out and explain if thats how it actually works under the hood)The second thing that comes to mind is why
req_2does not set the sessionsid_2in the browser (step 6)? I'm pretty sure this is an issue on my side with my implementation, so don't bother too much here. I think it's because when the request errors out it callsres.sendStatus(401)right after without a next thusres.cookie()would never be called?Third is the race problem, even if
req_2arrived with the same sessionId (sid_0) asreq_1did. The store data would have already been deleted from theregeneratecall inreq_1. Thusreq_2would have no data in its session and would fail. How should we handle these race cases? Would you guys be open to some sort of flag onregeneratethat lets the store data persist longer? Or even giving us access to "generate"?let me know if you would like me to explain anything further. Thanks
Beta Was this translation helpful? Give feedback.
All reactions