Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

malloc: pointer being freed was not allocated #8

Closed
ybogdanov opened this Issue Feb 12, 2011 · 2 comments

Comments

Projects
None yet
2 participants
Contributor

ybogdanov commented Feb 12, 2011

Hi,

Here is simplified code example (originally from [node-fiberize](from https://github.com/lm1/node-fiberize/)):

Fiber(function(){

    var fiber = Fiber.current,
        a = null;

    var cb = function(value) {
        a = value;
        /* This is to handle the case where the callback is called
           directly from wrapped function (not async), thus from same fiber.
           It should have been done in resume actually, but in current
           codebase there is no resume method added and native Fiber.run
           is used (with no check Fiber.run would start a new fiber in this
           case causing a disaster).
           https://github.com/lm1/node-fiberize/commit/87391a31ab9a4823fa9130b576521e744ee15054#L0R51
        */
        if (Fiber.current !== fiber) {
            fiber.run();
        }
    }

    setTimeout(function(){
        cb(1);
    }, 100)

    while (!a) {
        yield();
    }

    console.log(a);

}).run()

So, it's obviously prints the value of "a" when it will be assigned through callback:

1

But if I'll try to inherit another Fiber into:

Fiber(function(){

    var fiber = Fiber.current,
        a = null;

    var cb = function(value) {
        a = value;
        if (Fiber.current !== fiber) {
            fiber.run();
        }
    }

    Fiber(function(){
        cb(1);
    }).run();

    while (!a) {
        yield();
    }

    console.log(a);

}).run()

I'll get the following output:

1
node(22145) malloc: *** error for object 0x100916890: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
/Users/octave/bin/fiber-shim: line 25: 22145 Abort trap              FIBER_SHIM=1 DYLD_INSERT_LIBRARIES="$FIBERS_ROOT/coroutine.dylib" DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_LIBRARY_PATH="$FIBERS_ROOT" "$@"

node v0.3.8
node-fibers 0.1.3
OSX 10.6.3

Owner

laverdet commented Feb 17, 2011

Improved check for invalid calls to run()

If you attempt to run() a fiber that's already running from inside
another fiber you would get a segfault. This is because even though the
fiber isn't running, it hasn't yielded either. Thus where to resume
execution is unclear. This should throw an exception.

Closed by ffa06fe

Owner

laverdet commented Feb 17, 2011

Hey thanks for the solid report. It seems this is actually a invalid usage of fibers. Since the outer fiber is still running, the run() call can't execute correctly. You'd have to yield() first. You can see this simply with:

Fiber(function() {
  var that = Fiber.current;
  Fiber(function(){
    that.run();
  }).run();
}).run();

It's unclear exactly what that.run() should actually do. So ffa06fe which I just committed will just throw an exception when this happens now.

This issue was closed.

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