othiym23 / node-continuation-local-storage

implementation of https://github.com/joyent/node/issues/5243
BSD 2-Clause "Simplified" License
1.13k stars 207 forks source link

Losing Context with Async waterfall #55

Closed coryt closed 8 years ago

coryt commented 8 years ago

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 commented 8 years ago

+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 commented 8 years ago

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 commented 8 years ago

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 commented 7 years ago

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

LiorRabin commented 7 years ago

Hello, does the async.waterfall have to be inside ns.run for it to work? See https://github.com/othiym23/node-continuation-local-storage/issues/120