@@ -271,16 +271,32 @@ 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.
273273pub 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
276276 let handle_opt = if let Ok ( initial_send) = peer_manager. new_outbound_connection ( their_node_id, SocketDescriptor :: new ( us. clone ( ) ) ) {
277277 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 {
284300 Connection :: schedule_read ( peer_manager, us, reader, read_receiver, write_receiver) . await ;
285301 }
286302 } ) )
@@ -521,8 +537,7 @@ mod tests {
521537 }
522538 }
523539
524- #[ tokio:: test( threaded_scheduler) ]
525- async fn basic_connection_test ( ) {
540+ async fn do_basic_connection_test ( ) {
526541 let secp_ctx = Secp256k1 :: new ( ) ;
527542 let a_key = SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ;
528543 let b_key = SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ;
@@ -587,4 +602,13 @@ mod tests {
587602 fut_a. await ;
588603 fut_b. await ;
589604 }
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+ }
590614}
0 commit comments