Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib,doc: return boolean from child.send() #3516

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions doc/api/child_process.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,10 @@ argument: `null` on success, or an `Error` object on failure.
`child.send()` emits an `'error'` event if no callback was given and the message
cannot be sent, for example because the child process has already exited.

Returns `true` under normal circumstances or `false` when the backlog of
`child.send()` returns `false` if the channel has closed or when the backlog of
unsent messages exceeds a threshold that makes it unwise to send more.
Use the callback mechanism to implement flow control.
Otherwise, it returns `true`. Use the callback mechanism to implement flow
control.

#### Example: sending server object

Expand Down
6 changes: 3 additions & 3 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,15 @@ function setupChannel(target, channel) {
handle = undefined;
}
if (this.connected) {
this._send(message, handle, false, callback);
return;
return this._send(message, handle, false, callback);
}
const ex = new Error('channel closed');
if (typeof callback === 'function') {
process.nextTick(callback, ex);
} else {
this.emit('error', ex); // FIXME(bnoordhuis) Defer to next tick.
}
return false;
};

target._send = function(message, handle, swallowErrors, callback) {
Expand Down Expand Up @@ -577,7 +577,7 @@ function setupChannel(target, channel) {
handle: null,
message: message,
});
return;
return this._handleQueue.length < 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is logically always false. :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh! Fixed.

}

var req = new WriteWrap();
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-fork-send-returns-boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const fork = require('child_process').fork;

const n = fork(common.fixturesDir + '/empty.js');

const rv = n.send({ hello: 'world' });
assert.strictEqual(rv, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this test exit? Won't the child process keep it alive? It won't quit by itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's forking an empty file. The child has no listeners etc. so it exits immediately.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we have some kind of regression in -e:

$ out/Release/node -e 'require("child_process").fork("test/fixtures/empty.js").on("exit", console.log)'
# doesn't print, doesn't exit

parallel/test-child-process-fork3 does the same thing from a file and works fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the -e thing, FWIW, it hangs for me using Node 4.2.1, 0.12.7, 0.10.40, and 0.8.28. So definitely not a recent regression.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good to know. I've filed #3574.