From ae18a90f7e251854b965120faded1856946c60b4 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 5 Sep 2013 09:21:39 +0800 Subject: [PATCH 1/4] Add test case for #83. --- spec/fixtures/module/fork_ping.js | 11 +++++++++++ spec/node/child_process.coffee | 7 +++++++ 2 files changed, 18 insertions(+) create mode 100644 spec/fixtures/module/fork_ping.js diff --git a/spec/fixtures/module/fork_ping.js b/spec/fixtures/module/fork_ping.js new file mode 100644 index 000000000000..e6d3f921ba25 --- /dev/null +++ b/spec/fixtures/module/fork_ping.js @@ -0,0 +1,11 @@ +process.on('uncaughtException', function(error) { + process.send(error.stack); +}); + +var child = require('child_process').fork(__dirname + '/ping.js'); +process.on('message', function(msg) { + child.send(msg); +}); +child.on('message', function (msg) { + process.send(msg); +}); diff --git a/spec/node/child_process.coffee b/spec/node/child_process.coffee index 2246391b8c59..fdffb1058abb 100644 --- a/spec/node/child_process.coffee +++ b/spec/node/child_process.coffee @@ -12,3 +12,10 @@ describe 'child_process', -> assert.equal msg, 'message' done() child.send 'message' + + it 'should work in forked process', (done) -> + child = child_process.fork path.join(fixtures, 'module', 'fork_ping.js') + child.on 'message', (msg) -> + assert.equal msg, 'message' + done() + child.send 'message' From 88bdff583253bdc5be0688935c9c0aeb76144003 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 5 Sep 2013 09:22:24 +0800 Subject: [PATCH 2/4] Use environment variable to detect whether to run as node. --- app/atom_main.cc | 8 ++++---- vendor/node | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/atom_main.cc b/app/atom_main.cc index e868a63a6d7d..2317c9a9d60e 100644 --- a/app/atom_main.cc +++ b/app/atom_main.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include #include #include "content/public/app/content_main.h" @@ -72,10 +73,9 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) { #include "app/atom_library_main.h" int main(int argc, const char* argv[]) { - if (argc > 1 && strcmp(argv[1], "--atom-child_process-fork") == 0) { - argv[1] = argv[0]; - return node::Start(argc - 1, const_cast(argv + 1)); - } + char* node_indicator = getenv("ATOM_SHELL_INTERNAL_RUN_AS_NODE"); + if (node_indicator != NULL && strcmp(node_indicator, "1") == 0) + return node::Start(argc, const_cast(argv)); return AtomMain(argc, argv); } diff --git a/vendor/node b/vendor/node index de1afc6cc609..9fc97854c939 160000 --- a/vendor/node +++ b/vendor/node @@ -1 +1 @@ -Subproject commit de1afc6cc609ed37863b1b0e919357f96000c6c4 +Subproject commit 9fc97854c9395edea096464edfb1e0a3b68145fe From e17da272f48defda24523119959b8022c27e3566 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 5 Sep 2013 09:47:32 +0800 Subject: [PATCH 3/4] Make child_process.fork work when options.env is set. --- spec/node/child_process.coffee | 9 +++++++++ vendor/node | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/node/child_process.coffee b/spec/node/child_process.coffee index fdffb1058abb..6d0c1dbe5e95 100644 --- a/spec/node/child_process.coffee +++ b/spec/node/child_process.coffee @@ -19,3 +19,12 @@ describe 'child_process', -> assert.equal msg, 'message' done() child.send 'message' + + it 'should work in forked process when options.env is specifed', (done) -> + child = child_process.fork path.join(fixtures, 'module', 'fork_ping.js'), + [], + env: {test: 'somevar'} + child.on 'message', (msg) -> + assert.equal msg, 'message' + done() + child.send 'message' diff --git a/vendor/node b/vendor/node index 9fc97854c939..c2ecf615ac2b 160000 --- a/vendor/node +++ b/vendor/node @@ -1 +1 @@ -Subproject commit 9fc97854c9395edea096464edfb1e0a3b68145fe +Subproject commit c2ecf615ac2bb846a38ffa0b64e9ce0ff4f8953b From 84a3eb5411e9d94b7192c663220e2b40bb0f52ff Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 5 Sep 2013 09:49:22 +0800 Subject: [PATCH 4/4] Also fix nested child_process.fork on Windows. --- app/atom_main.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/atom_main.cc b/app/atom_main.cc index 2317c9a9d60e..0dfeb386a378 100644 --- a/app/atom_main.cc +++ b/app/atom_main.cc @@ -23,7 +23,8 @@ int Start(int argc, char *argv[]); int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) { int argc = 0; wchar_t** wargv = ::CommandLineToArgvW(::GetCommandLineW(), &argc); - if (argc > 1 && wcscmp(wargv[1], L"--atom-child_process-fork") == 0) { + char* node_indicator = getenv("ATOM_SHELL_INTERNAL_RUN_AS_NODE"); + if (node_indicator != NULL && strcmp(node_indicator, "1") == 0) // Convert argv to to UTF8 char** argv = new char*[argc]; for (int i = 0; i < argc; i++) { @@ -58,8 +59,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) { } } // Now that conversion is done, we can finally start. - argv[1] = argv[0]; - return node::Start(argc - 1, argv + 1); + return node::Start(argc, argv); } sandbox::SandboxInterfaceInfo sandbox_info = {0};