@@ -194,11 +194,8 @@ pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
194
194
/// indicating that read events on this descriptor should resume. A resume_read of false does
195
195
/// *not* imply that further read events should be paused.
196
196
fn send_data ( & mut self , data : & [ u8 ] , resume_read : bool ) -> usize ;
197
- /// Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
198
- /// more calls to write_buffer_space_avail, read_event or socket_disconnected may be made with
199
- /// this descriptor. No socket_disconnected call should be generated as a result of this call,
200
- /// though races may occur whereby disconnect_socket is called after a call to
201
- /// socket_disconnected but prior to socket_disconnected returning.
197
+ /// Disconnect the socket pointed to by this SocketDescriptor.
198
+ /// No [`PeerManager::socket_disconnected`] call need be generated as a result of this call.
202
199
fn disconnect_socket ( & mut self ) ;
203
200
}
204
201
@@ -616,7 +613,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
616
613
pub fn write_buffer_space_avail ( & self , descriptor : & mut Descriptor ) -> Result < ( ) , PeerHandleError > {
617
614
let mut peers = self . peers . lock ( ) . unwrap ( ) ;
618
615
match peers. peers . get_mut ( descriptor) {
619
- None => panic ! ( "Descriptor for write_event is not already known to PeerManager" ) ,
616
+ None => {
617
+ // This is most likely a simple race condition where the user found that the socket
618
+ // was writeable, then we told the user to `disconnect_socket()`, then they called
619
+ // this method. Return an error to make sure we get disconnected.
620
+ return Err ( PeerHandleError { no_connection_possible : false } ) ;
621
+ } ,
620
622
Some ( peer) => {
621
623
peer. awaiting_write_event = false ;
622
624
self . do_attempt_write_data ( descriptor, peer) ;
@@ -636,7 +638,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
636
638
/// If Ok(true) is returned, further read_events should not be triggered until a send_data call
637
639
/// on this file descriptor has resume_read set (preventing DoS issues in the send buffer).
638
640
///
639
- /// Panics if the descriptor was not previously registered in a new_*_connection event.
640
641
pub fn read_event ( & self , peer_descriptor : & mut Descriptor , data : & [ u8 ] ) -> Result < bool , PeerHandleError > {
641
642
match self . do_read_event ( peer_descriptor, data) {
642
643
Ok ( res) => Ok ( res) ,
@@ -664,7 +665,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
664
665
let mut msgs_to_forward = Vec :: new ( ) ;
665
666
let mut peer_node_id = None ;
666
667
let pause_read = match peers. peers . get_mut ( peer_descriptor) {
667
- None => panic ! ( "Descriptor for read_event is not already known to PeerManager" ) ,
668
+ None => {
669
+ // This is most likely a simple race condition where the user read some bytes
670
+ // from the socket, then we told the user to `disconnect_socket()`, then they
671
+ // called this method. Return an error to make sure we get disconnected.
672
+ return Err ( PeerHandleError { no_connection_possible : false } ) ;
673
+ } ,
668
674
Some ( peer) => {
669
675
assert ! ( peer. pending_read_buffer. len( ) > 0 ) ;
670
676
assert ! ( peer. pending_read_buffer. len( ) > peer. pending_read_buffer_pos) ;
@@ -1292,12 +1298,9 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
1292
1298
1293
1299
/// Indicates that the given socket descriptor's connection is now closed.
1294
1300
///
1295
- /// This must only be called if the socket has been disconnected by the peer or your own
1296
- /// decision to disconnect it and must NOT be called in any case where other parts of this
1297
- /// library (eg PeerHandleError, explicit disconnect_socket calls) instruct you to disconnect
1298
- /// the peer.
1299
- ///
1300
- /// Panics if the descriptor was not previously registered in a successful new_*_connection event.
1301
+ /// This need only be called if the socket has been disconnected by the peer or your own
1302
+ /// decision to disconnect it and may be skipped in any case where other parts of this library
1303
+ /// (eg PeerHandleError, explicit disconnect_socket calls) instruct you to disconnect the peer.
1301
1304
pub fn socket_disconnected ( & self , descriptor : & Descriptor ) {
1302
1305
self . disconnect_event_internal ( descriptor, false ) ;
1303
1306
}
@@ -1306,7 +1309,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
1306
1309
let mut peers = self . peers . lock ( ) . unwrap ( ) ;
1307
1310
let peer_option = peers. peers . remove ( descriptor) ;
1308
1311
match peer_option {
1309
- None => panic ! ( "Descriptor for disconnect_event is not already known to PeerManager" ) ,
1312
+ None => {
1313
+ // This is most likely a simple race condition where the user found that the socket
1314
+ // was disconnected, then we told the user to `disconnect_socket()`, then they
1315
+ // called this method. Either way we're disconnected, return.
1316
+ } ,
1310
1317
Some ( peer) => {
1311
1318
match peer. their_node_id {
1312
1319
Some ( node_id) => {
0 commit comments