Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lightning-net-tokio sometimes dropping messages #2832

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lightning-net-tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ impl peer_handler::SocketDescriptor for SocketDescriptor {
written_len += res;
if written_len == data.len() { return written_len; }
},
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
continue;
}
Comment on lines +505 to +507
Copy link

@coderabbitai coderabbitai bot Jan 17, 2024

Choose a reason for hiding this comment

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

The handling of std::io::ErrorKind::WouldBlock in the send_data method of SocketDescriptor uses a continue statement in a loop without any delay or backoff mechanism. This could lead to a busy-wait loop that consumes CPU resources unnecessarily when the socket is not ready to send more data.

To address this, consider implementing a backoff strategy or using an async waiting mechanism to allow other tasks to run while waiting for the socket to become writable again.

Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
    // Implement a delay or async wait before continuing
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we busy loop here its a bug in tokio, ultimately, as it should eventually return Poll::Pending for us.

Copy link

Choose a reason for hiding this comment

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

@TheBlueMatt Understood, if tokio's poll_write_ready behaves as expected, it should indeed prevent a busy loop by returning Poll::Pending when appropriate. Thank you for the clarification.

Err(_) => return written_len,
}
},
Expand Down