Skip to content

Commit 620ba41

Browse files
fengmk2tniessen
authored andcommitted
http: don't double-fire the req error event
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>
1 parent e96b949 commit 620ba41

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎lib/_http_client.js‎

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

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

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

406406
// Handle any pending data
@@ -433,8 +433,8 @@ function socketOnEnd() {
433433
if(!req.res&&!req.socket._hadError){
434434
// If we don't have a response then we know that the socket
435435
// ended prematurely and we need to emit an error on the request.
436-
req.emit('error',createHangUpError());
437436
req.socket._hadError=true;
437+
req.emit('error',createHangUpError());
438438
}
439439
if(parser){
440440
parser.finish();
@@ -455,8 +455,8 @@ function socketOnData(d) {
455455
debug('parse error',ret);
456456
freeParser(parser,req,socket);
457457
socket.destroy();
458-
req.emit('error',ret);
459458
req.socket._hadError=true;
459+
req.emit('error',ret);
460460
}elseif(parser.incoming&&parser.incoming.upgrade){
461461
// Upgrade or CONNECT
462462
varbytesParsed=ret;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
constassert=require('assert');
3+
consthttp=require('http');
4+
constcommon=require('../common');
5+
6+
// not exists host
7+
consthost='*'.repeat(256);
8+
constreq=http.get({ host });
9+
consterr=newError('mock unexpected code error');
10+
req.on('error',common.mustCall(()=>{
11+
throwerr;
12+
}));
13+
14+
process.on('uncaughtException',common.mustCall((e)=>{
15+
assert.strictEqual(e,err);
16+
}));

0 commit comments

Comments
 (0)