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 is lost in async waterfall in hapi #47

Open
elgreatly opened this issue Sep 12, 2019 · 1 comment
Open

context is lost in async waterfall in hapi #47

elgreatly opened this issue Sep 12, 2019 · 1 comment

Comments

@elgreatly
Copy link

elgreatly commented Sep 12, 2019

i'm creating a package and use cls-hooked to keep the request in the context in hapi.js

1- i created the function to start context

let createNamespace = require('cls-hooked').createNamespace;
const ns = createNamespace('test');

bindCurrentNamespace(req, reply) {
    ns.bindEmitter(req);

    ns.run(() => {
        reply()
    });
}

2- another functions to set and get value

 setCurrentContext(req, reply) {
    ns.set('test', 'test');
    reply();
}

 getCurrentContext() {
    return ns.get('test');
 }

when i'm using these functions in async waterfall i lost the context in the second callback

async.waterfall(
    [
      function(cb) {
        getCurrentContext(); // return 'test'
        getTest(cb);
      },
      function(testData, cb1) {
       getCurrentContext(); // return undefined
       getTest2(testData, (err, data) => {
          callback(err, data);
        });
      }
    ],
    callback
  );

i tried to console the namespace to check if it keep the same namespace or not i checked it like that

let ns = getNamespace();
async.waterfall(
    [
      function(cb) {
        console.log(ns);
        getCurrentContext(); // return 'test'
        getTest(cb);
      },
      function(testData, cb1) {
       console.log(ns);
       getCurrentContext(); // return undefined
       getTest2(testData, (err, data) => {
          callback(err, data);
        });
      }
    ],
    callback
  );

what i found is:

  1. first log i got the ns like that
Namespace {
  name: 'request',
  active: { _ns_name: 'request', id: 1096, 'test': 'test' },
  _set: [ null ],
  id: -1,
  _contexts:
   Map {
     1120 => { _ns_name: 'request', id: 1096, 'test': 'test' },
     1121 => { _ns_name: 'request', id: 1096, 'test': 'test' }
   }
}
  1. second log i get the ns like that
Namespace {
  name: 'request',
  active: null,
  _set: [],
  id: -1,
  _contexts: Map {},
  _indent: 0 
}

i found namespace.exit(context); in line 412 in context.js file this line exit the context before the callback Done

but when i removed it all requests overwrite each other

anyone have idea how to fix this issue?

@elgreatly
Copy link
Author

elgreatly commented Sep 12, 2019

@Jeff-Lewis do you have idea why this issue happen?

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

1 participant