Skip to content

Fix FD leak in connSocketBlockingConnect on timeout#3541

Open
madolson wants to merge 1 commit intovalkey-io:unstablefrom
madolson:fix/fd-leak-blocking-connect
Open

Fix FD leak in connSocketBlockingConnect on timeout#3541
madolson wants to merge 1 commit intovalkey-io:unstablefrom
madolson:fix/fd-leak-blocking-connect

Conversation

@madolson
Copy link
Copy Markdown
Member

@madolson madolson commented Apr 20, 2026

Summary

Fix a file descriptor leak in connSocketBlockingConnect() when aeWait() times out.

Bug

When anetTcpNonBlockConnect() succeeds but aeWait() times out (e.g., MIGRATE to an unreachable host), the fd is leaked because it was never assigned to conn->fd. The caller's connClose() checks conn->fd != -1 and skips cleanup.

Fix

Assign conn->fd = fd immediately after anetTcpNonBlockConnect() succeeds, before aeWait(). This way the caller's normal connClose() cleanup path handles the fd on any error, which is consistent with how the rest of the connection lifecycle works.

TLS connections also benefit since connTLSBlockingConnect delegates to this function for the TCP layer.

Reproducer

valkey-cli SET key hello
# Repeat against unreachable host:
for i in $(seq 1 30); do valkey-cli MIGRATE 192.0.2.1 6379 key 0 500; done
# Check: /proc/<pid>/fd shows 30 leaked socket fds

This issue was generated by AI but verified, with love, by a human.

@madolson madolson force-pushed the fix/fd-leak-blocking-connect branch from 25abc3b to 0e444b8 Compare April 20, 2026 18:50
Assign conn->fd immediately after anetTcpNonBlockConnect() succeeds,
so that the caller's connClose() properly cleans up the fd on any
subsequent error (e.g., aeWait timeout).

Previously, conn->fd was only assigned on success, so a timeout left
the fd orphaned — one leak per failed MIGRATE connection attempt.

Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
@madolson madolson force-pushed the fix/fd-leak-blocking-connect branch from 0e444b8 to 7474835 Compare April 20, 2026 18:53
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.44%. Comparing base (0327c27) to head (7474835).

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #3541      +/-   ##
============================================
+ Coverage     76.41%   76.44%   +0.02%     
============================================
  Files           159      159              
  Lines         79927    79927              
============================================
+ Hits          61078    61097      +19     
+ Misses        18849    18830      -19     
Files with missing lines Coverage Δ
src/socket.c 95.95% <100.00%> (+1.15%) ⬆️

... and 18 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@sarthakaggarwal97 sarthakaggarwal97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

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.

4 participants