Skip to content

Commit 4d16c36

Browse files
committed
Fix (and test) net-tokio outbound conns without a threaded env
1 parent 9610686 commit 4d16c36

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
@@ -255,16 +255,32 @@ pub fn setup_inbound<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<pee
255255
///
256256
/// See the module-level documentation for how to handle the event_notify mpsc::Sender.
257257
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=()> {
258-
let (reader, write_receiver, read_receiver, us) = Connection::new(event_notify, stream);
258+
let (reader, mut write_receiver, read_receiver, us) = Connection::new(event_notify, stream);
259259

260260
let handle_opt = if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone())) {
261261
Some(tokio::spawn(async move {
262-
if SocketDescriptor::new(us.clone()).send_data(&initial_send, true) != initial_send.len() {
263-
// We should essentially always have enough room in a TCP socket buffer to send the
264-
// initial 10s of bytes, if not, just give up as hopeless.
265-
eprintln!("Failed to write first full message to socket!");
266-
peer_manager.socket_disconnected(&SocketDescriptor::new(Arc::clone(&us)));
267-
} else {
262+
// We should essentially always have enough room in a TCP socket buffer to send the
263+
// initial 10s of bytes, however, tokio running in single-threaded mode will always
264+
// fail writes and wake us back up later to write, so we handle a Pending, but still
265+
// expect to write the full set of bytes at once and use a relatively tight timeout.
266+
if let Ok(Ok(())) = tokio::time::timeout(Duration::from_millis(100), async {
267+
loop {
268+
match SocketDescriptor::new(us.clone()).send_data(&initial_send, true) {
269+
v if v == initial_send.len() => break Ok(()),
270+
0 => {
271+
write_receiver.recv().await;
272+
// In theory we could check for if we've been instructed to disconnect
273+
// the peer here, but its OK to just skip it - we'll check for it in
274+
// schedule_read prior to any relevant calls into RL.
275+
},
276+
_ => {
277+
eprintln!("Failed to write first full message to socket!");
278+
peer_manager.socket_disconnected(&SocketDescriptor::new(Arc::clone(&us)));
279+
break Err(());
280+
}
281+
}
282+
}
283+
}).await {
268284
Connection::schedule_read(peer_manager, us, reader, read_receiver, write_receiver).await;
269285
}
270286
}))
@@ -505,8 +521,7 @@ mod tests {
505521
}
506522
}
507523

508-
#[tokio::test(threaded_scheduler)]
509-
async fn basic_connection_test() {
524+
async fn do_basic_connection_test() {
510525
let secp_ctx = Secp256k1::new();
511526
let a_key = SecretKey::from_slice(&[1; 32]).unwrap();
512527
let b_key = SecretKey::from_slice(&[1; 32]).unwrap();
@@ -571,4 +586,13 @@ mod tests {
571586
fut_a.await;
572587
fut_b.await;
573588
}
589+
590+
#[tokio::test(threaded_scheduler)]
591+
async fn basic_threaded_connection_test() {
592+
do_basic_connection_test().await;
593+
}
594+
#[tokio::test]
595+
async fn basic_unthreaded_connection_test() {
596+
do_basic_connection_test().await;
597+
}
574598
}

0 commit comments

Comments
 (0)