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

events: fix TypeError in EventEmitter warning #9021

Closed
wants to merge 1 commit into from

Conversation

jseagull
Copy link
Contributor

  • make -j8 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the events Issues and PRs related to the events subsystem / EventEmitter. label Oct 11, 2016
Copy link
Member

@Trott Trott left a comment

Choose a reason for hiding this comment

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

LGTM if CI is OK.

Nit: Would be great if you could rewrite the commit message to follow the commit message format requested in the CONTRIBUTING.md doc.

/cc @nodejs/testing At first I thought the test file where the test is put was probably not the right place, based on the test file name, but closer inspection makes me think it's actually OK. Second opinion welcome, though.

@lpinca
Copy link
Member

lpinca commented Oct 11, 2016

@jseagull I would add another test, similar to this one https://github.com/nodejs/node/blob/master/test/parallel/test-event-emitter-max-listeners-warning.js, to check that error message is the expected one.

@jseagull
Copy link
Contributor Author

@lpinca
test added

e.setMaxListeners(1);

process.on('warning', common.mustCall((warning) => {
assert.ok(warning instanceof Error);
Copy link
Member

Choose a reason for hiding this comment

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

Can you add an assertion with something like this:

assert.ok(warning.message.includes('2 Symbol(symbol) listeners added.'));

@santigimeno
Copy link
Member

LGTM with the commit message format fixed, @lpinca suggestion implemented and CI green.

CI: https://ci.nodejs.org/job/node-test-pull-request/4452/

@santigimeno
Copy link
Member

Copy link
Member

@lpinca lpinca left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -257,8 +257,9 @@ function _addListener(target, type, listener, prepend) {
if (m && m > 0 && existing.length > m) {
existing.warned = true;
const w = new Error('Possible EventEmitter memory leak detected. ' +
`${existing.length} ${type} listeners added. ` +
'Use emitter.setMaxListeners() to increase limit');
`${existing.length} ${type.toString()} listeners ` +
Copy link
Contributor

@mscdex mscdex Oct 11, 2016

Choose a reason for hiding this comment

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

This isn't safe since type can currently be any type of value. So null.toString() will fail for example. It will probably be better to just concatenate with an empty string.

EDIT: On second thought, we may have to just add logic before this to figure out how to create presentable output since Symbols cannot be implicitly converted. Perhaps something like this?:

var typeStr;
if (type && typeof type.toString === 'function')
  typeStr = type.toString();
else
  typeStr = '' + type;
const w = new Error('Possible EventEmitter memory leak detected. ' +
                    `${existing.length} ${typeStr} listeners ` +
// ...

Copy link
Member

Choose a reason for hiding this comment

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

@mscdex isn't this a programmer error? type should be either a string or a symbol.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mscdex I change type.toString() to String(type) for safe reason

Copy link
Contributor

Choose a reason for hiding this comment

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

@lpinca No, currently values like null are allowed as types. I've actually used numbers before for the event name as part of a generic protocol parser.

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha.

@gibfahn
Copy link
Member

gibfahn commented Oct 11, 2016

For the commit message, maybe something like:

events,test: fix TypeError in EventEmitter warning

Allows Symbol to be converted to String so it can be included in the 
error.

Fixes: https://github.com/nodejs/node/issues/9003

@gibfahn
Copy link
Member

gibfahn commented Oct 11, 2016

cc/ @addaleax @jasnell as the last people to work on this code.

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

LGTM. The ,test in the commit message can be left out, ideally almost all code changes come with tests, so that can be assumed as an implied default :)

@@ -13,6 +13,14 @@ assert.ok(!e._events['default'].hasOwnProperty('warned'));
e.on('default', function() {});
assert.ok(e._events['default'].warned);

// symbol
var sym = Symbol('x');
Copy link
Member

Choose a reason for hiding this comment

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

tiny nit: This can be const

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All variable in this file is defined with var, so I kept the style.

Copy link
Contributor

Choose a reason for hiding this comment

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

We are in the process of migrating the code from var to const. It's not really practical to update a codebase this large in one pass, so we make the updates as we touch the code. Can you please add new code using const.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you use const and let, you can also isolate the tests using curly braces. So:

{
  const sym = Symbol('x');

  e.setMaxListeners(1);
  ...
}

@lpinca lpinca changed the title Fix https://github.com/nodejs/node/issues/9003 and add test events: fix TypeError in EventEmitter warning Oct 11, 2016
@lpinca
Copy link
Member

lpinca commented Oct 11, 2016

I took the liberty to update PR title.

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

LGTM

@jasnell jasnell self-assigned this Oct 11, 2016
@jasnell
Copy link
Member

jasnell commented Oct 11, 2016

New CI: https://ci.nodejs.org/job/node-test-pull-request/4458/
Lots of red in the last run.

@Trott
Copy link
Member

Trott commented Oct 11, 2016

Nit: Add a test for null to cover the use case described by @mscdex.

Allows Symbol to be converted to String so it can be included in the
error.

Fixes: nodejs#9003
@jseagull
Copy link
Contributor Author

jseagull commented Oct 12, 2016

@jasnell

Using 'Changelog to branch' strategy.
Cleaning workspace
 > git rev-parse --verify HEAD # timeout=10
Resetting working tree
 > git reset --hard # timeout=10
FATAL: java.io.EOFException
hudson.remoting.RequestAbortedException: java.io.EOFException
    at hudson.remoting.Request.abort(Request.java:303)
    at hudson.remoting.Channel.terminate(Channel.java:847)
    at org.jenkinsci.remoting.nio.NioChannelHub$3.run(NioChannelHub.java:614)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at ......remote call to test-digitalocean-freebsd10-x64-1(Native Method)
    at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1416)
    at hudson.remoting.Request.call(Request.java:172)
    at hudson.remoting.Channel.call(Channel.java:780)
    at hudson.remoting.RemoteInvocationHandler.invoke(RemoteInvocationHandler.java:249)
    at com.sun.proxy.$Proxy73.clean(Unknown Source)
    at org.jenkinsci.plugins.gitclient.RemoteGitImpl.clean(RemoteGitImpl.java:449)
    at hudson.plugins.git.extensions.impl.CleanCheckout.onCheckoutCompleted(CleanCheckout.java:28)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1137)
    at hudson.scm.SCM.checkout(SCM.java:485)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1269)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:607)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:86)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:529)
    at hudson.model.Run.execute(Run.java:1738)
    at hudson.matrix.MatrixRun.run(MatrixRun.java:146)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:410)
