Permalink
Browse files

Fix race condition in 'meteor mongo'.

The 'exit' event on a ChildProcess doesn't necessarily occur before all the
'data' events on its stdio, so sometimes 'meteor mongo' didn't find the
process. We should have been using the 'close' event. Switch from
child_process.spawn to child_process.exec, which simplifies the code and uses
the right event.
  • Loading branch information...
1 parent fb24d39 commit 203a82d9b977c61ac0f20a0028caf27be6baa646 @glasser glasser committed Jan 3, 2013
Showing with 28 additions and 31 deletions.
  1. +28 −31 app/lib/mongo_runner.js
View
@@ -1,6 +1,6 @@
var fs = require("fs");
var path = require("path");
-var spawn = require('child_process').spawn;
+var child_process = require('child_process');
var files = require(path.join(__dirname, '..', 'lib', 'files.js'));
@@ -15,37 +15,34 @@ var _ = require('underscore');
*/
var find_mongo_pids = function (app_dir, port, callback) {
// 'ps ax' should be standard across all MacOS and Linux.
- var proc = spawn('ps', ['ax']);
- var data = '';
- proc.stdout.on('data', function (d) {
- data += d;
- });
-
- proc.on('exit', function (code, signal) {
- if (code === 0) {
- var pids = [];
-
- _.each(data.split('\n'), function (ps_line) {
- // matches mongos we start.
- var m = ps_line.match(/^\s*(\d+).+mongod .+--port (\d+) --dbpath (.+)(?:\/|\\)\.meteor(?:\/|\\)local(?:\/|\\)db\s*$/);
- if (m && m.length === 4) {
- var found_pid = parseInt(m[1]);
- var found_port = parseInt(m[2]);
- var found_path = m[3];
-
- if ( (!port || port === found_port) &&
- (!app_dir || app_dir === found_path)) {
- pids.push({
- pid: found_pid, port: found_port, app_dir: found_path});
+ child_process.exec('ps ax',
+ function (error, stdout, stderr) {
+ if (error) {
+ callback({reason: error});
+ } else if (stderr) {
+ callback({reason: 'ps produced stderr ' + stderr});
+ } else {
+ var pids = [];
+
+ _.each(stdout.split('\n'), function (ps_line) {
+ // matches mongos we start.
+ var m = ps_line.match(/^\s*(\d+).+mongod .+--port (\d+) --dbpath (.+)(?:\/|\\)\.meteor(?:\/|\\)local(?:\/|\\)db\s*$/);
+ if (m && m.length === 4) {
+ var found_pid = parseInt(m[1]);
+ var found_port = parseInt(m[2]);
+ var found_path = m[3];
+
+ if ( (!port || port === found_port) &&
+ (!app_dir || app_dir === found_path)) {
+ pids.push({
+ pid: found_pid, port: found_port, app_dir: found_path});
+ }
}
- }
- });
+ });
- callback(null, pids);
- } else {
- callback({reason: 'ps exit code ' + code});
- }
- });
+ callback(null, pids);
+ }
+ });
};
@@ -162,7 +159,7 @@ exports.launch_mongo = function (app_dir, port, launch_callback, on_exit_callbac
return;
}
- var proc = spawn(mongod_path, [
+ var proc = child_process.spawn(mongod_path, [
'--bind_ip', '127.0.0.1',
'--smallfiles',
'--port', port,

0 comments on commit 203a82d

Please sign in to comment.