2

Below is my code in server/server.js file. When I call Meteor.apply('testMethod') I get Error: listen EADDRINUSE. I am running meteor app with meteorite, the only non-generic package installed is npm

var Fiber = Meteor.require('fibers');
var fiber = Fiber.current;
Meteor.methods({
    testMethod: function(){
        setTimeout(function(){fiber.run('test')}, 2000);
        res = Fiber.yield();
        console.log(res);
        return res;
    }
})

I know the most obvious solution here is to use Meteor's wrappers around fiber, but what I really want to implement is yielding from fiber on async call and then resuming within some event handler. And I have not found anything suitable for that.

Stack trace:

Error: listen EADDRINUSE
    at errnoException (net.js:901:11)
    at Server._listen2 (net.js:1039:14)
    at listen (net.js:1061:10)
    at net.js:1135:9
    at dns.js:72:18
    at process._tickCallback (node.js:415:13)
Vasaka
  • 579
  • 6
  • 17
  • Why not use `Meteor.setTimeout()`? – Brad M Feb 18 '14 at 18:35
  • Because that does not solve anything. – Vasaka Feb 18 '14 at 18:44
  • Have you tried using `Meteor._wrapAsync` instead? Also the core issue with the error - are you leaving out any code? `Server._listen2` does not exist in Meteor. You're trying to listen to something somewhere in code you've not provided & the port is already used, perhaps when you ran the method the first time. – Tarang Feb 18 '14 at 19:07
  • Yes, that solves the problem in this particular case, but as I mentioned, the real purpose of using Fibers on low level is calling `fiber.run` within some event handler, so I have no method to wrap with `wrapAsync`. Also, the code in my post is full code of my application. `Server._listen2` exists within `net.js` of `node` libraries. And no, port is not in use, because without using fibers everything runs just fine. – Vasaka Feb 18 '14 at 19:12
  • You would have to give a bit more details about it, Fibers isn't causing it, its the sychronous nature of it that is causing something to lock up somewhere. What is the code you put in 'test'? – Tarang Feb 19 '14 at 11:27
  • I am not quite understanding your question, but I can say that this is the literal code of my application. There is nothing besides it. This is just Meteor's template with `npm` and this code in `server/server.js` file. – Vasaka Feb 19 '14 at 17:38

1 Answers1

1

I answered this on the Meteor IRC.

Move this line:

var fiber = Fiber.current;

into the first line of the 'testMethod' function. Like so:

var Fiber = Meteor.require('fibers');

Meteor.methods({
    testMethod: function(){
        var fiber = Fiber.current;
        setTimeout(function(){fiber.run('test')}, 2000);
        res = Fiber.yield();
        console.log(res);
        return res;
    }
});

I believe why you're experiencing this is because each user gets their own "fiber". So when the client calls the server, the current fiber is different than the fiber you declared in the outer scope.

Dave
  • 2,506
  • 2
  • 20
  • 27