@@ -255,16 +255,32 @@ pub fn setup_inbound<CMH: ChannelMessageHandler + 'static>(peer_manager: Arc<pee
255
255
///
256
256
/// See the module-level documentation for how to handle the event_notify mpsc::Sender.
257
257
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) ;
259
259
260
260
let handle_opt = if let Ok ( initial_send) = peer_manager. new_outbound_connection ( their_node_id, SocketDescriptor :: new ( us. clone ( ) ) ) {
261
261
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 {
268
284
Connection :: schedule_read ( peer_manager, us, reader, read_receiver, write_receiver) . await ;
269
285
}
270
286
} ) )
@@ -505,8 +521,7 @@ mod tests {
505
521
}
506
522
}
507
523
508
- #[ tokio:: test( threaded_scheduler) ]
509
- async fn basic_connection_test ( ) {
524
+ async fn do_basic_connection_test ( ) {
510
525
let secp_ctx = Secp256k1 :: new ( ) ;
511
526
let a_key = SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ;
512
527
let b_key = SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ;
@@ -571,4 +586,13 @@ mod tests {
571
586
fut_a. await ;
572
587
fut_b. await ;
573
588
}
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
+ }
574
598
}
0 commit comments