@@ -7,17 +7,17 @@ const CHECK_PROGRESS_INTERVAL = 100;
77const CLOSING_METHODS = [ 'quit' , 'close' , 'end' , 'disconnect' ] ;
88
99const activeSockets = new Set ( ) ;
10- const coreServers = new Set ( ) ; // Native HTTP/TCP servers via tracing:net.server.listen
10+ const nativeServers = new Set ( ) ; // Native HTTP/TCP servers via tracing:net.server.listen
1111const customServers = new Set ( ) ; // Custom servers (WS, SSE etc), mostly EventEmitter instances
1212const clients = new Map ( ) ; // Regular clients like ioredis or custom wrappers, guarded via TrackableClient
1313
1414diagnostics_channel . tracingChannel ( 'net.server.listen' ) . subscribe ( {
1515 asyncEnd ( { server } ) {
16- if ( coreServers . has ( server ) ) return ;
17- coreServers . add ( server ) ;
16+ if ( nativeServers . has ( server ) ) return ;
17+ nativeServers . add ( server ) ;
1818 server . once ( 'close' , ( ) => {
1919 console . log ( '[UniversalExit] Native server close event' ) ;
20- coreServers . delete ( server ) ;
20+ nativeServers . delete ( server ) ;
2121 } ) ;
2222 }
2323} ) ;
@@ -28,7 +28,7 @@ diagnostics_channel.channel('net.client.socket').subscribe(({ socket }) => {
2828
2929 socket . once ( 'close' , ( ) => {
3030 activeSockets . delete ( socket ) ;
31- console . log ( `[UniversalExit] Socket natively CLOSED. Left in set: ${ activeSockets . size } ` ) ;
31+ console . log ( `[UniversalExit] Socket CLOSED. Left in set: ${ activeSockets . size } ` ) ;
3232 } ) ;
3333
3434 socket . on ( 'error' , err => {
@@ -63,7 +63,7 @@ function trackServer(server) {
6363 if ( ! server || typeof server . close !== 'function' ) return ;
6464
6565 // Ignore native servers
66- if ( coreServers . has ( server ) ) {
66+ if ( nativeServers . has ( server ) ) {
6767 return ;
6868 }
6969
@@ -120,8 +120,8 @@ function closeCustomServers() {
120120 }
121121}
122122
123- function closeCoreServers ( ) {
124- for ( const server of coreServers ) {
123+ function closeNativeServers ( ) {
124+ for ( const server of nativeServers ) {
125125 try {
126126 server . close ( ) ;
127127 } catch ( err ) {
@@ -155,18 +155,19 @@ async function gracefulExit(options = { stallThreshold: REGULAR_STALL_THRESHOLD,
155155 shuttingDown = true ;
156156
157157 console . log (
158- `[UniversalExit] Clean shutdown initiated. Custom Servers: ${ customServers . size } , Native Servers: ${ coreServers . size } , Sockets: ${ activeSockets . size } `
158+ `[UniversalExit] Clean shutdown initiated. Custom Servers: ${ customServers . size } , Native Servers: ${ nativeServers . size } , Sockets: ${ activeSockets . size } `
159159 ) ;
160160
161161 closeCustomServers ( ) ;
162- closeCoreServers ( ) ;
162+ closeNativeServers ( ) ;
163163
164164 const now = Date . now ( ) ;
165165
166166 const lastState = {
167167 tasks : - 1 ,
168168 bytes : - 1 ,
169- servers : - 1 ,
169+ customServers : - 1 ,
170+ nativeServers : - 1 ,
170171 clients : - 1 ,
171172 sockets : - 1 ,
172173 started : now ,
@@ -178,12 +179,14 @@ async function gracefulExit(options = { stallThreshold: REGULAR_STALL_THRESHOLD,
178179 const currentBytes = getCurrentBytes ( ) ;
179180 const currentTasks = activeTasksCount ;
180181
181- console . log ( `[UniversalExit] currentTasks: ${ currentTasks } , currentBytes: ${ currentBytes } , customServers: ${ customServers . size } , clients: ${ clients . size } ` ) ;
182+ console . log (
183+ `[UniversalExit] currentTasks: ${ currentTasks } , currentBytes: ${ currentBytes } , customServers: ${ customServers . size } , nativeServers: ${ nativeServers . size } , clients: ${ clients . size } `
184+ ) ;
182185 if ( currentTasks === 0 ) {
183186 closeClients ( ) ;
184187 }
185188
186- if ( currentTasks === 0 && currentBytes === 0 && customServers . size === 0 && clients . size === 0 ) {
189+ if ( currentTasks === 0 && currentBytes === 0 && customServers . size === 0 && nativeServers . size === 0 && clients . size === 0 ) {
187190 console . log ( '[UniversalExit] All buffers, tasks, servers and clients cleared successfully. Exiting cleanly.' ) ;
188191 await setImmediate ( ) ;
189192 process . exit ( options . exitCode ) ;
@@ -192,15 +195,17 @@ async function gracefulExit(options = { stallThreshold: REGULAR_STALL_THRESHOLD,
192195 const progressMade =
193196 currentTasks < lastState . tasks ||
194197 currentBytes !== lastState . bytes ||
195- customServers . size < lastState . servers ||
198+ customServers . size < lastState . customServers ||
199+ nativeServers . size < lastState . nativeServers ||
196200 clients . size < lastState . clients ||
197201 activeSockets . size < lastState . sockets ;
198202 const now = Date . now ( ) ;
199203
200204 if ( progressMade || lastState . tasks === - 1 ) {
201205 lastState . tasks = currentTasks ;
202206 lastState . bytes = currentBytes ;
203- lastState . servers = customServers . size ;
207+ lastState . customServers = customServers . size ;
208+ lastState . nativeServers = nativeServers . size ;
204209 lastState . clients = clients . size ;
205210 lastState . sockets = activeSockets . size ;
206211 lastState . updated = now ;
0 commit comments