Caused by: java.io.EOFException
    at org.jenkinsci.remoting.nio.NioChannelHub$3.run(NioChannelHub.java:614)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Run condition [Always] enabling perform for step [[Execute a set of scripts, Execute a set of scripts]]
ERROR: Step ?Publish TAP Results? failed: no workspace for node-test-commit-freebsd/nodes=freebsd10-64 #4743
Checking ^not ok
ERROR: Build step failed with exception
java.lang.NullPointerException
    at hudson.plugins.textfinder.TextFinderPublisher.findText(TextFinderPublisher.java:101)
    at hudson.plugins.textfinder.TextFinderPublisher.perform(TextFinderPublisher.java:74)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:782)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:723)
    at hudson.model.Build$BuildExecution.post2(Build.java:185)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:668)
    at hudson.model.Run.execute(Run.java:1763)
    at hudson.matrix.MatrixRun.run(MatrixRun.java:146)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:410)
Build step 'Jenkins Text Finder' marked build as failure
Notifying upstream projects of job completion
Finished: FAILURE

It seems this is not caused by the changed code.

@jseagull
Copy link
Contributor Author

@Trott covered

@rvagg rvagg force-pushed the master branch 2 times, most recently from c133999 to 83c7a88 Compare October 18, 2016 17:02
@jasnell
Copy link
Member

jasnell commented Oct 19, 2016

jasnell pushed a commit that referenced this pull request Oct 26, 2016
Allows Symbol to be converted to String so it can be included in the
error.

Fixes: #9003
PR-URL: #9021
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
@jasnell
Copy link
Member

jasnell commented Oct 26, 2016

Landed in 254ab63

@jasnell jasnell closed this Oct 26, 2016
evanlucas pushed a commit that referenced this pull request Nov 3, 2016
Allows Symbol to be converted to String so it can be included in the
error.

Fixes: #9003
PR-URL: #9021
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
@MylesBorins
Copy link
Contributor

should this be backported? If so it will need a PR for both v4.x and v6.x

@gibfahn
Copy link
Member

gibfahn commented Apr 16, 2017

@jseagull if you'd be willing to raise a backport PR, the guide is here. Otherwise someone (anyone who's willing!) else can raise one.

MylesBorins pushed a commit that referenced this pull request Apr 18, 2017
Allows Symbol to be converted to String so it can be included in the
error.

Conflicts:
	lib/events.js

Fixes: #9003
Backport-PR-URL: #12459
PR-URL: #9021
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
MylesBorins pushed a commit that referenced this pull request Apr 19, 2017
Allows Symbol to be converted to String so it can be included in the
error.

Conflicts:
	lib/events.js

Fixes: #9003
Backport-PR-URL: #12459
PR-URL: #9021
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
@MylesBorins MylesBorins mentioned this pull request Apr 19, 2017
andrew749 pushed a commit to michielbaird/node that referenced this pull request Jul 19, 2017
Allows Symbol to be converted to String so it can be included in the
error.

Conflicts:
	lib/events.js

Fixes: nodejs#9003
Backport-PR-URL: nodejs/node#12459
PR-URL: nodejs/node#9021
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
events Issues and PRs related to the events subsystem / EventEmitter.
Projects
None yet
Development

Successfully merging this pull request may close these issues.