refactor: cleanup signal listeners and auto-remove handlers#4
Conversation
Replace global setMaxListeners(0) with explicit SIGINT/SIGTERM listener management: - use process.once in createServer() - remove handlers on httpServer close in setupCleanupHandlers()
|
Hi @hustcc . |
Can you elaborate on the memory leak you encountered? Won't process.exit(0) automatically clear the listener after the process terminates? Why do I need to remove it again? What logic causes listener accumulation? |
|
Hi @BQXBQX, thanks for digging in on this – here’s a bit more context on why we needed to explicitly remove the SIGINT/SIGTERM handlers: 1) The warning that tipped me offI ran into this exact warning when running in watch mode (where the server is restarted repeatedly): Because 2) Why
|
|
@BQXBQX Great questions! You're absolutely right to think about production implications. Production Environment ConcernYou make a valid point - in most production environments, servers run continuously without frequent restarts, so this specific issue is less common. The main production scenarios where this could occur are:
But honestly, you're right that it's primarily a development/testing issue. Port Conflict IssueI haven't personally encountered the port conflict issue you mentioned with # Kill previous process first to avoid conflicts
chokidar 'src/**/*.ts' -c 'pkill -f "bun src/index.ts" 2>/dev/null || true; sleep 0.5; bun src/index.ts -t sse'
# Use nodemon which typically handles process cleanup better
nodemon --watch src --ext ts --exec "bun src/index.ts -t sse"
# Or use a different port each time
chokidar 'src/**/*.ts' -c 'PORT=$((RANDOM + 3000)) bun src/index.ts -t sse'Have you found any specific patterns that trigger the port conflicts? My ThoughtsSince this is primarily a dev-time improvement and you're concerned about production relevance, I'm totally open to:
What's your preference? I want to make sure this aligns with how you envision the project being used. |
|
Thanks for your PR, we will merge this PR |
|

Summary
Replace global setMaxListeners(0) with explicit management of SIGINT/SIGTERM handlers.
Changes
process.once('SIGINT', …)increateServer()to attach only a one-time shutdown handler.createBaseHttpServer(), remove SIGINT/SIGTERM handlers on server close to prevent listener accumulation.process.setMaxListeners(0)hack.This prevents the MaxListenersExceededWarning while keeping listener leaks in check.