Skip to content

Commit d66c70e

Browse files
authored
Merge pull request #1964 from TheBlueMatt/2023-01-no-debug-panics
Use test/_test_utils to enable single-threaded debug assertions
2 parents 50d1260 + 7a9bea1 commit d66c70e

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

fuzz/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ libfuzzer_fuzz = ["libfuzzer-sys"]
1818
stdin_fuzz = []
1919

2020
[dependencies]
21-
lightning = { path = "../lightning", features = ["regex", "hashbrown"] }
21+
lightning = { path = "../lightning", features = ["regex", "hashbrown", "_test_utils"] }
2222
lightning-rapid-gossip-sync = { path = "../lightning-rapid-gossip-sync" }
2323
bitcoin = { version = "0.29.0", features = ["secp-lowmemory"] }
2424
hex = "0.3"

lightning-block-sync/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ serde_json = { version = "1.0", optional = true }
2727
chunked_transfer = { version = "1.4", optional = true }
2828

2929
[dev-dependencies]
30+
lightning = { version = "0.0.113", path = "../lightning", features = ["_test_utils"] }
3031
tokio = { version = "~1.14", features = [ "macros", "rt" ] }

lightning-net-tokio/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ tokio = { version = "1.0", features = [ "io-util", "macros", "rt", "sync", "net"
2121

2222
[dev-dependencies]
2323
tokio = { version = "~1.14", features = [ "io-util", "macros", "rt", "rt-multi-thread", "sync", "net", "time" ] }
24+
lightning = { version = "0.0.113", path = "../lightning", features = ["_test_utils"] }

lightning-net-tokio/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pub fn setup_inbound<PM, CMH, RMH, OMH, L, UMH>(
301301
{
302302
let remote_addr = get_addr_from_stream(&stream);
303303
let (reader, write_receiver, read_receiver, us) = Connection::new(stream);
304-
#[cfg(debug_assertions)]
304+
#[cfg(test)]
305305
let last_us = Arc::clone(&us);
306306

307307
let handle_opt = if let Ok(_) = peer_manager.new_inbound_connection(SocketDescriptor::new(us.clone()), remote_addr) {
@@ -322,8 +322,8 @@ pub fn setup_inbound<PM, CMH, RMH, OMH, L, UMH>(
322322
// socket shutdown(). Still, as a check during testing, to make sure tokio doesn't
323323
// keep too many wakers around, this makes sense. The race should be rare (we do
324324
// some work after shutdown()) and an error would be a major memory leak.
325-
#[cfg(debug_assertions)]
326-
assert!(Arc::try_unwrap(last_us).is_ok());
325+
#[cfg(test)]
326+
debug_assert!(Arc::try_unwrap(last_us).is_ok());
327327
}
328328
}
329329
}
@@ -355,7 +355,7 @@ pub fn setup_outbound<PM, CMH, RMH, OMH, L, UMH>(
355355
{
356356
let remote_addr = get_addr_from_stream(&stream);
357357
let (reader, mut write_receiver, read_receiver, us) = Connection::new(stream);
358-
#[cfg(debug_assertions)]
358+
#[cfg(test)]
359359
let last_us = Arc::clone(&us);
360360
let handle_opt = if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone()), remote_addr) {
361361
Some(tokio::spawn(async move {
@@ -401,8 +401,8 @@ pub fn setup_outbound<PM, CMH, RMH, OMH, L, UMH>(
401401
// socket shutdown(). Still, as a check during testing, to make sure tokio doesn't
402402
// keep too many wakers around, this makes sense. The race should be rare (we do
403403
// some work after shutdown()) and an error would be a major memory leak.
404-
#[cfg(debug_assertions)]
405-
assert!(Arc::try_unwrap(last_us).is_ok());
404+
#[cfg(test)]
405+
debug_assert!(Arc::try_unwrap(last_us).is_ok());
406406
}
407407
}
408408
}

lightning/src/ln/channelmanager.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1153,12 +1153,12 @@ macro_rules! handle_error {
11531153
match $internal {
11541154
Ok(msg) => Ok(msg),
11551155
Err(MsgHandleErrInternal { err, chan_id, shutdown_finish }) => {
1156-
#[cfg(debug_assertions)]
1156+
#[cfg(any(feature = "_test_utils", test))]
11571157
{
11581158
// In testing, ensure there are no deadlocks where the lock is already held upon
11591159
// entering the macro.
1160-
assert!($self.pending_events.try_lock().is_ok());
1161-
assert!($self.per_peer_state.try_write().is_ok());
1160+
debug_assert!($self.pending_events.try_lock().is_ok());
1161+
debug_assert!($self.per_peer_state.try_write().is_ok());
11621162
}
11631163

11641164
let mut msg_events = Vec::with_capacity(2);
@@ -1193,7 +1193,7 @@ macro_rules! handle_error {
11931193
let mut peer_state = peer_state_mutex.lock().unwrap();
11941194
peer_state.pending_msg_events.append(&mut msg_events);
11951195
}
1196-
#[cfg(debug_assertions)]
1196+
#[cfg(any(feature = "_test_utils", test))]
11971197
{
11981198
if let None = per_peer_state.get(&$counterparty_node_id) {
11991199
// This shouldn't occour in tests unless an unkown counterparty_node_id
@@ -1206,10 +1206,10 @@ macro_rules! handle_error {
12061206
=> {
12071207
assert_eq!(*data, expected_error_str);
12081208
if let Some((err_channel_id, _user_channel_id)) = chan_id {
1209-
assert_eq!(*channel_id, err_channel_id);
1209+
debug_assert_eq!(*channel_id, err_channel_id);
12101210
}
12111211
}
1212-
_ => panic!("Unexpected event"),
1212+
_ => debug_assert!(false, "Unexpected event"),
12131213
}
12141214
}
12151215
}
@@ -3565,7 +3565,7 @@ where
35653565
/// Fails an HTLC backwards to the sender of it to us.
35663566
/// Note that we do not assume that channels corresponding to failed HTLCs are still available.
35673567
fn fail_htlc_backwards_internal(&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason, destination: HTLCDestination) {
3568-
#[cfg(debug_assertions)]
3568+
#[cfg(any(feature = "_test_utils", test))]
35693569
{
35703570
// Ensure that no peer state channel storage lock is not held when calling this
35713571
// function.
@@ -3574,7 +3574,7 @@ where
35743574
// this function with any `per_peer_state` peer lock aquired would.
35753575
let per_peer_state = self.per_peer_state.read().unwrap();
35763576
for (_, peer) in per_peer_state.iter() {
3577-
assert!(peer.try_lock().is_ok());
3577+
debug_assert!(peer.try_lock().is_ok());
35783578
}
35793579
}
35803580

0 commit comments

Comments
 (0)