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
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ function socketCloseListener() {
// This socket error fired before we started to
// receive a response. The error needs to
// fire on the request.
req.emit('error', createHangUpError());
req.socket._hadError = true;
req.emit('error', createHangUpError());
}

// Too bad. That output wasn't getting written.
Expand All @@ -398,10 +398,10 @@ function socketErrorListener(err) {
debug('SOCKET ERROR:', err.message, err.stack);

if (req) {
req.emit('error', err);
// For Safety. Some additional errors might fire later on
// and we need to make sure we don't double-fire the error event.
req.socket._hadError = true;
req.emit('error', err);
}

// Handle any pending data
Expand Down Expand Up @@ -434,8 +434,8 @@ function socketOnEnd() {
if (!req.res && !req.socket._hadError) {
// If we don't have a response then we know that the socket
// ended prematurely and we need to emit an error on the request.
req.emit('error', createHangUpError());
req.socket._hadError = true;
req.emit('error', createHangUpError());
}
if (parser) {
parser.finish();
Expand All @@ -456,8 +456,8 @@ function socketOnData(d) {
debug('parse error', ret);
freeParser(parser, req, socket);
socket.destroy();
req.emit('error', ret);
req.socket._hadError = true;
req.emit('error', ret);
} else if (parser.incoming && parser.incoming.upgrade) {
// Upgrade or CONNECT
var bytesParsed = ret;
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-http-client-req-error-dont-double-fire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const assert = require('assert');
const http = require('http');
const common = require('../common');

// not exists host
const host = '*'.repeat(256);
const req = http.get({ host });
const err = new Error('mock unexpected code error');
req.on('error', common.mustCall(() => {
throw err;
}));

process.on('uncaughtException', common.mustCall((e) => {
assert.strictEqual(e, err);
}));