@@ -39,7 +39,7 @@ const { TCPConnectWrap } = process.binding('tcp_wrap');
39
39
const { PipeConnectWrap } = process . binding ( 'pipe_wrap' ) ;
40
40
const { ShutdownWrap, WriteWrap } = process . binding ( 'stream_wrap' ) ;
41
41
const { async_id_symbol } = process . binding ( 'async_wrap' ) ;
42
- const { newUid, setDefaultTriggerAsyncId } = require ( 'internal/async_hooks' ) ;
42
+ const { newUid, defaultTriggerAsyncIdScope } = require ( 'internal/async_hooks' ) ;
43
43
const { nextTick } = require ( 'internal/process/next_tick' ) ;
44
44
const errors = require ( 'internal/errors' ) ;
45
45
const dns = require ( 'dns' ) ;
@@ -270,6 +270,14 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
270
270
timers . _unrefActive ( s ) ;
271
271
} ;
272
272
273
+
274
+ function shutdownSocket ( self , callback ) {
275
+ var req = new ShutdownWrap ( ) ;
276
+ req . oncomplete = callback ;
277
+ req . handle = self . _handle ;
278
+ return self . _handle . shutdown ( req ) ;
279
+ }
280
+
273
281
// the user has called .end(), and all the bytes have been
274
282
// sent out to the other side.
275
283
function onSocketFinish ( ) {
@@ -291,14 +299,9 @@ function onSocketFinish() {
291
299
if ( ! this . _handle || ! this . _handle . shutdown )
292
300
return this . destroy ( ) ;
293
301
294
- var req = new ShutdownWrap ( ) ;
295
- req . oncomplete = afterShutdown ;
296
- req . handle = this . _handle ;
297
- // node::ShutdownWrap isn't instantiated and attached to the JS instance of
298
- // ShutdownWrap above until shutdown() is called. So don't set the init
299
- // trigger id until now.
300
- setDefaultTriggerAsyncId ( this [ async_id_symbol ] ) ;
301
- var err = this . _handle . shutdown ( req ) ;
302
+ var err = defaultTriggerAsyncIdScope (
303
+ this [ async_id_symbol ] , [ this , afterShutdown ] , shutdownSocket
304
+ ) ;
302
305
303
306
if ( err )
304
307
return this . destroy ( errnoException ( err , 'shutdown' ) ) ;
@@ -950,23 +953,15 @@ function internalConnect(
950
953
req . localAddress = localAddress ;
951
954
req . localPort = localPort ;
952
955
953
- // node::TCPConnectWrap isn't instantiated and attached to the JS instance
954
- // of TCPConnectWrap above until connect() is called. So don't set the init
955
- // trigger id until now.
956
- setDefaultTriggerAsyncId ( self [ async_id_symbol ] ) ;
957
956
if ( addressType === 4 )
958
957
err = self . _handle . connect ( req , address , port ) ;
959
958
else
960
959
err = self . _handle . connect6 ( req , address , port ) ;
961
-
962
960
} else {
963
961
const req = new PipeConnectWrap ( ) ;
964
962
req . address = address ;
965
963
req . oncomplete = afterConnect ;
966
- // node::PipeConnectWrap isn't instantiated and attached to the JS instance
967
- // of PipeConnectWrap above until connect() is called. So don't set the
968
- // init trigger id until now.
969
- setDefaultTriggerAsyncId ( self [ async_id_symbol ] ) ;
964
+
970
965
err = self . _handle . connect ( req , address , afterConnect ) ;
971
966
}
972
967
@@ -1035,7 +1030,9 @@ Socket.prototype.connect = function(...args) {
1035
1030
'string' ,
1036
1031
path ) ;
1037
1032
}
1038
- internalConnect ( this , path ) ;
1033
+ defaultTriggerAsyncIdScope (
1034
+ this [ async_id_symbol ] , [ this , path ] , internalConnect
1035
+ ) ;
1039
1036
} else {
1040
1037
lookupAndConnect ( this , options ) ;
1041
1038
}
@@ -1074,7 +1071,11 @@ function lookupAndConnect(self, options) {
1074
1071
if ( addressType ) {
1075
1072
nextTick ( self [ async_id_symbol ] , function ( ) {
1076
1073
if ( self . connecting )
1077
- internalConnect ( self , host , port , addressType , localAddress , localPort ) ;
1074
+ defaultTriggerAsyncIdScope (
1075
+ self [ async_id_symbol ] ,
1076
+ [ self , host , port , addressType , localAddress , localPort ] ,
1077
+ internalConnect
1078
+ ) ;
1078
1079
} ) ;
1079
1080
return ;
1080
1081
}
@@ -1095,33 +1096,33 @@ function lookupAndConnect(self, options) {
1095
1096
debug ( 'connect: dns options' , dnsopts ) ;
1096
1097
self . _host = host ;
1097
1098
var lookup = options . lookup || dns . lookup ;
1098
- setDefaultTriggerAsyncId ( self [ async_id_symbol ] ) ;
1099
- lookup ( host , dnsopts , function emitLookup ( err , ip , addressType ) {
1100
- self . emit ( 'lookup' , err , ip , addressType , host ) ;
1099
+ defaultTriggerAsyncIdScope ( self [ async_id_symbol ] , [ ] , function ( ) {
1100
+ lookup ( host , dnsopts , function emitLookup ( err , ip , addressType ) {
1101
+ self . emit ( 'lookup' , err , ip , addressType , host ) ;
1101
1102
1102
- // It's possible we were destroyed while looking this up.
1103
- // XXX it would be great if we could cancel the promise returned by
1104
- // the look up.
1105
- if ( ! self . connecting ) return ;
1103
+ // It's possible we were destroyed while looking this up.
1104
+ // XXX it would be great if we could cancel the promise returned by
1105
+ // the look up.
1106
+ if ( ! self . connecting ) return ;
1106
1107
1107
- if ( err ) {
1108
- // net.createConnection() creates a net.Socket object and
1109
- // immediately calls net.Socket.connect() on it (that's us).
1110
- // There are no event listeners registered yet so defer the
1111
- // error event to the next tick.
1112
- err . host = options . host ;
1113
- err . port = options . port ;
1114
- err . message = err . message + ' ' + options . host + ':' + options . port ;
1115
- process . nextTick ( connectErrorNT , self , err ) ;
1116
- } else {
1117
- self . _unrefTimer ( ) ;
1118
- internalConnect ( self ,
1119
- ip ,
1120
- port ,
1121
- addressType ,
1122
- localAddress ,
1123
- localPort ) ;
1124
- }
1108
+ if ( err ) {
1109
+ // net.createConnection() creates a net.Socket object and
1110
+ // immediately calls net.Socket.connect() on it (that's us).
1111
+ // There are no event listeners registered yet so defer the
1112
+ // error event to the next tick.
1113
+ err . host = options . host ;
1114
+ err . port = options . port ;
1115
+ err . message = err . message + ' ' + options . host + ':' + options . port ;
1116
+ process . nextTick ( connectErrorNT , self , err ) ;
1117
+ } else {
1118
+ self . _unrefTimer ( ) ;
1119
+ defaultTriggerAsyncIdScope (
1120
+ self [ async_id_symbol ] ,
1121
+ [ self , ip , port , addressType , localAddress , localPort ] ,
1122
+ internalConnect
1123
+ ) ;
1124
+ }
1125
+ } ) ;
1125
1126
} ) ;
1126
1127
}
1127
1128
0 commit comments