@@ -43,7 +43,7 @@ const { TCPConnectWrap } = process.binding('tcp_wrap');
43
43
const { PipeConnectWrap } = process . binding ( 'pipe_wrap' ) ;
44
44
const { ShutdownWrap, WriteWrap } = process . binding ( 'stream_wrap' ) ;
45
45
const { async_id_symbol } = process . binding ( 'async_wrap' ) ;
46
- const { newUid, setDefaultTriggerAsyncId } = require ( 'internal/async_hooks' ) ;
46
+ const { newUid, defaultTriggerAsyncIdScope } = require ( 'internal/async_hooks' ) ;
47
47
const { nextTick } = require ( 'internal/process/next_tick' ) ;
48
48
const errors = require ( 'internal/errors' ) ;
49
49
const dns = require ( 'dns' ) ;
@@ -274,6 +274,14 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
274
274
timers . _unrefActive ( s ) ;
275
275
} ;
276
276
277
+
278
+ function shutdownSocket ( self , callback ) {
279
+ var req = new ShutdownWrap ( ) ;
280
+ req . oncomplete = callback ;
281
+ req . handle = self . _handle ;
282
+ return self . _handle . shutdown ( req ) ;
283
+ }
284
+
277
285
// the user has called .end(), and all the bytes have been
278
286
// sent out to the other side.
279
287
function onSocketFinish ( ) {
@@ -295,14 +303,9 @@ function onSocketFinish() {
295
303
if ( ! this . _handle || ! this . _handle . shutdown )
296
304
return this . destroy ( ) ;
297
305
298
- var req = new ShutdownWrap ( ) ;
299
- req . oncomplete = afterShutdown ;
300
- req . handle = this . _handle ;
301
- // node::ShutdownWrap isn't instantiated and attached to the JS instance of
302
- // ShutdownWrap above until shutdown() is called. So don't set the init
303
- // trigger id until now.
304
- setDefaultTriggerAsyncId ( this [ async_id_symbol ] ) ;
305
- var err = this . _handle . shutdown ( req ) ;
306
+ var err = defaultTriggerAsyncIdScope (
307
+ this [ async_id_symbol ] , [ this , afterShutdown ] , shutdownSocket
308
+ ) ;
306
309
307
310
if ( err )
308
311
return this . destroy ( errnoException ( err , 'shutdown' ) ) ;
@@ -936,23 +939,15 @@ function internalConnect(
936
939
req . localAddress = localAddress ;
937
940
req . localPort = localPort ;
938
941
939
- // node::TCPConnectWrap isn't instantiated and attached to the JS instance
940
- // of TCPConnectWrap above until connect() is called. So don't set the init
941
- // trigger id until now.
942
- setDefaultTriggerAsyncId ( self [ async_id_symbol ] ) ;
943
942
if ( addressType === 4 )
944
943
err = self . _handle . connect ( req , address , port ) ;
945
944
else
946
945
err = self . _handle . connect6 ( req , address , port ) ;
947
-
948
946
} else {
949
947
const req = new PipeConnectWrap ( ) ;
950
948
req . address = address ;
951
949
req . oncomplete = afterConnect ;
952
- // node::PipeConnectWrap isn't instantiated and attached to the JS instance
953
- // of PipeConnectWrap above until connect() is called. So don't set the
954
- // init trigger id until now.
955
- setDefaultTriggerAsyncId ( self [ async_id_symbol ] ) ;
950
+
956
951
err = self . _handle . connect ( req , address , afterConnect ) ;
957
952
}
958
953
@@ -1021,7 +1016,9 @@ Socket.prototype.connect = function(...args) {
1021
1016
'string' ,
1022
1017
path ) ;
1023
1018
}
1024
- internalConnect ( this , path ) ;
1019
+ defaultTriggerAsyncIdScope (
1020
+ this [ async_id_symbol ] , [ this , path ] , internalConnect
1021
+ ) ;
1025
1022
} else {
1026
1023
lookupAndConnect ( this , options ) ;
1027
1024
}
@@ -1064,7 +1061,11 @@ function lookupAndConnect(self, options) {
1064
1061
if ( addressType ) {
1065
1062
nextTick ( self [ async_id_symbol ] , function ( ) {
1066
1063
if ( self . connecting )
1067
- internalConnect ( self , host , port , addressType , localAddress , localPort ) ;
1064
+ defaultTriggerAsyncIdScope (
1065
+ self [ async_id_symbol ] ,
1066
+ [ self , host , port , addressType , localAddress , localPort ] ,
1067
+ internalConnect
1068
+ ) ;
1068
1069
} ) ;
1069
1070
return ;
1070
1071
}
@@ -1091,33 +1092,33 @@ function lookupAndConnect(self, options) {
1091
1092
debug ( 'connect: dns options' , dnsopts ) ;
1092
1093
self . _host = host ;
1093
1094
var lookup = options . lookup || dns . lookup ;
1094
- setDefaultTriggerAsyncId ( self [ async_id_symbol ] ) ;
1095
- lookup ( host , dnsopts , function emitLookup ( err , ip , addressType ) {
1096
- self . emit ( 'lookup' , err , ip , addressType , host ) ;
1095
+ defaultTriggerAsyncIdScope ( self [ async_id_symbol ] , [ ] , function ( ) {
1096
+ lookup ( host , dnsopts , function emitLookup ( err , ip , addressType ) {
1097
+ self . emit ( 'lookup' , err , ip , addressType , host ) ;
1097
1098
1098
- // It's possible we were destroyed while looking this up.
1099
- // XXX it would be great if we could cancel the promise returned by
1100
- // the look up.
1101
- if ( ! self . connecting ) return ;
1099
+ // It's possible we were destroyed while looking this up.
1100
+ // XXX it would be great if we could cancel the promise returned by
1101
+ // the look up.
1102
+ if ( ! self . connecting ) return ;
1102
1103
1103
- if ( err ) {
1104
- // net.createConnection() creates a net.Socket object and
1105
- // immediately calls net.Socket.connect() on it (that's us).
1106
- // There are no event listeners registered yet so defer the
1107
- // error event to the next tick.
1108
- err . host = options . host ;
1109
- err . port = options . port ;
1110
- err . message = err . message + ' ' + options . host + ':' + options . port ;
1111
- process . nextTick ( connectErrorNT , self , err ) ;
1112
- } else {
1113
- self . _unrefTimer ( ) ;
1114
- internalConnect ( self ,
1115
- ip ,
1116
- port ,
1117
- addressType ,
1118
- localAddress ,
1119
- localPort ) ;
1120
- }
1104
+ if ( err ) {
1105
+ // net.createConnection() creates a net.Socket object and
1106
+ // immediately calls net.Socket.connect() on it (that's us).
1107
+ // There are no event listeners registered yet so defer the
1108
+ // error event to the next tick.
1109
+ err . host = options . host ;
1110
+ err . port = options . port ;
1111
+ err . message = err . message + ' ' + options . host + ':' + options . port ;
1112
+ process . nextTick ( connectErrorNT , self , err ) ;
1113
+ } else {
1114
+ self . _unrefTimer ( ) ;
1115
+ defaultTriggerAsyncIdScope (
1116
+ self [ async_id_symbol ] ,
1117
+ [ self , ip , port , addressType , localAddress , localPort ] ,
1118
+ internalConnect
1119
+ ) ;
1120
+ }
1121
+ } ) ;
1121
1122
} ) ;
1122
1123
}
1123
1124
0 commit comments