-
Notifications
You must be signed in to change notification settings - Fork 6
fix async protocol.read().await not returning on closed sockets #160
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
Merged
rustaceanrob
merged 2 commits into
rust-bitcoin:main
from
0xB10C:2026-01-fix-reads-on-closed-socket
Jan 9, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -154,6 +154,83 @@ fn hello_world_happy_path() { | |
| assert_eq!(message, decrypted_message[1..].to_vec()); // Skip header byte | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[cfg(feature = "tokio")] | ||
| async fn pingpong_with_closed_connection_async() { | ||
| use bip324::{futures::Protocol, io::Payload}; | ||
| use bitcoin::consensus; | ||
| use p2p::message::{NetworkMessage, V2NetworkMessage}; | ||
| use tokio::net::TcpListener; | ||
| use tokio::net::TcpStream; | ||
|
|
||
| // Start a server that responds to exactly one Ping(x) message with a | ||
| // Pong(x) message and then stops. This allows testing to read from a closed stream. | ||
| let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); | ||
| let addr = listener.local_addr().unwrap(); | ||
| let server = tokio::spawn(async move { | ||
| let (stream, _) = listener.accept().await.unwrap(); | ||
| let (reader, writer) = stream.into_split(); | ||
| let mut protocol = Protocol::new( | ||
| p2p::Magic::REGTEST, | ||
| bip324::Role::Responder, | ||
| None, // no garbage | ||
| None, // no decoys | ||
| reader, | ||
| writer, | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| let payload = protocol.read().await.unwrap(); | ||
| let received_message = | ||
| consensus::deserialize::<V2NetworkMessage>(payload.contents()).unwrap(); | ||
| if let NetworkMessage::Ping(x) = received_message.payload() { | ||
| let pong = V2NetworkMessage::new(NetworkMessage::Pong(*x)); | ||
| let message = consensus::serialize(&pong); | ||
| protocol.write(&Payload::genuine(message)).await.unwrap(); | ||
| println!("Pong sent, stopping server.") | ||
| } else { | ||
| panic!("Expected Ping, but received: {received_message:?}"); | ||
| } | ||
| }); | ||
|
|
||
| let stream = TcpStream::connect(addr).await.unwrap(); | ||
|
|
||
| let (reader, writer) = stream.into_split(); | ||
|
|
||
| // Initialize high-level async protocol with handshake | ||
| println!("Starting async BIP-324 handshake"); | ||
| let mut protocol = Protocol::new( | ||
| p2p::Magic::REGTEST, | ||
| bip324::Role::Initiator, | ||
| None, // no garbage | ||
| None, // no decoys | ||
| reader, | ||
| writer, | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| println!("Sending Ping using async Protocol::write()"); | ||
| let ping = V2NetworkMessage::new(NetworkMessage::Ping(45324)); | ||
| let message = consensus::serialize(&ping); | ||
| protocol.write(&Payload::genuine(message)).await.unwrap(); | ||
|
|
||
| println!("Reading response using async Protocol::read()"); | ||
| let payload = protocol.read().await.unwrap(); | ||
| let response_message = consensus::deserialize::<V2NetworkMessage>(payload.contents()).unwrap(); | ||
|
|
||
| assert_eq!(NetworkMessage::Pong(45324), *response_message.payload()); | ||
|
|
||
| println!("Successfully ping-pong message using async Protocol API!"); | ||
| server.await.unwrap(); | ||
|
|
||
| println!( | ||
| "Trying to read another message from the server, while the connection is already closed." | ||
| ); | ||
| assert!(protocol.read().await.is_err()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm the test still passed when async writing to the closed protocol. I think that should return an error too? At least it doesn't hang.. index 5b96eca..e7cb5d1 100644
--- a/protocol/tests/round_trips.rs
+++ b/protocol/tests/round_trips.rs
@@ -229,6 +229,10 @@ async fn pingpong_with_closed_connection_async() {
"Trying to read another message from the server, while the connection is already closed."
);
assert!(protocol.read().await.is_err());
+
+ let ping = V2NetworkMessage::new(NetworkMessage::Ping(45324));
+ let message = consensus::serialize(&ping);
+ protocol.write(&Payload::genuine(message)).await.unwrap();
}
#[test]Probably a good followup once this is merged. |
||
| } | ||
|
|
||
| #[test] | ||
| #[cfg(feature = "std")] | ||
| fn regtest_handshake() { | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Went with
ConnectionAbortedhere.ConnectionResetis an alternative. Happy to be convinced to useConnectionResetif someone has strong feelings about it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No strong opinion here,
ConnectionAbortedis fine with me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use
UnexpectedEof? It looks like that is what tokio bubbles up in similar scenarios.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about this too, but found the Rust stdlib docs to not to be a perfect fit for our case? https://doc.rust-lang.org/std/io/enum.ErrorKind.html#variant.UnexpectedEof
(but also don't really care too much personally)