Skip to content

Commit

Permalink
Just use plain blocks for mutex critical sections
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Apr 23, 2023
1 parent 2f29a3d commit b281cb6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
3 changes: 0 additions & 3 deletions zebra-network/src/peer/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,6 @@ where
// It is ok to wait for the lock here, because handshakes have a short
// timeout, and the async mutex will be released when the task times
// out.
//
// START CRITICAL SECTION
{
let mut locked_nonces = nonces.lock().await;

Expand Down Expand Up @@ -627,7 +625,6 @@ where

std::mem::drop(locked_nonces);
}
// END CRITICAL SECTION

// Don't leak our exact clock skew to our peers. On the other hand,
// we can't deviate too much, or zcashd will get confused.
Expand Down
50 changes: 23 additions & 27 deletions zebra-network/src/peer_set/initialize/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,8 +1140,7 @@ async fn self_connections_should_fail() {
.await;

// Insert our own address into the address book, and make sure it works
// BEGIN CRITICAL SECTION
let real_self_listener = {
let (real_self_listener, updated_addr) = {
let mut unlocked_address_book = address_book
.lock()
.expect("unexpected panic in address book");
Expand All @@ -1158,37 +1157,35 @@ async fn self_connections_should_fail() {
);

std::mem::drop(unlocked_address_book);
// END CRITICAL SECTION

// Make sure we modified the address book correctly
assert!(
updated_addr.is_some(),
"inserting our own address into the address book failed: {real_self_listener:?}"
);
assert_eq!(
updated_addr.unwrap().addr(),
real_self_listener.addr(),
"wrong address inserted into address book"
);
assert_ne!(
updated_addr.unwrap().addr().ip(),
Ipv4Addr::UNSPECIFIED,
"invalid address inserted into address book: ip must be valid for inbound connections"
);
assert_ne!(
updated_addr.unwrap().addr().port(),
0,
"invalid address inserted into address book: port must be valid for inbound connections"
);

real_self_listener
(real_self_listener, updated_addr)
};

// Make sure we modified the address book correctly
assert!(
updated_addr.is_some(),
"inserting our own address into the address book failed: {real_self_listener:?}"
);
assert_eq!(
updated_addr.unwrap().addr(),
real_self_listener.addr(),
"wrong address inserted into address book"
);
assert_ne!(
updated_addr.unwrap().addr().ip(),
Ipv4Addr::UNSPECIFIED,
"invalid address inserted into address book: ip must be valid for inbound connections"
);
assert_ne!(
updated_addr.unwrap().addr().port(),
0,
"invalid address inserted into address book: port must be valid for inbound connections"
);

// Wait until the crawler has tried at least one self-connection
tokio::time::sleep(TEST_CRAWL_NEW_PEER_INTERVAL * 3).await;

// Check that the self-connection failed
// BEGIN CRITICAL SECTION
let self_connection_status = {
let mut unlocked_address_book = address_book
.lock()
Expand All @@ -1199,7 +1196,6 @@ async fn self_connections_should_fail() {
.expect("unexpected dropped listener address in address book");

std::mem::drop(unlocked_address_book);
// END CRITICAL SECTION

self_connection_status
};
Expand Down

0 comments on commit b281cb6

Please sign in to comment.