vazco / universe-modules

Use ES6 / ES2015 modules in Meteor with SystemJS
https://atmospherejs.com/universe/modules
MIT License
52 stars 7 forks source link

Server-side module code not running within a Fiber. #19

Closed optilude closed 8 years ago

optilude commented 9 years ago

Hi,

Since upgrading to 0.4.1 (I'm not sure if the error was there before as a few other things changed at the same time) I'm getting this error on startup:

W20150809-22:07:55.129(1)? (STDERR) Potentially unhandled rejection [2] Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20150809-22:07:55.130(1)? (STDERR)     at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
W20150809-22:07:55.130(1)? (STDERR)     at [object Object]._.extend.get (packages/meteor/dynamics_nodejs.js:21:1)
W20150809-22:07:55.130(1)? (STDERR)     at withoutInvocation (packages/meteor/timers.js:4:1)
W20150809-22:07:55.131(1)? (STDERR)     at bindAndCatch (packages/meteor/timers.js:13:1)
W20150809-22:07:55.131(1)? (STDERR)     at Object._.extend.setInterval (packages/meteor/timers.js:40:1)
W20150809-22:07:55.131(1)? (STDERR)     at execute (lib/models.import.js:142:12)
W20150809-22:07:55.131(1)? (STDERR)     at ensureEvaluated (packages/universe:modules/vendor/system.js:2007:1)
W20150809-22:07:55.131(1)? (STDERR)     at ensureEvaluated (packages/universe:modules/vendor/system.js:1999:1)
W20150809-22:07:55.131(1)? (STDERR)     at Object.execute (packages/universe:modules/vendor/system.js:2137:1)
W20150809-22:07:55.131(1)? (STDERR)     at doDynamicExecute (packages/universe:modules/vendor/system.js:688:1)
W20150809-22:07:55.131(1)? (STDERR)     Error loading file:///Users/maraspeli/Development/JavaScript/jiraflow/app/.meteor/local/build/programs/server/server/startup

The file server/main.js contains:

System.import('server/startup');

server/startup.import.js in turn contains various imports and a block wrapped in Meteor.startup(...). One of the imports is lib/models.import.js where the error is originating.

What is the right way to "bootstrap" server-side?

optilude commented 9 years ago

This is the offending code in lib/modules.import.js:

    Meteor.setInterval(function() {
        let now = new Date();
        AnalysisCache.remove({ "expires": { $lte: now } });
    }, 1000 * 60 * (Meteor.settings.clearCacheInterval || 5));

This was modelled on some similar cache-expiry code in Meteor itself.

If I move it to a Meteor.startup() block, then the error goes away. I think that's a workable solution, but I'm not sure if I'm doing something wrong to require it?

MacRusher commented 9 years ago

Yes there were some problems with code from modules not running inside fibers but I had some problems reproducing it. I will take a look at this again. Maybe we should run all code from System.register inside Meteor.bindEnvironment but I'm not sure if this won't introduce some unexpected behavior.

Meteor.startup is wrapping code inside fiber again, thats why it works, but this kind of hack shouldn't be required for modules to work properly.

johnsoftek commented 9 years ago

@MacRusher Any progress on this bug? In my case, the error occurs only when I run with --production. The offending line in lib/models.js is just export let Watchers = new Mongo.Collection('watchers'). I can't see a work-around for this statement. I can't wrap an export statement.

MacRusher commented 9 years ago

Yeah we have some problems with this from time to time but using Universe Collection instead of normal collections works for some reason.

Of course this shouldn't happen, but this error is really not caused by modules but by way that promises are handled on server side in general (but polyfill is provided by our package so it is our fault)

But I have some good news! In Meteor 1.2 there will be a meteor-aware Promise polyfill that will make this problem go away once and for all :) So I just need to ask you for some patience.

dalgard commented 9 years ago

Was this bug resolved with Meteor 1.2?

MacRusher commented 9 years ago

@dalgard yes, it's already fixed in upcoming version 0.5. It's still work in progress but for now you can check branch meteor-1.2 or get it from atmosphere 0.5.0-beta (there are known bugs in this release)

But fibers are working like a charm thanks to MDG promise polyfill :)

MacRusher commented 8 years ago

This should go away with new 0.5.0 version. Keep in mind that there are some braking changes in this release.