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

Losing Context with Async waterfall #55

Closed
coryt opened this issue Dec 22, 2015 · 5 comments
Closed

Losing Context with Async waterfall #55

coryt opened this issue Dec 22, 2015 · 5 comments

Comments

@coryt
Copy link

coryt commented Dec 22, 2015

The CLS context seems to be lost during async waterfall or series functions. It does appear to be working with parallel.

Tested with
node 5.0.0 and node 5.3.0
cls 3.1.4
async 1.5.0

Here is a simple test I've setup which fails on the 2nd waterfall function, the context is available within the 1st waterfall function. Am I doing something incorrect or is there an issue here?

var async = require("async");
var should = require("should");
var cls = require("continuation-local-storage");

describe("API Context", function () {

    context("Test CLS",
        function () {

            it("it should provide context across async.waterfall flow",
                function (done) {
                    var ns = cls.createNamespace("test1");
                    var expectedDate = new Date();
                    ns.run(function () {
                        ns.set("currentDate", expectedDate);

                        async.waterfall(
                            [
                                function (next) {
                                    ns.get("currentDate").should.be.eql(expectedDate);
                                    next();
                                },
                                function (next) {
                                    ns.get("currentDate").should.be.eql(expectedDate);
                                    //cannot get access to context here
                                    next();
                                }
                            ],
                            function (err) {
                                ns.get("currentDate").should.be.eql(expectedDate);
                                //cannot get access to context here
                                done();
                            }
                        )

                    })
                }
            );
        });
});
@mpseidel
Copy link

mpseidel commented Jan 8, 2016

+1 just stumbled upon this too using waterfall - any hints on how to work arround this?
node 4.1.0, cls 3.1.4, async 0.9.2

@mpseidel
Copy link

mpseidel commented Jan 9, 2016

Update: false alarm in my case.
My problem was not waterfall but calling request further down the line.

Fixed it by requiring continuation-local-storage before anything else.

See #44

@coryt
Copy link
Author

coryt commented Jan 15, 2016

Thanks @mpseidel this lead to solving my issue as well.
For the record, when requiring both async and cls, the order in which these are required matters so changing to the following resolves the issue.

var cls = require("continuation-local-storage");
var async = require("async");

@amiram
Copy link

amiram commented May 18, 2017

Thanks! this saved my life just now. I've used express-http-context and express-jwt together. The latter uses async.

@LiorRabin
Copy link

LiorRabin commented Jun 27, 2017

Hello, does the async.waterfall have to be inside ns.run for it to work?
See #120

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

4 participants