Skip to content

Losing currentContext after installing Newrelic agent #1984

Closed

Description

@olivierl

When I add Newrelic agent on top of the server.js, the currentContext "lose" the accessToken in my operation hook...

Check this sandbox

server/server.js

require('newrelic');

var loopback = require('loopback');
var boot = require('loopback-boot');

var app = module.exports = loopback();

app.use(loopback.context());
app.use(loopback.token({
  model: app.models.accessToken,
  currentUserLiteral: 'me'
}));

common/models/customer.js

Customer.observe('access', function limitToOwner(ctx, next) {
  var accessToken = loopback.getCurrentContext().get('accessToken');
  console.log(accessToken); // display 'undefined' in the console
  ctx.query = _.merge({where: {ownerId: accessToken.userId}}, ctx.query);
  next();
});

package.json

"loopback": "^2.26.2",
"loopback-boot": "^2.16.0",
"loopback-connector-postgresql": "^2.4.0",
"loopback-datasource-juggler": "^2.44.0",
"newrelic": "^1.25.0",
  • OS: Mac OS X El Capitain (10.11.3)
  • Node.js: 4.2.6 (with NVM 0.29.0)

REM: Everything is working fine on Node.js 0.12.x (tested on 0.12.7 and 0.12.9), so I only have the issue on Node.js 4.2.x (tested on 4.2.4 and 4.2.6)

Activity

  1. self-assigned this
    on Jan 29, 2016
  2. esopian commented on Jan 29, 2016

    @esopian

    +1

  3. jannyHou commented on Jan 29, 2016

    @jannyHou
    Contributor

    hey @olivierl do you get the error from postgresql only or from the default db as well?

  4. olivierl commented on Jan 29, 2016

    @olivierl
    Author

    hi @jannyHou, I've made the test with the default db (memory) and it's worst, loopback.getCurrentContext() returns undefined

  5. jannyHou commented on Feb 2, 2016

    @jannyHou
    Contributor

    Thanks @olivierl I reproduced your error.
    I created a sandbox to simplify your example and test it with default db:

    When I hit the endpoint GET/ customers I got

    "error": {
        "name": "TypeError",
        "status": 500,
        "message": "Cannot read property 'get' of null",
        "stack": "TypeError: Cannot read property 'get' of null\n    at limitToOwner (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\common\\models\\customer.js:7:51)\n    at notifySingleObserver (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:98:22)\n    at iterate (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\node_modules\\async\\lib\\async.js:181:13)\n    at Object.async.eachSeries (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\node_modules\\async\\lib\\async.js:197:9)\n    at doNotify (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:95:11)\n    at doNotify (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:93:49)\n    at doNotify (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:93:49)\n    at doNotify (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:93:49)\n    at Function.ObserverMixin._notifyBaseObservers (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:116:5)\n    at Function.ObserverMixin.notifyObserversOf (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:91:8)"
      }
    

    We will look into the problem!

    One more question, I am not so familiar with newrelic agent, have you tried to get currentContext without it?

  6. andressrg commented on Feb 2, 2016

    @andressrg

    +1

  7. jannyHou commented on Feb 2, 2016

    @jannyHou
    Contributor

    Thank you @esopian @andressrg for reporting this, we will take a look. One more question for better debug: are you also getting this error with newrelic or even without using newrelic?

  8. barocsi commented on Feb 4, 2016

    @barocsi

    could be same origin as #1807

  9. jfurie commented on Mar 8, 2016

    @jfurie

    +1

  10. Valko54 commented on Apr 27, 2016

    @Valko54

    +1

  11. a-c-m commented on Oct 5, 2016

    @a-c-m

    +1

  12. viczam commented on Oct 5, 2016

    @viczam

    -1

  13. odlevakp commented on Oct 5, 2016

    @odlevakp

    +1

  14. bajtos commented on Jan 9, 2017

    @bajtos
    Member

    Hi, loopback-context is know to loose the context in many cases. We have deprecated it in favour of explicit context passing via the "options" argument, see #1495.

    There is also an alternative approach being explored in https://github.com/strongloop/loopback-context, feel free to give it a try. If you do, then please report any issues found in loopback-context's issue tracker.

    I am closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions