Fibers breaks the behavior of domains #172

Open
fresheneesz opened this Issue Apr 24, 2014 · 4 comments

Projects

None yet

4 participants

@fresheneesz
Contributor

I've found what looks to be a bug in Fibers, causing node.js domains to act incorrectly. Here's a minimal repro:

var Fiber = require("fibers")
var Future = require("future")
var domain = require('domain').create

var d1 = domain()
d1.on('error', function(err) {
    console.log("1")
})

d1.run(function() {
    Fiber(function() {

        var d2 = domain()
        d2.on('error', function(err) {
            console.log("2")
        })

        d2.run(function() {
            var f = new Future
            setTimeout(function() {
                f.return()
            },100)

            f.wait()
        })

        throw Error('theError')

    }).run()
})

Expected output:
1

Got output:

/home/vagrant/backend/node_modules/fibers/future.js:173
                                                throw(ex);
                                                      ^
Error: theError
    at Error (<anonymous>)
    at /home/vagrant/backend/git/test/deadunitBugTest2.js:59:15

Similarly, this (very similar code) causes the wrong domain handler to be called:

var Fiber = require("fibers")
var Future = require("future")
var domain = require('domain').create

Fiber(function() {

var d1 = domain()
d1.on('error', function(err) {
    console.log("1")
})

    d1.run(function() {
        var d2 = domain()
        d2.on('error', function(err) {
            console.log("2")
        })

        d2.run(function() {
            var f = new Future
            setTimeout(function() {
                f.return()
            },100)

            f.wait()
        })

        throw Error('theError')
    })
}).run()

Expected output:
1

Got output:
2

I'm testing on a centos 6.5 system running in a virtual machine. Node version v0.10.25, fibers version 1.0.1

@fresheneesz fresheneesz changed the title from Fibers break behavior of domains to Fibers breaks the behavior of domains Apr 24, 2014
@okeuday
okeuday commented Nov 22, 2014

There wouldn't be a solution for this unless node.js supported more than 1 global domain, right? (unless this fibers library provided its own domain mechanism) I ask, because this problem appears to be unavoidable in node.js, preventing any real user-level threading (fibers) when domains are used.

@okeuday
okeuday commented Dec 3, 2014

@laverdet Will you consider supporting the use of multiple domains, 1 for each fiber, which are moved in and out of the global domain stack?

@laverdet
Owner

I honestly haven't messed with domains enough to know what the right solution is here. My intuition is that domain shuffling would belong in the Future library, but I just don't know.

@Gaubee
Gaubee commented Jul 23, 2015

same problem:

var Fiber = require("fibers");
var proxy_handle = function(foo) {
    setTimeout(function() {
        try {
            foo()
        } catch (e) {
            console.error("proxy_handle catch:", e);
        }
    }, 100);
};
Fiber(function() {
    var fiber = Fiber.current;
    proxy_handle(function() {
        fiber.run();
    });
    Fiber.yield();
    throw "my error";
}).run();

OUTPUT:

proxy_handle catch: asdad
[Finished in 0.3s]

I think node-fibers should manage v8 Call Stack, but this is counterintuitive and dangerous.

@Gaubee Gaubee referenced this issue in Gaubee/blog Jul 23, 2015
Open

node-fibers源码的阅读与理解 #84

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment