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

stronger checking for functions in client.js #204

Merged
merged 1 commit into from
Mar 6, 2018

Conversation

arcana261
Copy link
Contributor

@arcana261 arcana261 commented Mar 5, 2018

checking for functions simply by instanceof would render library usesless in vm or REPL contexts. because if client is created in another V8 context, typeof would still return "function" but instanceof Function would fail and return false for functions and arrow functions. thus it would be impossible to create client before starting a REPL context.

Mainly I ran into problem using grpcc with following error:

Error: Argument mismatch in makeClientStreamRequest
    at ServiceClient.Client.makeClientStreamRequest (/usr/local/lib/node_modules/grpcc/node_modules/grpc/src/client.js:640:11)
    at apply (/usr/local/lib/node_modules/grpcc/node_modules/lodash/lodash.js:470:17)
    at ServiceClient.wrapper [as add] (/usr/local/lib/node_modules/grpcc/node_modules/lodash/lodash.js:5329:16)

After inspecting the problem and reading node docs about V8 contexts and so forth I concluded that "instanceof" is not evaluated properly because when a callback is created in REPL context, it is treated as when functions are created in another window and instanceof would fail.

@thelinuxfoundation
Copy link

Thank you for your pull request. Before we can look at your contribution, we need to ensure all contributors are covered by a Contributor License Agreement.

After the following items are addressed, please respond with a new comment here, and the automated system will re-verify.

Regards,
CLA GitHub bot

@arcana261
Copy link
Contributor Author

I've signed CLA

@murgatroid99
Copy link
Member

Can you share reproduction steps for this problem?

@murgatroid99
Copy link
Member

Also, since we are already depending on lodash anyway, I would prefer to just use _.isFunction, rather than take a new dependency.

@arcana261
Copy link
Contributor Author

Dear good sir.. The following example fails with error message Argument Mismatch

const grpc = require('grpc');
const f = '../proto/proto/math.proto';
const vm = require('vm');

parsed = grpc.load(f);

const service = new parsed.math.MathService('localhost:8000',  grpc.credentials.createInsecure());
const sandbox = { service: service };
vm.createContext(sandbox);

script = `
ch = service.add((err, data) => console.log(err, data));
ch.write(1);
ch.write(5);
ch.end();
`;

vm.runInContext(script, sandbox);

@arcana261
Copy link
Contributor Author

A more general example of V8 contexts and behavior of instanceof Function would be:

const vm = require('vm');

const sandbox = {
  isFunction: f => f instanceof Function,
  result: null
};
vm.createContext(sandbox);

// prints:
// outof sandbox: true
console.log('outof sandbox:', sandbox.isFunction(() => console.log()));

script = `
result = isFunction(() => console.log());
`;
vm.runInContext(script, sandbox);

// prints:
// inside sandbox: false
console.log('inside sandbox:', sandbox.result);

checking for functions simply by instanceof would render library usesless in vm or REPL contexts. because if client is created in another V8 context, typeof would still return "function" but instanceof Function would fail and return false for functions and arrow functions. thus it would be impossible to create client before starting a REPL context.
@arcana261
Copy link
Contributor Author

I apologize for not noticing that.. I've replaced usage of isFunction with _.isFunction

@murgatroid99 murgatroid99 changed the base branch from master to v1.10.x March 6, 2018 22:25
@murgatroid99 murgatroid99 changed the base branch from v1.10.x to master March 6, 2018 22:25
@murgatroid99
Copy link
Member

Thank you for your contribution. This change will definitely go into the 1.11.x release, and we may decide to backport it to 1.10.x.

@murgatroid99 murgatroid99 merged commit 227c095 into grpc:master Mar 6, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants