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

http: don't double-fire the req error event #14333

Closed

Conversation

fengmk2
Copy link
Contributor

@fengmk2 fengmk2 commented Jul 18, 2017

Should set req.socket._hadError to true before emit the error event.

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

http

@nodejs-github-bot nodejs-github-bot added the http Issues or PRs related to the http subsystem. label Jul 18, 2017
process.on('uncaughtException', common.mustCall((err) => {
uncaughtExceptionCount++;
console.log(' -> uncaughtException fire, count: %s', uncaughtExceptionCount);
assert(count === 1, 'http request error event should only fire once');
Copy link
Contributor

Choose a reason for hiding this comment

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

These asserts aren't necessary since common.mustCall() already checks that the functions are called exactly the specified number of times (with 1 being the default).

const URL = url.URL;
const testPath = '/foo?bar';

const u = `http://${common.localhostIPv4}:40404${testPath}`;
Copy link
Contributor

@mscdex mscdex Jul 18, 2017

Choose a reason for hiding this comment

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

Instead of using a fixed port as-is like this, consider using a custom http.Agent that returns an error in createConnection(). For an example of this, see this commit. This way we don't end up having a potentially flaky test in the future.

@fengmk2 fengmk2 force-pushed the http-client-req-error-dont-double-fire branch from 03aa455 to 5e9c272 Compare July 18, 2017 03:46
const http = require('http');

// not exists host
const host = '*'.repeat(256);
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 use not exists host instead.


'use strict';
const common = require('../common');
const assert = require('assert');
Copy link
Contributor

Choose a reason for hiding this comment

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

assert is unused now.

@fengmk2 fengmk2 force-pushed the http-client-req-error-dont-double-fire branch from 5e9c272 to 934cb88 Compare July 18, 2017 03:52
port: 1,
host,
});
let count = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

These count variables and console.log()s are no longer needed either IMHO.

@fengmk2 fengmk2 force-pushed the http-client-req-error-dont-double-fire branch from 934cb88 to ba8e1b7 Compare July 18, 2017 04:19
@fengmk2
Copy link
Contributor Author

fengmk2 commented Jul 18, 2017

@mscdex @dead-horse Thanks for the quickly review, all changed.

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

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

LGTM with a few suggestions.

@@ -0,0 +1,35 @@
// Copyright Joyent, Inc. and other Node contributors.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you have to include these first 21 lines in new files.


// not exists host
const host = '*'.repeat(256);
const req = http.get({
Copy link
Contributor

Choose a reason for hiding this comment

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

const req = http.get({ host }); should fit comfortably on one line.

throw new Error('mock unexpected code error');
}));

process.on('uncaughtException', common.mustCall());
Copy link
Contributor

Choose a reason for hiding this comment

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

You should probably verify that the error received here is the same one that is thrown. If you declare the error outside of the req.on('error', ...) block, but still throw it from in there, then you can just use assert.strictEqual() in here.

Should set req.socket._hadError to true before emit the error event.
@fengmk2 fengmk2 force-pushed the http-client-req-error-dont-double-fire branch from ba8e1b7 to c4b1a86 Compare July 20, 2017 13:14
@fengmk2
Copy link
Contributor Author

fengmk2 commented Jul 20, 2017

@cjihrig done!

@fengmk2
Copy link
Contributor Author

fengmk2 commented Jul 25, 2017

@mscdex @cjihrig can we land this fix at the next node version release?

@mscdex
Copy link
Contributor

mscdex commented Jul 25, 2017

@fengmk2
Copy link
Contributor Author

fengmk2 commented Aug 2, 2017

@mscdex need to restart ci task.

@mscdex
Copy link
Contributor

mscdex commented Aug 2, 2017

@jasnell
Copy link
Member

jasnell commented Aug 3, 2017

Any idea on the semver-iness of this change? I'd prefer it not to be major but it may have to be. Thoughts?

@cjihrig
Copy link
Contributor

cjihrig commented Aug 4, 2017

I'd prefer patch I think.

@fengmk2
Copy link
Contributor Author

fengmk2 commented Aug 7, 2017

@jasnell I think this change is a bugfix.

@jasnell
Copy link
Member

jasnell commented Aug 8, 2017

Works for me

@tniessen
Copy link
Member

Landed in 620ba41.

@tniessen tniessen closed this Aug 16, 2017
tniessen referenced this pull request Aug 16, 2017
req.socket._hadError should be set before emitting the error event.

PR-URL: #14659
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
@fengmk2 fengmk2 deleted the http-client-req-error-dont-double-fire branch October 18, 2017 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants