Showing with 37 additions and 22 deletions.
  1. +10 −5 lib/child_process.js
  2. +1 −0 src/env.h
  3. +3 −0 src/spawn_sync.cc
  4. +17 −3 test/simple/test-child-process-spawnsync-input.js
  5. +6 −14 test/simple/test-child-process-spawnsync-timeout.js
@@ -860,6 +860,8 @@ function _validateStdio(stdio, sync) {
ipc: true
});
} else if (stdio === 'inherit') {
if (sync)
throw new TypeError(util.format('%s[%d]', MSG_SYNC_INHERIT, i));
acc.push({
type: 'inherit',
fd: i
@@ -1205,6 +1207,9 @@ function lookupSignal(signal) {
}


var MSG_SYNC_INHERIT = 'Synchronous forks cannot inherit stdio';


function spawnSync(/*file, args, options*/) {
var opts = normalizeSpawnArguments.apply(null, arguments);

@@ -1219,6 +1224,9 @@ function spawnSync(/*file, args, options*/) {
if (options.killSignal)
options.killSignal = lookupSignal(options.killSignal);

if (options.stdio === 'inherit')
throw new TypeError(MSG_SYNC_INHERIT);

options.stdio = _validateStdio(options.stdio || 'pipe', true).stdio;

if (options.input) {
@@ -1259,11 +1267,8 @@ function spawnSync(/*file, args, options*/) {
result.stdout = result.output && result.output[1];
result.stderr = result.output && result.output[2];

if (result.error) {
var err = errnoException(result.error, 'spawnSync');
util._extend(err, result);
throw err;
}
if (result.error)
result.error = errnoException(result.error, 'spawnSync');

return result;
}
@@ -127,6 +127,7 @@ namespace node {
V(onstop_string, "onstop") \
V(output_string, "output") \
V(path_string, "path") \
V(pid_string, "pid") \
V(pipe_string, "pipe") \
V(port_string, "port") \
V(processed_string, "processed") \
@@ -667,6 +667,9 @@ Local<Object> SyncProcessRunner::BuildResultObject() {
else
js_result->Set(env()->output_string(), Null(node_isolate));

js_result->Set(env()->pid_string(),
Number::New(env()->isolate(), uv_process_.pid));

return scope.Close(js_result);
}

@@ -29,9 +29,23 @@ var options = {
};

assert.throws(function() {
spawnSync('cat', [], options)
},
/TypeError:.*should be Buffer or string not number/);
spawnSync('cat', [], options)
}, /TypeError:.*should be Buffer or string not number/);


assert.throws(function() {
options = {
stdio: 'inherit',
};
spawnSync(process.execPath, ['-v'], options);
}, /TypeError: Synchronous forks cannot inherit stdio/);

assert.throws(function() {
options = {
stdio: ['ignore', 'inherit', 'inherit'],
};
spawnSync(process.execPath, ['-v'], options);
}, /TypeError: Synchronous forks cannot inherit stdio/);

function checkRet(ret) {
assert.strictEqual(ret.status, 0);
@@ -36,19 +36,11 @@ switch(process.argv[2]) {
break;
default:
var start = Date.now();
var caught = false;
try
{
var ret = spawnSync(process.execPath, [__filename, 'child'], {timeout: TIMER});
} catch (e) {
caught = true;
ret = e;
assert.strictEqual(e.errno, 'ETIMEDOUT');
} finally {
assert.strictEqual(caught, true);
var end = Date.now() - start;
assert(end < SLEEP);
assert(ret.status > 128);
}
var ret = spawnSync(process.execPath, [__filename, 'child'], {timeout: TIMER});
assert.strictEqual(ret.error.errno, 'ETIMEDOUT');
console.log(ret);
var end = Date.now() - start;
assert(end < SLEEP);
assert(ret.status > 128);
break;
}