Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Response.locals strange behavior with concurrent users #1605

Closed
lucastschmidt opened this Issue · 2 comments

1 participant

@lucastschmidt

Hey guys, I am using res.locals to store data like current logged in user and things like that, just like you guys present in the example in the website.

Problem is that, if I have 2 concurrent requests, I ve seen that res.locals from user A gets sent to res.locals of user B.

When I talk about concurrent requests, it means, user A requests Page 1, but I do another request to Resource 1, and then I render Page 1, while user B requests Page 2 ...

These res.locals from the users are being set before I do any external requests, like:

User A - sets locals
User A - performs external request
User B - sets locals
User B - performs external request
User B - external request finished
User B - response finished
User A - external request finished
User A - response finished

Am I crazy / doing something very wrong or is it possible that there is a problem? Thanks for the attention. I will try to do an example later.

express@3.2.2
node@0.10.5

@lucastschmidt

More info, did some tests here.

1 - I do a request with chrome, I set res.locals.userAgent according to req
2 - I do a request with firefox, I set res.locals.userAgent according to req

If I do one request, wait for it to finish, then do another request, it works normal, BUT, for some reason, res.locals is initialized as previous res.locals, meaning, res.locals.userAgent is already initialized as firefox when I do [2]

If I do both requests at same time, almost always I get the error, that req.headers['user-agent'] !== res.locals.userAgent

On this [COMMIT]
ab61837#lib/middleware.js

On this [FILE]
https://github.com/visionmedia/express/blob/ab61837885f8698f12105acbf0b41bb496e623c5/lib/middleware.js

If I remove the code on [FILE], meaning, res.locals = utils.locals(res); in that init function, my requests seem to work fine, I couldnt reproduce that error.

I am still investigating, but if you guys have any ideas, let me know.

edited: I wasnt able to detect yet the problem. I was able to create a simple application and I couldnt replicate my strange behavior. So, I will try to strip everything outside my application. [already did that, amazingly no problem found when I removed one middleware by one]

@lucastschmidt

Well, after hours and hours of testing and trying to find out the reason, I finally got it.

I was trying out module Cluster2 https://github.com/ql-io/cluster2. Obviously I deactivated the module to test if this problem was related to that. But I didnt remove the require('cluster2'). For some amazing reason, just by requiring the module, some sort of code is messing with something else, causing this ridiculous behavior in the response.locals object.

I didnt analyse what cluster2 does wrong, but I will create an issue there and see whats up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.