Skip to content

Commit c4b1a86

Browse files
committed
http: don't double-fire the req error event
Should set req.socket._hadError to true before emit the error event.
1 parent 9ab63d6 commit c4b1a86

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/_http_client.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ function socketCloseListener() {
374374
// This socket error fired before we started to
375375
// receive a response. The error needs to
376376
// fire on the request.
377-
req.emit('error', createHangUpError());
378377
req.socket._hadError = true;
378+
req.emit('error', createHangUpError());
379379
}
380380

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

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

407407
// Handle any pending data
@@ -434,8 +434,8 @@ function socketOnEnd() {
434434
if (!req.res && !req.socket._hadError) {
435435
// If we don't have a response then we know that the socket
436436
// ended prematurely and we need to emit an error on the request.
437-
req.emit('error', createHangUpError());
438437
req.socket._hadError = true;
438+
req.emit('error', createHangUpError());
439439
}
440440
if (parser) {
441441
parser.finish();
@@ -456,8 +456,8 @@ function socketOnData(d) {
456456
debug('parse error', ret);
457457
freeParser(parser, req, socket);
458458
socket.destroy();
459-
req.emit('error', ret);
460459
req.socket._hadError = true;
460+
req.emit('error', ret);
461461
} else if (parser.incoming && parser.incoming.upgrade) {
462462
// Upgrade or CONNECT
463463
var bytesParsed = ret;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const assert = require('assert');
3+
const http = require('http');
4+
const common = require('../common');
5+
6+
// not exists host
7+
const host = '*'.repeat(256);
8+
const req = http.get({ host });
9+
const err = new Error('mock unexpected code error');
10+
req.on('error', common.mustCall(() => {
11+
throw err;
12+
}));
13+
14+
process.on('uncaughtException', common.mustCall((e) => {
15+
assert.strictEqual(e, err);
16+
}));

0 commit comments

Comments
 (0)