GeezerVison is showing github.com — http: fix ClientRequest unhandled errors · nodejs/node@4cdc5ea
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/_http_client.js +9-7Lines changed: 9 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -818,13 +818,15 @@ function onSocketNT(req, socket, err) { |
818 | 818 | req.emit('close'); |
819 | 819 | } |
820 | 820 |
|
821 | | - if (!err && req.agent) { |
822 | | - socket?.emit('free'); |
823 | | - } else if (socket) { |
824 | | - finished(socket.destroy(err || req[kError]), (er) => { |
825 | | - _destroy(req, er || err); |
826 | | - }); |
827 | | - return; |
| 821 | + if (socket) { |
| 822 | + if (!err && req.agent && !socket.destroyed) { |
| 823 | + socket.emit('free'); |
| 824 | + } else { |
| 825 | + finished(socket.destroy(err || req[kError]), (er) => { |
| 826 | + _destroy(req, er || err); |
| 827 | + }); |
| 828 | + return; |
| 829 | + } |
828 | 830 | } |
829 | 831 |
|
830 | 832 | _destroy(req, err || req[kError]); |
|
Collapse file
Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const http = require('http'); |
| 5 | +const net = require('net'); |
| 6 | + |
| 7 | +function createConnection() { |
| 8 | + const socket = new net.Socket(); |
| 9 | + |
| 10 | + process.nextTick(function() { |
| 11 | + socket.destroy(new Error('Oops')); |
| 12 | + }); |
| 13 | + |
| 14 | + return socket; |
| 15 | +} |
| 16 | + |
| 17 | +{ |
| 18 | + const req = http.get({ createConnection }); |
| 19 | + |
| 20 | + req.on('error', common.expectsError({ name: 'Error', message: 'Oops' })); |
| 21 | + req.abort(); |
| 22 | +} |
| 23 | + |
| 24 | +{ |
| 25 | + class CustomAgent extends http.Agent {} |
| 26 | + CustomAgent.prototype.createConnection = createConnection; |
| 27 | + |
| 28 | + const req = http.get({ agent: new CustomAgent() }); |
| 29 | + |
| 30 | + req.on('error', common.expectsError({ name: 'Error', message: 'Oops' })); |
| 31 | + req.abort(); |
| 32 | +} |