Skip to content

Commit daee256

Browse files
committed
Fix (and test) net-tokio outbound conns without a threaded env
1 parent 6e82898 commit daee256

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,34 @@ pub fn setup_inbound<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<pee
271271
///
272272
/// See the module-level documentation for how to handle the event_notify mpsc::Sender.
273273
pub fn setup_outbound<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<peer_handler::PeerManager<SocketDescriptor, Arc<CMH>>>, event_notify: mpsc::Sender<()>, their_node_id: PublicKey, stream: TcpStream) -> impl std::future::Future<Output=()> {
274-
let (reader, write_receiver, read_receiver, us) = Connection::new(event_notify, stream);
274+
let (reader, mut write_receiver, read_receiver, us) = Connection::new(event_notify, stream);
275275
#[cfg(debug_assertions)]
276276
let last_us = Arc::clone(&us);
277277

278278
let handle_opt = if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone())) {
279279
Some(tokio::spawn(async move {
280-
if SocketDescriptor::new(us.clone()).send_data(&initial_send, true) != initial_send.len() {
281-
// We should essentially always have enough room in a TCP socket buffer to send the
282-
// initial 10s of bytes, if not, just give up as hopeless.
283-
eprintln!("Failed to write first full message to socket!");
284-
peer_manager.socket_disconnected(&SocketDescriptor::new(Arc::clone(&us)));
285-
} else {
280+
// We should essentially always have enough room in a TCP socket buffer to send the
281+
// initial 10s of bytes, however, tokio running in single-threaded mode will always
282+
// fail writes and wake us back up later to write, so we handle a Pending, but still
283+
// expect to write the full set of bytes at once and use a relatively tight timeout.
284+
if let Ok(Ok(())) = tokio::time::timeout(Duration::from_millis(100), async {
285+
loop {
286+
match SocketDescriptor::new(us.clone()).send_data(&initial_send, true) {
287+
v if v == initial_send.len() => break Ok(()),
288+
0 => {
289+
write_receiver.recv().await;
290+
// In theory we could check for if we've been instructed to disconnect
291+
// the peer here, but its OK to just skip it - we'll check for it in
292+
// schedule_read prior to any relevant calls into RL.
293+
},
294+
_ => {
295+
eprintln!("Failed to write first full message to socket!");
296+
peer_manager.socket_disconnected(&SocketDescriptor::new(Arc::clone(&us)));
297+
break Err(());
298+
}
299+
}
300+
}
301+
}).await {
286302
Connection::schedule_read(peer_manager, us, reader, read_receiver, write_receiver).await;
287303
}
288304
}))
@@ -531,8 +547,7 @@ mod tests {
531547
}
532548
}
533549

534-
#[tokio::test(threaded_scheduler)]
535-
async fn basic_connection_test() {
550+
async fn do_basic_connection_test() {
536551
let secp_ctx = Secp256k1::new();
537552
let a_key = SecretKey::from_slice(&[1; 32]).unwrap();
538553
let b_key = SecretKey::from_slice(&[1; 32]).unwrap();
@@ -597,4 +612,13 @@ mod tests {
597612
fut_a.await;
598613
fut_b.await;
599614
}
615+
616+
#[tokio::test(threaded_scheduler)]
617+
async fn basic_threaded_connection_test() {
618+
do_basic_connection_test().await;
619+
}
620+
#[tokio::test]
621+
async fn basic_unthreaded_connection_test() {
622+
do_basic_connection_test().await;
623+
}
600624
}

0 commit comments

Comments
 (0)