Skip to content
This repository

Using domain in intermiddeate function cause undefined behaviour #29

Open
goelvivek opened this Issue July 28, 2013 · 1 comment

1 participant

Vivek
Vivek

Expected Behavior:
error thrown after d.on('error', function(er) { should either go to
1. Parent domain scope.
2. If no parent domain, then It should crash the application.

Current Behavior:
It is getting caught to same domain.error. Which is causing infinite looping.

More details:

I am using a third party library. Which is using node domain for error handling. If the callback function passed to that third party library have any error, It end-up in calling my callback multiple times.

Example code is:

//Read this code from bottom to top

var startFunction = function (callback) {

    //Call thirdParty function and wait for response
    thirdPartyFunction(function (error, data) {
        console.log("Called with");
        console.log(arguments);

        //Assume there is an error in my callback function
        setTimeout(function () {
            dd
            callback.apply(null);
        }, 2000);
    });

}


//ThirdParty function don't modify anything here
var thirdPartyFunction = function (callback) {
    var Domain = require("domain");

    var domain = Domain.create();
    var d = require('domain').create();

    d.on('error', function (er) {
        console.log("hi");
        callback.apply(null, er);
    });

    d.run(function () {

        setTimeout(function () {
            callback.apply(null, [null, "Hello"]);
        }, 1000);


    });
}

//Call Start function and wait for response
startFunction(function () {
    console.log("Got response")
});

We reported this bug to third party lib and they have modified the source code. Like:

    d.on('error', function (er) {

        if (isCalled == false) {
            isCalled = true;
        } else {
            return;
        }

        console.log("hi");
        callback.apply(null, er);
    });

Now problem of function getting called multiple times is solved. But final callback is never getting called.

How to handle this behavior of node ?

If third party lib modify there code to, It result in application crash. Putting a wrapper domain also not help.

 d.on('error', function (er) {

        if (isCalled == false) {
            isCalled = true;
        } else {
            throw new Error("Getting called");
            return;
        }

        console.log("hi");
        callback.apply(null, er);
    });

What is the best method of handing such cases in node ?
What I want is startFunction should come back in all cases.

Vivek

Please close. Wrong place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.