@@ -25,7 +25,10 @@ const assert = require('assert');
25
25
const Buffer = require ( 'buffer' ) . Buffer ;
26
26
const util = require ( 'util' ) ;
27
27
const EventEmitter = require ( 'events' ) ;
28
+ const setInitTriggerId = require ( 'async_hooks' ) . setInitTriggerId ;
28
29
const 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 ;
29
32
30
33
const UDP = process . binding ( 'udp_wrap' ) . UDP ;
31
34
const SendWrap = process . binding ( 'udp_wrap' ) . SendWrap ;
@@ -111,6 +114,7 @@ function Socket(type, listener) {
111
114
this . _handle = handle ;
112
115
this . _receiving = false ;
113
116
this . _bindState = BIND_STATE_UNBOUND ;
117
+ this [ async_id_symbol ] = this . _handle . getAsyncId ( ) ;
114
118
this . type = type ;
115
119
this . fd = null ; // compatibility hack
116
120
@@ -432,6 +436,10 @@ function doSend(ex, self, ip, list, address, port, callback) {
432
436
req . callback = callback ;
433
437
req . oncomplete = afterSend ;
434
438
}
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 ] ) ;
435
443
var err = self . _handle . send ( req ,
436
444
list ,
437
445
list . length ,
@@ -441,7 +449,7 @@ function doSend(ex, self, ip, list, address, port, callback) {
441
449
if ( err && callback ) {
442
450
// don't emit as error, dgram_legacy.js compatibility
443
451
const ex = exceptionWithHostPort ( err , 'send' , address , port ) ;
444
- process . nextTick ( callback , ex ) ;
452
+ nextTick ( self [ async_id_symbol ] , callback , ex ) ;
445
453
}
446
454
}
447
455
@@ -468,7 +476,7 @@ Socket.prototype.close = function(callback) {
468
476
this . _stopReceiving ( ) ;
469
477
this . _handle . close ( ) ;
470
478
this . _handle = null ;
471
- process . nextTick ( socketCloseNT , this ) ;
479
+ nextTick ( this [ async_id_symbol ] , socketCloseNT , this ) ;
472
480
473
481
return this ;
474
482
} ;
0 commit comments