peerlibrary / meteor-server-autorun

Server-side Tracker.autorun
https://atmospherejs.com/peerlibrary/server-autorun
MIT License
36 stars 7 forks source link

Server restarts on logout when using this lib #9

Closed MilosStanic closed 8 years ago

MilosStanic commented 8 years ago

Hi guys, thanks for the beautiful work. Makes my life easy using yuukan:streamy serverside.

I'm making an app using angular-meteor. So when calling $meteor.logout() which is an async wrapper for Meteor's logout function -- the server crashes.

The crash message:

W20151123-22:32:51.417(1)? (STDERR) /home/milos/devel/chat-app/.meteor/local/build/programs/server/packages/meteor.js:974
W20151123-22:32:51.417(1)? (STDERR)     throw new Error("Meteor code must always run within a Fiber. " +          
W20151123-22:32:51.417(1)? (STDERR)           ^
W20151123-22:32:51.456(1)? (STDERR) Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20151123-22:32:51.457(1)? (STDERR)     at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
W20151123-22:32:51.457(1)? (STDERR)     at [object Object]._.extend.get (packages/meteor/dynamics_nodejs.js:21:1)
W20151123-22:32:51.458(1)? (STDERR)     at withoutInvocation (packages/meteor/timers.js:4:1)
W20151123-22:32:51.458(1)? (STDERR)     at bindAndCatch (packages/meteor/timers.js:13:1)
W20151123-22:32:51.458(1)? (STDERR)     at Object._.extend.defer (packages/meteor/timers.js:68:1)
W20151123-22:32:51.459(1)? (STDERR)     at TrackerInstance._deferAndTransfer (packages/peerlibrary_server-autorun/packages/peerlibrary_server-autorun.js:85:1)
W20151123-22:32:51.459(1)? (STDERR)     at TrackerInstance.requireFlush (packages/peerlibrary_server-autorun/packages/peerlibrary_server-autorun.js:102:1)
W20151123-22:32:51.460(1)? (STDERR)     at Computation.Tracker.Computation.Computation.invalidate (packages/peerlibrary_server-autorun/packages/peerlibrary_server-autorun.js:365:1)
W20151123-22:32:51.460(1)? (STDERR)     at Dependency.Tracker.Dependency.Dependency.changed (packages/peerlibrary_server-autorun/packages/peerlibrary_server-autorun.js:520:1)
W20151123-22:32:51.460(1)? (STDERR)     at [object Object].ReactiveVar.set (packages/reactive-var/reactive-var.js:82:1)
=> Exited with code: 8
=> Meteor server restarted

Thanks again for your wonderful work. Is there anything that can be done to fix this? I can share full app code if you want to see how it behaves for yourself. This server crashing is a real deal breaker. And I have no option but to use meteor-server-autorun, while on the other hand server crashes on user logouts, which is unacceptable.

Note: all packages, including Meteor are at their latest versions.

mitar commented 8 years ago

Hm, can you make a small reproduction instead? Not the whole app code? So that we find where exactly is the problem? Without many dependencies?

mitar commented 8 years ago

So you call logout on the client and server crashes?

MilosStanic commented 8 years ago

yes, I call logout on the client, and server crashes. I don't know how to reduce the app, but I can really point out the critical parts, here's how the code is called:


angular.module('chatapp').config(
    function ($urlRouterProvider, $stateProvider, $locationProvider) {

    //main routes of the application

    $locationProvider.html5Mode(true);

    $stateProvider
        .state('home', {
            url: '/',
            templateUrl: 'client/chat/views/chat-main.ng.html',
            controller: 'MainChatCtrl',
            controllerAs: 'mcc'
        })
        .state('logout', {
            url: '/logout',
            resolve: {
                "logout": ['$meteor', '$state', function($meteor, $state) {
                    return $meteor.logout().then(function(){
                        $state.go('home');
                    }, function(err){
                        console.log('logout error - ', err);
                    });
                }]
            }
        });

    $urlRouterProvider.otherwise("/");

    });

This is a front-end AngularJS router. located in the /client folder. I don't know how much you know AngularJS, but the code is selfexplanatory. Nothing to explain, actually. You can see in the logout route, where it calls $meteor.logout This code is activated by a press of a button in browser page. Really nothing to it, and nothing in between.

mitar commented 8 years ago

Are you sure this has anything with this package? I am more interested in your server-side code.

mitar commented 8 years ago

So the code where you use autorun on the server.

MilosStanic commented 8 years ago

Here it is, server code, nothing to do with logging out :

/**
 * Called upon a client connection, insert the user
 */
Streamy.onConnect(function(socket) {   
  Tracker.autorun(function() {
    var uid = Streamy.userId(socket); // uid will be null if the user is not logged in, otherwise, it will take the userId value
    if(uid) {
      console.log("New userId state for", Streamy.id(socket), uid);
      Clients.update({ uid: uid }, 
                      { $set: { sid: Streamy.id(socket)}}, 
                      { upsert: true});
    }
  });  
});
MilosStanic commented 8 years ago

When I remove server-autorun, here is the error message I get:

I20151123-22:53:56.563(1)? Exception from Tracker recompute function:
I20151123-22:53:56.564(1)? Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
I20151123-22:53:56.564(1)?     at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
I20151123-22:53:56.564(1)?     at [object Object]._.extend.get (packages/meteor/dynamics_nodejs.js:21:1)
I20151123-22:53:56.564(1)?     at Object.DDP.randomStream (packages/ddp-client/random_stream.js:5:1)
I20151123-22:53:56.564(1)?     at [object Object].self._makeNewID (packages/mongo/collection.js:75:1)
I20151123-22:53:56.564(1)?     at [object Object].Mongo.Collection.(anonymous function) [as update] (packages/mongo/collection.js:522:1)
I20151123-22:53:56.564(1)?     at server/chatserver.js:21:1
I20151123-22:53:56.565(1)?     at packages/tracker/tracker.js:99:1
I20151123-22:53:56.565(1)?     at Object.Meteor._noYieldsAllowed (packages/meteor/fiber_helpers.js:11:1)
I20151123-22:53:56.565(1)?     at packages/tracker/tracker.js:98:1
I20151123-22:53:56.565(1)?     at [object Object].Tracker.Computation._compute (packages/tracker/tracker.js:323:1)
I20151123-22:53:56.728(1)? removed socket vzyyjyGcwrjhh9qnz
I20151123-22:53:59.183(1)? removed socket eyZbzRQaF6zzJTEDi
I20151123-22:54:06.100(1)? New userId state for ZdB36GM5k7p4hABo2 2f3sdZopwgR6Hnyh5
I20151123-22:54:06.101(1)? [Error: Can't call yield in a noYieldsAllowed block!]
I20151123-22:54:06.105(1)? Exception from Tracker recompute function:
I20151123-22:54:06.105(1)? Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
I20151123-22:54:06.105(1)?     at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
I20151123-22:54:06.106(1)?     at [object Object]._.extend.get (packages/meteor/dynamics_nodejs.js:21:1)
I20151123-22:54:06.106(1)?     at Object.DDP.randomStream (packages/ddp-client/random_stream.js:5:1)
I20151123-22:54:06.106(1)?     at [object Object].self._makeNewID (packages/mongo/collection.js:75:1)
I20151123-22:54:06.107(1)?     at [object Object].Mongo.Collection.(anonymous function) [as update] (packages/mongo/collection.js:522:1)
I20151123-22:54:06.107(1)?     at server/chatserver.js:21:1
I20151123-22:54:06.107(1)?     at packages/tracker/tracker.js:99:1
I20151123-22:54:06.107(1)?     at Object.Meteor._noYieldsAllowed (packages/meteor/fiber_helpers.js:11:1)
I20151123-22:54:06.107(1)?     at packages/tracker/tracker.js:98:1
I20151123-22:54:06.108(1)?     at [object Object].Tracker.Computation._compute (packages/tracker/tracker.js:323:1)

but now Logout works normally.

line 21 in chatserver.js, mentioned in the error message, is the line in the previous message quoted code where I call Clients.update

mitar commented 8 years ago

What is Streamy.onConnect?

mitar commented 8 years ago

So yea, please make a small reproduction and show the whole code. This back and forth will not get us far.

MilosStanic commented 8 years ago

As I said in my opening post, it's part of yuukan:streamy library, it fires when a websockets connection is open between a client and a server.

MilosStanic commented 8 years ago

ok, will make a small reproduction... as small as I can. Thanks a lot for your effort.

MilosStanic commented 8 years ago

Hello, here, I made a working minimal demo: https://github.com/MilosStanic/angular-meteor-chat-demo

just clone, and run meteor. Open in two browsers, register one user in each browser (with dummy emails), and then under the input box you'll see a button with other user email, click on it, and you can send chat message to the other user.

As things are right now, when you logout, server crashes. If you remove server-autorun, server doesn't crash when you logout.

Thanks a lot. I hope we'll get this fixed ;)

mitar commented 8 years ago

Fixed with 0.5.2. Thanks for reporting.

MilosStanic commented 8 years ago

Thanks for fixing it so fast. Cheers!