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