-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Race condition in logout function #1004
Comments
Hi @chr15m I've recently experienced the same issue after a v0.6 upgrade and have been trying and failing to reproduce (using redis as a store). Are you able to confirm if the additional save that needs to happen after |
@BaileyFirman I believe the whole if (options.keepSessionInfo) {
merge(req.session, prevSession);
req.session.save(function(err) {
if (err) {
return cb(err);
}
cb();
});
} else {
cb();
} This code is working on my stack but I have not run any comprehensive tests. I will issue a PR now. |
@chr15m Thank you for the fast reply, and the PR. Hopefully it won't be too long before it's merged in and available on npm. |
Updating to confirm that when we compare our environments with and without this fix applied, we can see the issue is resolved. |
@BaileyFirman thanks for testing the patch. I'm personally happy to wait for Jared to review, merge and release. I imagine he is incredibly busy and maintaining this repo is a thankless task. This package is installed half a million times each month and yet only a handful of people sponsor Jared. It would be great if he was sponsored by more of the many commercial entities who depend on this code in production. Which reminds me, as an individual, thank you @jaredhanson for creating and maintaining this repo! 🙏 |
Hi @chr15m just wanted to report back that we did see the issue again these last couple days although it did seem to be reduced substantially after applying your fix. I think any remaining issues may be a result of how we are handling destroying sessions as it relates to some of our logout flows. I will add any updates if I find them to be relevant to your PR or passport. |
Following up on this thread after some time in case the information is still relevant for you @chr15m
This is clunky but it resolved 99% of our problems, but we would still get 1-3 instances of a session error per day.
This has evolved into a full refactor, it has some slight but hopefully non-breaking tweaks to satisfy typing etc. |
Thanks for the update, that's very interesting. I wonder if introducing promises added subtle delays which are covering up more timing or order of execution bugs hiding in there. 🤔 I'm glad the changes got rid of the issues for you! |
Possibly, I tried a thread sleep that polled waiting for the callback from req.logIn to finish before continues execution (just hacky polling with a while) and it tended to wait up to 40ms which matched the redis calls in our logging, if I had to guess there's probably a magic number where if getting a session takes too long it all falls apart without promises. |
Interesting, and maybe it's not delays that promises introduce but simply enforcing the order of execution. |
So, is this gonna get merged or what? |
I encountered an intermittent issue in my app where two sessions were unexpectedly created for the same user between log in and log out. I wrote a test script to repeatedly execute the log in and log out code over and over so I could catch it reliably. Upon examining the most recent release's change I think a potential race condition has been missed in the
0.6.0
upgrade.The source of the potential race condition can be found here: https://github.com/jaredhanson/passport/blob/master/lib/sessionmanager.js#L87-L90
The callback is called immediately after
merge(req.session, prevSession);
but in this casereq.session
may not yet be saved when the callback is called.Fix
Add an additional
req.session.save()
call for the case wherekeepSessionInfo
istrue
. The other code path can run thecb();
straight away as it currently does when there is no change toreq.session
.Happy to provide a PR.
Expected behavior
If
keepSessionInfo
is set on the login and logout function, and a random number is persisted to the session, then you repeatedly log in and log out over and over again, you would expect only one session to exist with the random number you persisted.Actual behavior
With an async database backend (such as postgres) and after logging in and out hundreds of times, one can observe the situation where a session is sometimes duplicated.
Steps to reproduce
Use an async database backend, persist a random number to a session, then log in and out many times. Eventually two sessions will be created that have the same random number in their session data.
Environment
The text was updated successfully, but these errors were encountered: