@@ -25,7 +25,10 @@ const assert = require('assert');
2525const Buffer = require ( 'buffer' ) . Buffer ;
2626const util = require ( 'util' ) ;
2727const EventEmitter = require ( 'events' ) ;
28+ const setInitTriggerId = require ( 'async_hooks' ) . setInitTriggerId ;
2829const UV_UDP_REUSEADDR = process . binding ( 'constants' ) . os . UV_UDP_REUSEADDR ;
30+ const async_id_symbol = process . binding ( 'async_wrap' ) . async_id_symbol ;
31+ const nextTick = require ( 'internal/process/next_tick' ) . nextTick ;
2932
3033const UDP = process . binding ( 'udp_wrap' ) . UDP ;
3134const SendWrap = process . binding ( 'udp_wrap' ) . SendWrap ;
@@ -111,6 +114,7 @@ function Socket(type, listener) {
111114 this . _handle = handle ;
112115 this . _receiving = false ;
113116 this . _bindState = BIND_STATE_UNBOUND ;
117+ this [ async_id_symbol ] = this . _handle . getAsyncId ( ) ;
114118 this . type = type ;
115119 this . fd = null ; // compatibility hack
116120
@@ -432,6 +436,10 @@ function doSend(ex, self, ip, list, address, port, callback) {
432436 req . callback = callback ;
433437 req . oncomplete = afterSend ;
434438 }
439+ // node::SendWrap isn't instantiated and attached to the JS instance of
440+ // SendWrap above until send() is called. So don't set the init trigger id
441+ // until now.
442+ setInitTriggerId ( self [ async_id_symbol ] ) ;
435443 var err = self . _handle . send ( req ,
436444 list ,
437445 list . length ,
@@ -441,7 +449,7 @@ function doSend(ex, self, ip, list, address, port, callback) {
441449 if ( err && callback ) {
442450 // don't emit as error, dgram_legacy.js compatibility
443451 const ex = exceptionWithHostPort ( err , 'send' , address , port ) ;
444- process . nextTick ( callback , ex ) ;
452+ nextTick ( self [ async_id_symbol ] , callback , ex ) ;
445453 }
446454}
447455
@@ -468,7 +476,7 @@ Socket.prototype.close = function(callback) {
468476 this . _stopReceiving ( ) ;
469477 this . _handle . close ( ) ;
470478 this . _handle = null ;
471- process . nextTick ( socketCloseNT , this ) ;
479+ nextTick ( this [ async_id_symbol ] , socketCloseNT , this ) ;
472480
473481 return this ;
474482} ;
0 commit comments