-
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
loopback.getCurrentContext() seems to return null when MySQL is used #878
Comments
I am using almost precisely the same code, also extracted from #569. It works okay for me. Where are you placing the code? |
In my I am using loopback 2.8.2, node 0.10 |
All the same on my side, sorry. If you post a more complete code snippet I can do a more thorough comparison, if you like. |
Here it is: http://pastebin.com/kiHwaVeh but it's almost the default |
There is no special configuration for it that I have done. Can I suggest you do an |
I did it, nothing changed. This is the
I really don't know why it doesn't work, it seems that everyone uses this code snippet |
I have built you a very simple project which implements this routine. Everything seems to work fine. Perhaps you can use it to find what's broken in your project: http://goo.gl/CCrbgM |
Thank you, you were very kind. I did the same creating a new loopback application and it works either! |
Could you try what happens if you link your |
I'm not set up right now with a MySQL datasource, but can try perhaps in a day or two. Sounds like you may have found the difference, though. |
Finally, but it costed me one day. I hope that MySQL is the cause (and if it is that someone will fix it!) |
Just a suggestion, I did not test it, but could you try |
@diasalvatore Your test project w/ a MySQL datastore indeed does fail for me also. |
The bug should have been fixed by #809 in |
I have installed [email protected] but still gets currentUser: null , as mentioned below var ctx1 = loopback.getCurrentContext(); |
@viveke10 please provide an app that we can use to reproduce the problem locally on our machines, see wiki instructions. |
I cannot provide you app because its client project of our company |
@viveke10 Have you read the instructions? We are not asking you to disclose your proprietary code, but to create a new small app that reproduces the problem.
|
Before trying the sandbox, here a report I got same (it seems) problem, with MongoDB though, even as I have the latest versions everything, with supposed fix for MongoDB.
I found in a Further I have checked, it isn't the namespace it is supposed to be. I am looking in the debugger where it gets to Got current versions couple days ago, to be sure did
|
Have a reproducible case at Before that commit it was good, after that commit it wasn't. The extra I got to the reproducible case with memory db. This is equivalent to our application with a real database. |
For that reproducible case at |
@srguiwiz thank you for the update. I am curious, does the same problem occur when running on io.js 2.1? It ships a much newer version of V8 which may have fixed promise-related issues like this one. |
@Sandy1088 Hum, I have never used the realm and I don't know when and where |
Okkay I can do that soon....Thanks |
//Given function in repo sometimes gives currentUser and sometimes does not....Please ignore other functions
Members.getMember=function (id,cb) {
var ctx = loopback.getCurrentContext();
var currentUser = ctx && ctx.get('currentUser');
console.log(currentUser);
Members.find({"where":{MemberId:id}},function (mem_err,mem_res) {
if (!mem_err)
{
cb(mem_err,mem_res);
}
})
} |
@Sandy1088 code worked fine for me on node v4.2.4 and v5.3.0. It is difficult to work with the sample like this without knowing more about your environment. It would also help if the sample project were more distilled. To anybody else - if there already exists a project which can be used to reproduce the problem, please provide a link and details to reproduce? @Sandy1088 regarding your environment - can you please:
Regarding project distillation - can you please make the following changes to your project, ensuring that you can reproduce the error at the end? After the changes, calling
After those changes, hitting If you're having trouble reproducing this @Sandy1088 after the changes above, please drop me a PM or find me on Gitter (https://gitter.im/strongloop/loopback) and we can walk through it to try to distill a project which can be used to reliably reproduce the problem. |
The issue of CLS losing context has little to do with Loopback, but rather the Async and Promise implementations used by Loopback and our apps. The order in which libs are required makes a BIG difference. That's why slc run might work but slc debug might not. See how DylanSale resolved it for him with MySQL and what all of the cls-lib modules have to do fix other libraries. Because the V8 engine does not provide a way to trace async call stack (yet hopefully), we in user-land have to hack and Shim libraries and try to keep track the async call stack which is rather difficult, especially when async and promise implementations use process.nextTick() without informing CLS. Try your test in a way that ensures you require("continuous-local-storage") before ANYTHING else. Then make sure your all dependencies of all npm modules are using a cls wrapped version of Async, including bluebirds own asyn internal implementation. This will make it work, but it's difficult to maintain and is a pain in the ass. That's why many of us struggle with it. See the NG-Tracing group and @trevnorris's work on async-wrap for future developments of this. |
@Jeff-Lewis That's interesting and makes sense. If that's the case, it does give credence to the call to deprecate use of CLS, though still leaves LB users without a clear alternative. |
@ALL Interesting developments - I think the main take-away is that, while we can (and probably should) deprecate the use of CLS, the functionality it offered (currentContext) should definitely be preserved going forward. |
We're working to get AsyncWrap stable enough to be considered public API in the next couple months. Though we're still hosed in regards to the native Promise implementation. Nothing we can do ATM to observe what's going on. |
I just want keep the currentUser in the context and use it for my business logic. So I use loopback.createContext() and then I add currentUser to the created context but I am not sure how will I access it in another js file. Any Ideas? |
Could this issue affect loopback's implementation of checking whether a user in authenticated via the ACL? ie. with principalId: "$authenticated" |
Just got the issue again. Updating a model in a RemoteMethod was causing the I fixed the issue by… removing newrelic (see #1984 ) |
Should everyone be avoiding using node above v0.12.* for right now with loopback? |
I have got this working on newer versions of Node, including the latest 5.9.1 - but only on certain machines - for instance my dev machine it works fine, on my colleague's it has this same problem. However, reverting to 0.12.13 version of Node it seems to work reliably across machines and servers. Obviously this is annoying as this is quite an old version of Node. Is there any update on this? |
Just ran into this again with 5.10.1... the issue is very intermittent |
If this is currently an intermittent issue why don't we change the documentation to warn this is currently not working. I have to spend some hours before I noticed it was a problem in the framework not in my own code. Also as it's intermittent it could led people to release their software with a piece of code that will be affecting in a bad manner their production software. This is unacceptable. We should add a warning right here: https://docs.strongloop.com/display/public/LB/Using+current+context |
I am seeing this issue when using promises. Every call that is made to getCurrentContext from a promise completion is causing it to be null. Not just before save, access is also returning null. Possibly others as well. I have switched to callback and is working fine. Tried with node v4.4.5 and v4.4.7 LTS versions |
Never had this issue. Changed nothing but my version of node from 0.10 to 4.4.7 and it started. Using callbacks with async lib, not promises. I added |
Have had this issue intermittently and can't seem to reproduce it consistently on any environment, but it's enough to discourage usage of I'm only really using the loopback context to propagate user authentication details to a couple of dynamic role resolvers. I can consistently get around this issue by directly accessing the This means that the implementation won't be transport agnostic, but for now at least the issue seems to have disappeared completely. |
Having the same issue while using promises with Transactions. Switched to callbacks and it worked. Same issue appeared in an after save hook where I used callbacks though. Turns out the issue was cause I used a promise in a asyncValidator and that messed up the after save (even though I didn't use any promises in there). |
Hello. The original In the meantime, a new version |
As in #569 I was trying to set currentUser in context in order to retrieve it after in requests.
the problem is that
loopback.getCurrentContext()
returnsnull
so it will not be set.Why does this happen?
The text was updated successfully, but these errors were encountered: