@@ -83,7 +83,7 @@ use lightning::ln::peer_handler::SocketDescriptor as LnSocketTrait;
83
83
use lightning:: ln:: msgs:: { ChannelMessageHandler , RoutingMessageHandler } ;
84
84
use lightning:: util:: logger:: Logger ;
85
85
86
- use std:: { task, thread } ;
86
+ use std:: task;
87
87
use std:: net:: SocketAddr ;
88
88
use std:: net:: TcpStream as StdTcpStream ;
89
89
use std:: sync:: { Arc , Mutex } ;
@@ -114,11 +114,6 @@ struct Connection {
114
114
// socket. To wake it up (without otherwise changing its state, we can push a value into this
115
115
// Sender.
116
116
read_waker : mpsc:: Sender < ( ) > ,
117
- // When we are told by rust-lightning to disconnect, we can't return to rust-lightning until we
118
- // are sure we won't call any more read/write PeerManager functions with the same connection.
119
- // This is set to true if we're in such a condition (with disconnect checked before with the
120
- // top-level mutex held) and false when we can return.
121
- block_disconnect_socket : bool ,
122
117
read_paused : bool ,
123
118
rl_requested_disconnect : bool ,
124
119
id : u64 ,
@@ -153,31 +148,24 @@ impl Connection {
153
148
} }
154
149
}
155
150
156
- macro_rules! prepare_read_write_call {
157
- ( ) => { {
158
- let mut us_lock = us. lock( ) . unwrap( ) ;
159
- if us_lock. rl_requested_disconnect {
160
- shutdown_socket!( "disconnect_socket() call from RL" , Disconnect :: CloseConnection ) ;
161
- }
162
- us_lock. block_disconnect_socket = true ;
163
- } }
164
- }
165
-
166
- let read_paused = us. lock ( ) . unwrap ( ) . read_paused ;
151
+ let read_paused = {
152
+ let us_lock = us. lock ( ) . unwrap ( ) ;
153
+ if us_lock. rl_requested_disconnect {
154
+ shutdown_socket ! ( "disconnect_socket() call from RL" , Disconnect :: CloseConnection ) ;
155
+ }
156
+ us_lock. read_paused
157
+ } ;
167
158
tokio:: select! {
168
159
v = write_avail_receiver. recv( ) => {
169
160
assert!( v. is_some( ) ) ; // We can't have dropped the sending end, its in the us Arc!
170
- prepare_read_write_call!( ) ;
171
161
if let Err ( e) = peer_manager. write_buffer_space_avail( & mut our_descriptor) {
172
162
shutdown_socket!( e, Disconnect :: CloseConnection ) ;
173
163
}
174
- us. lock( ) . unwrap( ) . block_disconnect_socket = false ;
175
164
} ,
176
165
_ = read_wake_receiver. recv( ) => { } ,
177
166
read = reader. read( & mut buf) , if !read_paused => match read {
178
167
Ok ( 0 ) => shutdown_socket!( "Connection closed" , Disconnect :: PeerDisconnected ) ,
179
168
Ok ( len) => {
180
- prepare_read_write_call!( ) ;
181
169
let read_res = peer_manager. read_event( & mut our_descriptor, & buf[ 0 ..len] ) ;
182
170
let mut us_lock = us. lock( ) . unwrap( ) ;
183
171
match read_res {
@@ -188,7 +176,6 @@ impl Connection {
188
176
} ,
189
177
Err ( e) => shutdown_socket!( e, Disconnect :: CloseConnection ) ,
190
178
}
191
- us_lock. block_disconnect_socket = false ;
192
179
} ,
193
180
Err ( e) => shutdown_socket!( e, Disconnect :: PeerDisconnected ) ,
194
181
} ,
@@ -223,7 +210,7 @@ impl Connection {
223
210
( reader, write_receiver, read_receiver,
224
211
Arc :: new ( Mutex :: new ( Self {
225
212
writer : Some ( writer) , write_avail, read_waker, read_paused : false ,
226
- block_disconnect_socket : false , rl_requested_disconnect : false ,
213
+ rl_requested_disconnect : false ,
227
214
id : ID_COUNTER . fetch_add ( 1 , Ordering :: AcqRel )
228
215
} ) ) )
229
216
}
@@ -450,18 +437,10 @@ impl peer_handler::SocketDescriptor for SocketDescriptor {
450
437
}
451
438
452
439
fn disconnect_socket ( & mut self ) {
453
- {
454
- let mut us = self . conn . lock ( ) . unwrap ( ) ;
455
- us. rl_requested_disconnect = true ;
456
- us. read_paused = true ;
457
- // Wake up the sending thread, assuming it is still alive
458
- let _ = us. write_avail . try_send ( ( ) ) ;
459
- // Happy-path return:
460
- if !us. block_disconnect_socket { return ; }
461
- }
462
- while self . conn . lock ( ) . unwrap ( ) . block_disconnect_socket {
463
- thread:: yield_now ( ) ;
464
- }
440
+ let mut us = self . conn . lock ( ) . unwrap ( ) ;
441
+ us. rl_requested_disconnect = true ;
442
+ // Wake up the sending thread, assuming it is still alive
443
+ let _ = us. write_avail . try_send ( ( ) ) ;
465
444
}
466
445
}
467
446
impl Clone for SocketDescriptor {
0 commit comments