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

uv: binding improvements #14933

Closed
wants to merge 4 commits into from
Closed

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Aug 18, 2017

Few improvements to process.binding('uv')

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

uv

The error message when passing in `err >= 0` to `util._errnoException()`
was less than useful.
@jasnell jasnell added the semver-major PRs that contain breaking changes and should be released in the next major version. label Aug 18, 2017
@nodejs-github-bot nodejs-github-bot added the lib / src Issues and PRs related to general changes in the lib or src directory. label Aug 18, 2017
else
errCode = uv.UV_EPIPE;
const code = uv[`UV_${err.code}`];
errCode = err.code && code ? code : uv.UV_EPIPE;
Copy link
Member

Choose a reason for hiding this comment

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

teeny tiny suggestion: err.code && code in parentheses might be more readable

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestions:

  • This closes over err, why not hoist it out of the callback, so only errCode gets closed.
  • if !err.code then `UV_${err.code}` == somethinglike('UV_undefined') so uv[`UV_${err.code}`] == false
  • IIFE can be nice:
const errCode = (() => {
  if (!err) return 0;
  return uv[`UV_${err.code}`] || uv.UV_EPIPE;
})();

@addaleax
Copy link
Member

I don’t think this needs to be semver-major.

refack
refack previously requested changes Aug 19, 2017
else
errCode = uv.UV_EPIPE;
const code = uv[`UV_${err.code}`];
errCode = err.code && code ? code : uv.UV_EPIPE;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestions:

  • This closes over err, why not hoist it out of the callback, so only errCode gets closed.
  • if !err.code then `UV_${err.code}` == somethinglike('UV_undefined') so uv[`UV_${err.code}`] == false
  • IIFE can be nice:
const errCode = (() => {
  if (!err) return 0;
  return uv[`UV_${err.code}`] || uv.UV_EPIPE;
})();

@@ -56,8 +56,7 @@ RoundRobinHandle.prototype.add = function(worker, send) {
// Hack: translate 'EADDRINUSE' error string back to numeric error code.
// It works but ideally we'd have some backchannel between the net and
// cluster modules for stuff like this.
const errno = uv['UV_' + err.errno];
send(errno, null);
send(uv[`UV_${err.errno}`], null);
Copy link
Contributor

Choose a reason for hiding this comment

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

-0 on this.
I like one expression per line...

});

[0, 1, 'test', {}, [], Infinity, -Infinity, NaN].forEach((key) => {
assert.throws(() => util._errnoException(key),
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use the new expectsError syntex:

common.expectsError(
  () => util._errnoException(key),
  {
    code: 'ERR_INVALID_ARG_TYPE',
    type: TypeError,
    message: 'The "err" argument must be of type negative number'
  }
);

if (key === 'errname')
return;

assert.doesNotThrow(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure the assert.doesNotThrow is necessary...

Copy link
Member Author

Choose a reason for hiding this comment

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

Not strictly necessary but not a problem either

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it mangles the actual error a little bit:

{
let threw = false;
try {
assert.doesNotThrow(makeBlock(thrower, Error));
} catch (e) {
threw = true;
common.expectsError({
code: 'ERR_ASSERTION',
message: /Got unwanted exception\.\n\[object Object\]/
})(e);
}
assert.ok(threw);
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm aware of what the function does. These cases do not throw

const util = require('util');
const uv = process.binding('uv');

const keys = Object.keys(uv);
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I'd go:

const keys = Object.keys(uv).filter(k => k !== 'errname');

and drop L10-11

Copy link
Member Author

Choose a reason for hiding this comment

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

Not particularly a fan of using filter

Copy link
Contributor

Choose a reason for hiding this comment

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

Ack.

if (key === 'errname')
return; // skip this
const val = uv[key];
assert.throws(() => uv[key] = 1,
Copy link
Contributor

@refack refack Aug 19, 2017

Choose a reason for hiding this comment

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

AFAICT assignment is not a valid lambda expression, you need to () => { uv[key] = 1; }

Copy link
Contributor

Choose a reason for hiding this comment

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

So what am I thinking us 😕? I remember an old debate that ended up with the invention of Object.assign().
Ohh it return the LHS, not the object.

@refack
Copy link
Contributor

refack commented Aug 19, 2017

Not all comments are blocking, but PTAL.

@jasnell
Copy link
Member Author

jasnell commented Aug 19, 2017

It has to be semver-major because of the error message change.

var e = new Error(message);
e.code = name;
e.errno = name;
const e = new Error(message);
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO a new type something like errors.UVError or errors.PlatformError would be nice, but then we'd need to expose it somehow.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's not something I want to do in this pr

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed.

@refack refack dismissed their stale review August 19, 2017 02:38

Comments have been discussed.

@mscdex mscdex added the libuv Issues and PRs related to the libuv dependency or the uv binding. label Aug 19, 2017
@jasnell
Copy link
Member Author

jasnell commented Aug 19, 2017

var name = errname(err);
if (typeof err !== 'number' || err >= 0 || !Number.isSafeInteger(err)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'err',
'negative number');
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to add the actual argument?

Copy link
Member Author

@jasnell jasnell Aug 20, 2017

Choose a reason for hiding this comment

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

Not particularly useful. The ERR_INVALID_ARG_TYPE actual prints out the type of the actual, not the value itself. So if I passed in _errnoException(1), the error message would be The "err" argument must be of type negative number. Received type number, which is just odd. We could turn this into a RangeError if the err is a number and isn't negative, but that adds a second if-check that is not strictly necessary for what is supposed to be an internal API.

@jasnell
Copy link
Member Author

jasnell commented Aug 21, 2017

CI run was a bit too red, although all of the failures were unrelated. Running again just to be safe: https://ci.nodejs.org/job/node-test-pull-request/9778/

jasnell added a commit that referenced this pull request Aug 23, 2017
* ensure that UV_... props are constants
* improve error message ... the error message when passing
  in `err >= 0` to `util._errnoException()` was less than
   useful.
* refine uses of process.binding('uv')

PR-URL: #14933
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@jasnell
Copy link
Member Author

jasnell commented Aug 23, 2017

Landed in 58831b2

@jasnell jasnell closed this Aug 23, 2017
addaleax pushed a commit to addaleax/ayo that referenced this pull request Aug 25, 2017
* ensure that UV_... props are constants
* improve error message ... the error message when passing
  in `err >= 0` to `util._errnoException()` was less than
   useful.
* refine uses of process.binding('uv')

PR-URL: nodejs/node#14933
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
addaleax pushed a commit to ayojs/ayo that referenced this pull request Aug 28, 2017
* ensure that UV_... props are constants
* improve error message ... the error message when passing
  in `err >= 0` to `util._errnoException()` was less than
   useful.
* refine uses of process.binding('uv')

PR-URL: nodejs/node#14933
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory. libuv Issues and PRs related to the libuv dependency or the uv binding. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants