-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Refactor CLS support #5709
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
Comments
Having the context be same for the entire promise chain would definitely be ideal. |
On second thoughts, I don't think this change should be considered a breaking change, but rather a bug fix. Changing the example above to either So I conclude that what I'm suggesting is the correct behavior. So I guess it wouldn't need to wait for v4. |
@mickhansen OK cool. I'll get to work on it when I can. |
The context should definitely be preserved for the whole lifetime of the promise chain I'm not sure we can use cls-bluebird instead of our own shimming - we need to manually shim all the methods, if we only shim |
@janmeier cls-bluebird patches bluebird so CLS context is preserved across all bluebird methods - |
OK, this is going to take some time. I'm going to start by re-writing all the tests for cls-bluebird to clarify where context should be maintained and where it shouldn't, and then making sure it's doing what it should be. But watch this space... |
For anyone who's interested, here's where I'm up to so far: https://github.com/overlookmotel/cls-bluebird-test |
Having looked into this fairly deeply, I've decided that the issue I raised here isn't a bug per se. There are several approaches to how CLS should work with promises, and I've decided that the best thing is to maintain the convention that callbacks should continue the CLS context from when the handler is attached to the promise. This is "convention 3" as discussed in this issue, which has generated a lot of interesting discussion: othiym23/node-continuation-local-storage#64 Therefore closing this issue. |
Hi all.
I've been investigating the reason why sometimes my code is losing CLS context when combining Sequelize with co.
I think that the cause is the way CLS is handled within Sequelize. This is a bit subtle, so I'm going to lay it out at length...
There are two problems that need to be solved to support CLS:
bluebird
loses CLS contextgeneric-pool
The present solution is that all
bluebird
methods (.then()
etc) are shimmed so that the callbacks are all bound to the CLS context. (see https://github.com/sequelize/sequelize/blob/master/lib/promise.js)This works almost 100%, however, there's a subtle problem. The CLS context that the
.then()
callback receives is the context when.then()
is called rather than maintaining the context of the previous call in the promise chain.For example:
The output is:
My feeling is that the 2nd
.then()
callback should be called within the CLS context of the promise chain and therefore should output2nd value: 123
.I think this is also the reason why CLS context also gets lost sometimes when
yield
ing a Sequelize promise within a co co-routine.I propose an alternative way of supporting CLS within Sequelize which would resolve this problem:
bluebird
methodsbluebird
so it maintain CLS contextI am happy to implement this myself, but just wanted to check if it would be a welcome PR.
Also, I guess this could in some few edge cases break peoples' apps, so would be best to go into Sequelize v4. What's the timescale for release of v4 looking like?
The text was updated successfully, but these errors were encountered: