@@ -39,7 +39,7 @@ const { TCPConnectWrap } = process.binding('tcp_wrap');
3939const { PipeConnectWrap } = process . binding ( 'pipe_wrap' ) ;
4040const { ShutdownWrap, WriteWrap } = process . binding ( 'stream_wrap' ) ;
4141const { async_id_symbol } = process . binding ( 'async_wrap' ) ;
42- const { newUid, setDefaultTriggerAsyncId } = require ( 'internal/async_hooks' ) ;
42+ const { newUid, defaultTriggerAsyncIdScope } = require ( 'internal/async_hooks' ) ;
4343const { nextTick } = require ( 'internal/process/next_tick' ) ;
4444const errors = require ( 'internal/errors' ) ;
4545const dns = require ( 'dns' ) ;
@@ -270,6 +270,14 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
270270 timers . _unrefActive ( s ) ;
271271} ;
272272
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+
273281// the user has called .end(), and all the bytes have been
274282// sent out to the other side.
275283function onSocketFinish ( ) {
@@ -291,14 +299,9 @@ function onSocketFinish() {
291299 if ( ! this . _handle || ! this . _handle . shutdown )
292300 return this . destroy ( ) ;
293301
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+ ) ;
302305
303306 if ( err )
304307 return this . destroy ( errnoException ( err , 'shutdown' ) ) ;
@@ -950,23 +953,15 @@ function internalConnect(
950953 req . localAddress = localAddress ;
951954 req . localPort = localPort ;
952955
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 ] ) ;
957956 if ( addressType === 4 )
958957 err = self . _handle . connect ( req , address , port ) ;
959958 else
960959 err = self . _handle . connect6 ( req , address , port ) ;
961-
962960 } else {
963961 const req = new PipeConnectWrap ( ) ;
964962 req . address = address ;
965963 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+
970965 err = self . _handle . connect ( req , address , afterConnect ) ;
971966 }
972967
@@ -1035,7 +1030,9 @@ Socket.prototype.connect = function(...args) {
10351030 'string' ,
10361031 path ) ;
10371032 }
1038- internalConnect ( this , path ) ;
1033+ defaultTriggerAsyncIdScope (
1034+ this [ async_id_symbol ] , [ this , path ] , internalConnect
1035+ ) ;
10391036 } else {
10401037 lookupAndConnect ( this , options ) ;
10411038 }
@@ -1074,7 +1071,11 @@ function lookupAndConnect(self, options) {
10741071 if ( addressType ) {
10751072 nextTick ( self [ async_id_symbol ] , function ( ) {
10761073 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+ ) ;
10781079 } ) ;
10791080 return ;
10801081 }
@@ -1095,33 +1096,33 @@ function lookupAndConnect(self, options) {
10951096 debug ( 'connect: dns options' , dnsopts ) ;
10961097 self . _host = host ;
10971098 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 ) ;
11011102
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 ;
11061107
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+ } ) ;
11251126 } ) ;
11261127}
11271128
0 commit comments