Skip to content

fix: prevent process hanging when aborting requests to unresponsive hosts (fixes #4405)#4407

Closed
mcollina wants to merge 10 commits into
mainfrom
fix-4405
Closed

fix: prevent process hanging when aborting requests to unresponsive hosts (fixes #4405)#4407
mcollina wants to merge 10 commits into
mainfrom
fix-4405

Conversation

@mcollina
Copy link
Copy Markdown
Member

Summary

  • Fixes issue where aborting HTTP requests to unresponsive hosts prevents Node.js process from exiting cleanly
  • Replaces monkey-patching of socket.destroy with proper close event listeners for cleanup
  • Ensures timers and handles are properly cleaned up when connections are closed or aborted

Rationale

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

  • Fixed process hanging on aborted requests: Replaced monkey-patching approach with event-based cleanup using the canonical close event
  • Improved cleanup reliability: The close event is the definitive cleanup event for all Node.js streams and sockets, covering all closure scenarios (destroy, timeout, error, abort, etc.)
  • Follows Node.js best practices: Uses event-driven cleanup instead of method modification

Features

  • Comprehensive test coverage: Added reproduction test that verifies the fix works by ensuring the process can exit cleanly after aborting requests to unresponsive hosts

Breaking Changes and Deprecations

N/A - This is a bug fix that maintains backward compatibility while improving reliability.

Test Plan

  • Reproduction test: test/fetch/issue-4405.js - Verifies that the Node.js process can exit cleanly after aborting requests to unresponsive hosts
  • Existing test suite: All existing tests continue to pass, ensuring no regressions
  • Manual testing: Verified that the process no longer hangs when aborting requests to unresponsive hosts
  • Edge cases: Test covers the specific scenario where connection timeouts and aborts interact

The fix follows Node.js core patterns by using the canonical close event 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

mcollina and others added 4 commits August 12, 2025 17:05
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>
@mcollina mcollina marked this pull request as draft August 12, 2025 18:57
@Uzlopak Uzlopak force-pushed the fix-4405 branch 2 times, most recently from 6942918 to db5593a Compare August 13, 2025 16:03
@Uzlopak Uzlopak closed this Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants