Skip to content
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

Value persists in cls namespace even when http request has ended #65

Open
stnever opened this issue May 12, 2016 · 6 comments
Open

Value persists in cls namespace even when http request has ended #65

stnever opened this issue May 12, 2016 · 6 comments

Comments

@stnever
Copy link

stnever commented May 12, 2016

Hi! I'm using the usual method to have CLS contexts per http request:

// express middleware
function runInNamespace(req, res, next) {
  var namespace = cls.getNamespace('horus-dashboard-ns');

  // wrap the events from request and response
  namespace.bindEmitter(req);
  namespace.bindEmitter(res);

  // run following middleware in the scope of
  // the namespace we created
  namespace.run(function() {
    namespace.set('login', 'anonymous');
    namespace.set('reqMethod', req.method);
    namespace.set('reqUrl', req.url);
    next();
  });
}

I'm using the Sequelize Bluebird shim to have CLS play nicely with Promises.

On a certain request, I pre-populate a cache and store it in the namespace:

var cache = exports.clsNamespace.get('allConfigs');
if ( cache != null ) return Promise.resolve(cache);
// populates some data
exports.clsNamespace.set('allConfigs', data);

The thing is, I thought that allConfigs property would disappear when the HTTP request ended (that cache is intended to be used by several pieces of business logic, but on the same request). What I'm seeing is, that once the first request populates the cache, other requests detect it and I end up always using stale data.

Is this the correct behaviour? Should I manually clear the namespace before every request?

@benjaminhaven
Copy link

I'm having exactly the same issue.

@overlookmotel
Copy link
Contributor

Hi @stnever.

Can you post the complete code? i.e. in what context you're running the var cache = exports.clsNamespace.get('allConfigs'); etc code?

It sounds like the cache code is being run out of context i.e. is writing the cache to the "root" CLS context, rather than within the context of the request. Hence why it then persists across different requests.

Combining CLS with promises gets complicated. (see #64 if you want to nerd out about it!)

@mariodu
Copy link

mariodu commented Oct 16, 2016

@stnever
Did you fix it?

@overlookmotel
Copy link
Contributor

By the way, I'd recommend using cls-bluebird if you're using CLS with Bluebird promises. I did a big rewrite of it and it's more reliable than the Sequelize shim. cls-bluebird has got 100,000 tests for god's sake!

Concerning this issue, I don't think the OP ever gave enough detail to understand where the problem is coming from. If you're also having this problem @stnever maybe post a more complete example?

@stnever
Copy link
Author

stnever commented Oct 17, 2016

Oops, sorry for the delay!

I'm afraid the portion of code where this was happening was refactored a while ago, and I didn't get around to building a completely reproducible example. At any rate, I'm no longer seeing this problem.

I'll check cls-bluebird, thanks!

@overlookmotel
Copy link
Contributor

OK if you're also having this problem @mariodu maybe post a more complete example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants