Skip to content

Commit e2542d2

Browse files
committed
fix: unref timers to allow graceful process exit
Unref all setTimeout timers so they don't prevent the Node.js process from exiting when all socket connections are closed. This allows the server to shut down cleanly in test environments and improves graceful shutdown behavior in production.
1 parent be3ec69 commit e2542d2

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/smtp-connection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ class SMTPConnection extends EventEmitter {
207207
}
208208

209209
// Keep a small delay for detecting early talkers
210-
setTimeout(() => this.connectionReady(), 100);
210+
let readyTimer = setTimeout(() => this.connectionReady(), 100);
211+
// Unref timer so connection init delay doesn't prevent process exit
212+
readyTimer.unref();
211213
});
212214
}
213215

@@ -308,6 +310,8 @@ class SMTPConnection extends EventEmitter {
308310
greetingSent = true;
309311
reverseCb(new Error('Timeout'));
310312
}, 1500);
313+
// Unref timer so DNS timeout doesn't prevent process exit
314+
reverseTimer.unref();
311315

312316
// Helper function to handle resolver results consistently
313317
const handleResolverResult = (...args) => {

lib/smtp-server.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ class SMTPServer extends EventEmitter {
172172
return realCallback();
173173
}
174174
}, timeout);
175+
176+
// Unref timer so it doesn't prevent process exit if all connections close naturally
177+
this._closeTimeout.unref();
175178
}
176179

177180
/**

0 commit comments

Comments
 (0)