fix: prevent process hanging when aborting requests to unresponsive hosts (fixes #4405)#4407
Conversation
This test reproduces a bug where aborting a fetch request to an unresponsive host prevents the Node.js process from exiting cleanly. The process hangs for ~8-10 seconds due to internal timers/handles remaining referenced in the event loop after the abort signal. The test uses a worker thread to isolate the issue and demonstrates that while the fetch operation completes and is aborted within ~1s, the process takes significantly longer to exit. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Matteo Collina <hello@matteocollina.com>
…4405 This commit addresses the process hanging issue when aborting requests to unresponsive hosts. Instead of monkey-patching the destroy method, we now properly handle the close event to ensure timers and handles are cleaned up when connections are closed or aborted. This prevents internal timers from keeping the event loop alive after a request has been aborted, allowing the Node.js process to exit cleanly. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Matteo Collina <hello@matteocollina.com>
Remove monkey-patching of socket.destroy and use the canonical 'close' event instead for cleanup. This follows Node.js core patterns and is more maintainable. The close event is the definitive cleanup event for all Node.js streams and sockets, covering all closure scenarios (destroy, timeout, error, abort, etc.) without modifying existing methods. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Matteo Collina <hello@matteocollina.com>
Restore util.js from main branch and remove unused kClearConnectTimeout symbol since the improved implementation using close event listeners doesn't require these changes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Matteo Collina <hello@matteocollina.com>
6942918 to
db5593a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
socket.destroywith proper close event listeners for cleanupRationale
This addresses the bug reported in #4405 where the Node.js process would hang indefinitely after aborting requests to unresponsive hosts. The root cause was that internal timers were not being properly cleaned up, keeping the event loop alive even after request abortion.
Changes
Bug Fixes
closeeventcloseevent is the definitive cleanup event for all Node.js streams and sockets, covering all closure scenarios (destroy, timeout, error, abort, etc.)Features
Breaking Changes and Deprecations
N/A - This is a bug fix that maintains backward compatibility while improving reliability.
Test Plan
test/fetch/issue-4405.js- Verifies that the Node.js process can exit cleanly after aborting requests to unresponsive hostsThe fix follows Node.js core patterns by using the canonical
closeevent instead of monkey-patching methods. This approach is more maintainable and reliable, as the close event covers all possible closure scenarios without needing to modify existing socket methods.🤖 Generated with Claude Code