@@ -70,7 +70,7 @@ use lightning::ln::peer_handler;
70
70
use lightning:: ln:: peer_handler:: SocketDescriptor as LnSocketTrait ;
71
71
use lightning:: ln:: msgs:: ChannelMessageHandler ;
72
72
73
- use std:: task;
73
+ use std:: { task, thread } ;
74
74
use std:: net:: SocketAddr ;
75
75
use std:: sync:: { Arc , Mutex , MutexGuard } ;
76
76
use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
@@ -101,6 +101,11 @@ struct Connection {
101
101
// socket. To wake it up (without otherwise changing its state, we can push a value into this
102
102
// Sender.
103
103
read_waker : mpsc:: Sender < ( ) > ,
104
+ // When we are told by rust-lightning to disconnect, we can't return to rust-lightning until we
105
+ // are sure we won't call any more read/write PeerManager functions with the same connection.
106
+ // This is set to true if we're in such a condition (with disconnect checked before with the
107
+ // top-level mutex held) and false when we can return.
108
+ block_disconnect_socket : bool ,
104
109
read_paused : bool ,
105
110
rl_requested_disconnect : bool ,
106
111
id : u64 ,
@@ -137,35 +142,43 @@ impl Connection {
137
142
} }
138
143
}
139
144
145
+ macro_rules! prepare_read_write_call {
146
+ ( ) => { {
147
+ let mut us_lock = us. lock( ) . unwrap( ) ;
148
+ if us_lock. rl_requested_disconnect {
149
+ shutdown_socket!( "disconnect_socket() call from RL" , Disconnect :: CloseConnection ) ;
150
+ }
151
+ us_lock. block_disconnect_socket = true ;
152
+ } }
153
+ }
154
+
140
155
let read_paused = us. lock ( ) . unwrap ( ) . read_paused ;
141
156
tokio:: select! {
142
157
v = write_avail_receiver. recv( ) => {
143
158
assert!( v. is_some( ) ) ; // We can't have dropped the sending end, its in the us Arc!
144
- if us. lock( ) . unwrap( ) . rl_requested_disconnect {
145
- shutdown_socket!( "disconnect_socket() call from RL" , Disconnect :: CloseConnection ) ;
146
- }
159
+ prepare_read_write_call!( ) ;
147
160
if let Err ( e) = peer_manager. write_buffer_space_avail( & mut our_descriptor) {
148
161
shutdown_socket!( e, Disconnect :: CloseConnection ) ;
149
162
}
163
+ us. lock( ) . unwrap( ) . block_disconnect_socket = false ;
150
164
} ,
151
165
_ = read_wake_receiver. recv( ) => { } ,
152
166
read = reader. read( & mut buf) , if !read_paused => match read {
153
167
Ok ( 0 ) => shutdown_socket!( "Connection closed" , Disconnect :: PeerDisconnected ) ,
154
168
Ok ( len) => {
155
- if us. lock( ) . unwrap( ) . rl_requested_disconnect {
156
- shutdown_socket!( "disconnect_socket() call from RL" , Disconnect :: CloseConnection ) ;
157
- }
169
+ prepare_read_write_call!( ) ;
158
170
let read_res = peer_manager. read_event( & mut our_descriptor, & buf[ 0 ..len] ) ;
171
+ let mut us_lock = us. lock( ) . unwrap( ) ;
159
172
match read_res {
160
173
Ok ( pause_read) => {
161
- let mut us_lock = us. lock( ) . unwrap( ) ;
162
174
if pause_read {
163
175
us_lock. read_paused = true ;
164
176
}
165
177
Self :: event_trigger( & mut us_lock) ;
166
178
} ,
167
179
Err ( e) => shutdown_socket!( e, Disconnect :: CloseConnection ) ,
168
180
}
181
+ us_lock. block_disconnect_socket = false ;
169
182
} ,
170
183
Err ( e) => shutdown_socket!( e, Disconnect :: PeerDisconnected ) ,
171
184
} ,
@@ -197,8 +210,8 @@ impl Connection {
197
210
198
211
( reader, write_receiver, read_receiver,
199
212
Arc :: new ( Mutex :: new ( Self {
200
- writer : Some ( writer) , event_notify, write_avail, read_waker,
201
- read_paused : false , rl_requested_disconnect : false ,
213
+ writer : Some ( writer) , event_notify, write_avail, read_waker, read_paused : false ,
214
+ block_disconnect_socket : false , rl_requested_disconnect : false ,
202
215
id : ID_COUNTER . fetch_add ( 1 , Ordering :: AcqRel )
203
216
} ) ) )
204
217
}
@@ -405,15 +418,18 @@ impl peer_handler::SocketDescriptor for SocketDescriptor {
405
418
}
406
419
407
420
fn disconnect_socket ( & mut self ) {
408
- let mut us = self . conn . lock ( ) . unwrap ( ) ;
409
- us. rl_requested_disconnect = true ;
410
- us. read_paused = true ;
411
- // Wake up the sending thread, assuming it is still alive
412
- let _ = us. write_avail . try_send ( ( ) ) ;
413
- // TODO: There's a race where we don't meet the requirements of disconnect_socket if the
414
- // read task is about to call a PeerManager function (eg read_event or write_event).
415
- // Ideally we need to release the us lock and block until we have confirmation from the
416
- // read task that it has broken out of its main loop.
421
+ {
422
+ let mut us = self . conn . lock ( ) . unwrap ( ) ;
423
+ us. rl_requested_disconnect = true ;
424
+ us. read_paused = true ;
425
+ // Wake up the sending thread, assuming it is still alive
426
+ let _ = us. write_avail . try_send ( ( ) ) ;
427
+ // Happy-path return:
428
+ if !us. block_disconnect_socket { return ; }
429
+ }
430
+ while self . conn . lock ( ) . unwrap ( ) . block_disconnect_socket {
431
+ thread:: yield_now ( ) ;
432
+ }
417
433
}
418
434
}
419
435
impl Clone for SocketDescriptor {
0 commit comments