You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using this module in my express app. As you suggested I have used it as a request context and stopped setting properties on request and response.
But I ran into a problem. When an error is thrown inside Promise, active context(namespace) never exits. All the calls made after this; share same context.
Following is a small code snippet which shows the issue.
varexpress=require('express')varapp=express();vargetNamespace=require('continuation-local-storage').getNamespace;varnameSpaceName='sample';varsampleKey='sampleKey';varcreateNamespace=require('continuation-local-storage').createNamespace;createNamespace(nameSpaceName);// add middle-wareapp.use(function(req,res,next){varnamespace=getNamespace(nameSpaceName);// hack for exiting context <=========if(namespace.active){console.log(namespace.get(sampleKey));// prints `sample` instead of `undefined`console.log('calling exit');namespace.exit(namespace.active);}namespace.bindEmitter(req);namespace.bindEmitter(res);namespace.run(function(){next();});});// throws an error in promisevarerrorPromise=function(){varpromise=newPromise((resolve,reject)=>{reject('str');});Promise.resolve(promise).then(function(obj){}).catch(function(err){thrownewError(err);});}// routeapp.get('/',function(req,res){// adding sample key in context// which should reset in every callvarnamespace=getNamespace(nameSpaceName);namespace.set(sampleKey,'sample');// if request query as test, call promiseif(req.query.test)errorPromise();res.status(200).send('{"status": 200}');});app.listen(3000,function(){console.log('Example app listening on port 3000!')})
Here when query string contains test, call to error promise is made. After this error when next call is made, the earlier context is still active.
Following are the requests made and respective console output
For now before I call namespace.run, if the namespace is active, I explicitly call exit on it. But this is not the right solution, as in parallel calls for few milliseconds the context is shared.
Please let me know if I'm doing something wrong or propose a solution.
Thanks.
The text was updated successfully, but these errors were encountered:
I don't understand why this happens, but here is an ad-hoc for this.
varerrorPromise=function(){varpromise=newPromise((resolve,reject)=>{reject('str');});Promise.resolve(promise).then(function(obj){}).catch(function(err){//throw new Error(err);returnPromise.reject(err);});}
Actually, I think it's a async-listener problem, not continuation-local-storage. In this case, when throw an error in promise.catch method, it trigger 'unhandledRejection' instead of calling process._fatalException function. Missing calling for asyncCatcher
I'm using this module in my express app. As you suggested I have used it as a request context and stopped setting properties on
request
andresponse
.But I ran into a problem. When an error is thrown inside
Promise
, active context(namespace) never exits. All the calls made after this; share same context.Following is a small code snippet which shows the issue.
Here when query string contains
test
, call to error promise is made. After this error when next call is made, the earlier context is still active.Following are the requests made and respective console output
For now before I call
namespace.run
, if the namespace is active, I explicitly callexit
on it. But this is not the right solution, as in parallel calls for few milliseconds the context is shared.Please let me know if I'm doing something wrong or propose a solution.
Thanks.
The text was updated successfully, but these errors were encountered: