Skip to content

Commit 8708f5d

Browse files
committed
No longer block disconnect_socket calls in lightning-net-tokio
See the previous commit for more information.
1 parent 6a030d6 commit 8708f5d

File tree

1 file changed

+12
-33
lines changed

1 file changed

+12
-33
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ struct Connection {
114114
// socket. To wake it up (without otherwise changing its state, we can push a value into this
115115
// Sender.
116116
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,
122117
read_paused: bool,
123118
rl_requested_disconnect: bool,
124119
id: u64,
@@ -153,31 +148,24 @@ impl Connection {
153148
} }
154149
}
155150

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+
};
167158
tokio::select! {
168159
v = write_avail_receiver.recv() => {
169160
assert!(v.is_some()); // We can't have dropped the sending end, its in the us Arc!
170-
prepare_read_write_call!();
171161
if let Err(e) = peer_manager.write_buffer_space_avail(&mut our_descriptor) {
172162
shutdown_socket!(e, Disconnect::CloseConnection);
173163
}
174-
us.lock().unwrap().block_disconnect_socket = false;
175164
},
176165
_ = read_wake_receiver.recv() => {},
177166
read = reader.read(&mut buf), if !read_paused => match read {
178167
Ok(0) => shutdown_socket!("Connection closed", Disconnect::PeerDisconnected),
179168
Ok(len) => {
180-
prepare_read_write_call!();
181169
let read_res = peer_manager.read_event(&mut our_descriptor, &buf[0..len]);
182170
let mut us_lock = us.lock().unwrap();
183171
match read_res {
@@ -188,7 +176,6 @@ impl Connection {
188176
},
189177
Err(e) => shutdown_socket!(e, Disconnect::CloseConnection),
190178
}
191-
us_lock.block_disconnect_socket = false;
192179
},
193180
Err(e) => shutdown_socket!(e, Disconnect::PeerDisconnected),
194181
},
@@ -223,7 +210,7 @@ impl Connection {
223210
(reader, write_receiver, read_receiver,
224211
Arc::new(Mutex::new(Self {
225212
writer: Some(writer), write_avail, read_waker, read_paused: false,
226-
block_disconnect_socket: false, rl_requested_disconnect: false,
213+
rl_requested_disconnect: false,
227214
id: ID_COUNTER.fetch_add(1, Ordering::AcqRel)
228215
})))
229216
}
@@ -450,18 +437,10 @@ impl peer_handler::SocketDescriptor for SocketDescriptor {
450437
}
451438

452439
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(());
465444
}
466445
}
467446
impl Clone for SocketDescriptor {

0 commit comments

Comments
 (0)