Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/spawn_sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "stream_wrap.h"
#include "string_bytes.h"
#include "util-inl.h"

Expand Down Expand Up @@ -1063,12 +1064,26 @@ Maybe<int> SyncProcessRunner::ParseStdioOption(int child_fd,
return Nothing<int>();
}
return Just(AddStdioInheritFD(child_fd, inherit_fd));
}
} else if (js_type->StrictEquals(env()->wrap_string())) {
Local<Value> val;
if (!js_stdio_option->Get(context, env()->handle_string()).ToLocal(&val)) {
return Nothing<int>();
}
if (!val->IsObject()) {
return Just<int>(UV_EINVAL);
}

Local<Object> handle = val.As<Object>();
Local<v8::FunctionTemplate> sw = env()->libuv_stream_wrap_ctor_template();
if (sw.IsEmpty() || !sw->HasInstance(handle)) {
return Just<int>(UV_EINVAL);
}

Utf8Value stdio_type(env()->isolate(), js_type);
fprintf(stderr, "invalid child stdio type: %s\n", stdio_type.out());
uv_stream_t* stream = LibuvStreamWrap::From(env(), handle)->stream();
return Just(AddStdioInheritStream(child_fd, stream));
}

UNREACHABLE();
return Just<int>(UV_EINVAL);
}

int SyncProcessRunner::AddStdioIgnore(uint32_t child_fd) {
Expand Down Expand Up @@ -1116,6 +1131,18 @@ int SyncProcessRunner::AddStdioInheritFD(uint32_t child_fd, int inherit_fd) {
return 0;
}


int SyncProcessRunner::AddStdioInheritStream(uint32_t child_fd,
uv_stream_t* stream) {
CHECK_LT(child_fd, stdio_count_);
CHECK(!stdio_pipes_[child_fd]);

uv_stdio_containers_[child_fd].flags = UV_INHERIT_STREAM;
uv_stdio_containers_[child_fd].data.stream = stream;

return 0;
}

Maybe<int> SyncProcessRunner::CopyJsString(Local<Value> js_value,
const char** target) {
Isolate* isolate = env()->isolate();
Expand Down
1 change: 1 addition & 0 deletions src/spawn_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class SyncProcessRunner {
bool writable,
uv_buf_t input_buffer);
inline int AddStdioInheritFD(uint32_t child_fd, int inherit_fd);
inline int AddStdioInheritStream(uint32_t child_fd, uv_stream_t* stream);

static bool IsSet(v8::Local<v8::Value> value);
v8::Maybe<int> CopyJsString(v8::Local<v8::Value> js_value,
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-child-process-stdin-close-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { spawn } = require('child_process');

if (common.isWindows) {
common.skip('Not applicable on Windows');
}

const child = spawn(process.execPath, [
'-e',
'require("fs").closeSync(0); setTimeout(() => {}, 2000)',
], { stdio: ['pipe', 'ignore', 'ignore'] });

const timeout = setTimeout(() => {
assert.fail('stdin close event was not emitted');
}, 1000);

child.stdin.on('close', common.mustCall(() => {
clearTimeout(timeout);
child.kill();
}));

child.on('exit', common.mustCall(() => {
clearTimeout(timeout);
}));
Loading