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

Context lost after making the first call to odbc #18

Open
ranjithchev opened this issue Aug 8, 2018 · 8 comments
Open

Context lost after making the first call to odbc #18

ranjithchev opened this issue Aug 8, 2018 · 8 comments

Comments

@ranjithchev
Copy link

const app = require('express')();
const httpContext = require('express-http-context');
var db = require('odbc')();
const cn = "DRIVER={ODBC Driver 13 for SQL Server};SERVER=SOMESERVER,SOMEPORT;DATABASE=somedb;Trusted_Connection=Yes"

app.use(httpContext.middleware);

app.get('/', (req, res) => {
    httpContext.set('foo', 'bar');

    console.log(httpContext.get('foo'));      // prints 'bar'

    db.open(cn, (err) => {
        if (err) throw err;

        console.log(httpContext.get('foo'));  // prints undefined!
        ...
    });
});
...

My environment is

RedHat Enterprise Linux 7.2 (maipo)
Node: 6.9.1
npm: 5.6.0
express-http-context: 1.0.4 (have tried 1.0.0 as well)

@skonves
Copy link
Owner

skonves commented Aug 8, 2018

Thanks for the detailed info! 😃

As I have been looking into these types of issues (context being lost in certain scenarios) I have been looking at the issues in both cls-hooked (on which express-http-context depends) and NodeJS itself. Jeff Lewis, the maintainer of cls-hooked, has some good commentary in Jeff-Lewis/cls-hooked#23 as well as in https://github.com/nodejs/diagnostics/blob/master/tracing/AsyncHooks/problematic-modules.md. (In fact, most of Jeff's contributions are worth reading.)

It looks like many of the consumers of async_hooks and it's dependencies are seeing issues with loss of context and we're doing what we can to try to get it working for everyone. Information like you have provided really helps, so again, thank you!

@ranjithchev
Copy link
Author

Hi @skonves . Can you help us with an example as requested wankdanker/node-odbc#37 (comment)

@edevil
Copy link

edevil commented Sep 5, 2018

@skonves shouldn’t odbc and mysql be added to that list of modules that break the async hooks?

@real-artswan
Copy link

Is there any news/workarounds on making it work with mongoose?

@webdevian
Copy link

Still having the same issue with mongoose

@ayyappayalamati
Copy link

same issue when playing with redis as well, context turned undefined after making the call to redis

@rohitsud
Copy link

Short of implementing these modules with support for CLS, there is no way to ensure request tracking will work once these modules have been called.

@zhujun24
Copy link

I have write a tidy npm package http-request-context to do this, using async_hooks, u can try it.

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

8 participants