Skip to content

Conversation

@brndnsvr
Copy link

The refill block in recv_inject() used if (l > -1) to check recv() return value, which incorrectly treated EOF (recv returning 0) as valid data. This caused tight CPU loops (40-60% per process) and CLOSE_WAIT connections when peers closed their side of the connection.

Changes:

  • Replace if (l > -1) with proper three-way check:
    • l > 0: Success, set inject_len
    • l == 0: EOF, set io_status_close and return
    • l < 0: Error, handle EAGAIN/EWOULDBLOCK with io_status_retry, others with io_status_error
  • Move inject_off reset before recv() for code hygiene
  • Add io_status_ok reporting when returning buffered data

@brndnsvr brndnsvr force-pushed the fix/recv-inject-eof-handling branch from 6316718 to 55a3890 Compare December 29, 2025 21:04
@brndnsvr brndnsvr marked this pull request as ready for review December 29, 2025 21:06
Copilot AI review requested due to automatic review settings December 29, 2025 21:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an infinite loop bug in recv_inject() that occurred when a peer closed its side of the connection, causing high CPU usage (40-60% per process) and connections stuck in CLOSE_WAIT state. The fix replaces an incorrect two-way check with a proper three-way check to distinguish between successful data reception, EOF, and errors.

Key Changes:

  • Replace if (l > -1) with proper handling for recv() return values: success (l > 0), EOF (l == 0), and errors (l < 0)
  • Move inject_off reset inside the success block to prevent resetting on EOF/error
  • Add explicit status reporting for all code paths including buffered data returns

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

The refill block in recv_inject() used `if (l > -1)` to check recv() return
value, which incorrectly treated EOF (recv returning 0) as valid data. This
caused tight CPU loops (40-60% per process) and CLOSE_WAIT connections when
peers closed their side of the connection.

Changes:
- Replace `if (l > -1)` with proper three-way check:
  - l > 0: Success, set inject_len
  - l == 0: EOF, set io_status_close and return
  - l < 0: Error, handle EAGAIN/EWOULDBLOCK with io_status_retry,
           others with io_status_error
- Reset inject_off only after successful recv()
- Add io_status_ok reporting when returning buffered data
@brndnsvr brndnsvr force-pushed the fix/recv-inject-eof-handling branch from 55a3890 to ae012aa Compare December 29, 2025 21:11
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.

1 participant