@@ -271,16 +271,32 @@ pub fn setup_inbound<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<pee
271
271
///
272
272
/// See the module-level documentation for how to handle the event_notify mpsc::Sender.
273
273
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) ;
275
275
276
276
let handle_opt = if let Ok ( initial_send) = peer_manager. new_outbound_connection ( their_node_id, SocketDescriptor :: new ( us. clone ( ) ) ) {
277
277
Some ( tokio:: spawn ( async move {
278
- if SocketDescriptor :: new ( us. clone ( ) ) . send_data ( & initial_send, true ) != initial_send. len ( ) {
279
- // We should essentially always have enough room in a TCP socket buffer to send the
280
- // initial 10s of bytes, if not, just give up as hopeless.
281
- eprintln ! ( "Failed to write first full message to socket!" ) ;
282
- peer_manager. socket_disconnected ( & SocketDescriptor :: new ( Arc :: clone ( & us) ) ) ;
283
- } else {
278
+ // We should essentially always have enough room in a TCP socket buffer to send the
279
+ // initial 10s of bytes, however, tokio running in single-threaded mode will always
280
+ // fail writes and wake us back up later to write, so we handle a Pending, but still
281
+ // expect to write the full set of bytes at once and use a relatively tight timeout.
282
+ if let Ok ( Ok ( ( ) ) ) = tokio:: time:: timeout ( Duration :: from_millis ( 100 ) , async {
283
+ loop {
284
+ match SocketDescriptor :: new ( us. clone ( ) ) . send_data ( & initial_send, true ) {
285
+ v if v == initial_send. len ( ) => break Ok ( ( ) ) ,
286
+ 0 => {
287
+ write_receiver. recv ( ) . await ;
288
+ // In theory we could check for if we've been instructed to disconnect
289
+ // the peer here, but its OK to just skip it - we'll check for it in
290
+ // schedule_read prior to any relevant calls into RL.
291
+ } ,
292
+ _ => {
293
+ eprintln ! ( "Failed to write first full message to socket!" ) ;
294
+ peer_manager. socket_disconnected ( & SocketDescriptor :: new ( Arc :: clone ( & us) ) ) ;
295
+ break Err ( ( ) ) ;
296
+ }
297
+ }
298
+ }
299
+ } ) . await {
284
300
Connection :: schedule_read ( peer_manager, us, reader, read_receiver, write_receiver) . await ;
285
301
}
286
302
} ) )
@@ -521,8 +537,7 @@ mod tests {
521
537
}
522
538
}
523
539
524
- #[ tokio:: test( threaded_scheduler) ]
525
- async fn basic_connection_test ( ) {
540
+ async fn do_basic_connection_test ( ) {
526
541
let secp_ctx = Secp256k1 :: new ( ) ;
527
542
let a_key = SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ;
528
543
let b_key = SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ;
@@ -587,4 +602,13 @@ mod tests {
587
602
fut_a. await ;
588
603
fut_b. await ;
589
604
}
605
+
606
+ #[ tokio:: test( threaded_scheduler) ]
607
+ async fn basic_threaded_connection_test ( ) {
608
+ do_basic_connection_test ( ) . await ;
609
+ }
610
+ #[ tokio:: test]
611
+ async fn basic_unthreaded_connection_test ( ) {
612
+ do_basic_connection_test ( ) . await ;
613
+ }
590
614
}
0 commit comments