Slightly reduce race conditions in NioPeerHandler
[ldk-java] / src / main / java / org / ldk / batteries / NioPeerHandler.java
1 package org.ldk.batteries;
2
3 import org.ldk.structs.*;
4
5 import java.io.IOException;
6 import java.net.SocketAddress;
7 import java.net.StandardSocketOptions;
8 import java.nio.ByteBuffer;
9 import java.nio.channels.*;
10
11 /**
12  * A NioPeerHandler maps LDK's PeerHandler to Java's NIO I/O interface. It spawns a single background thread which
13  * processes socket events and provides the data to LDK for decryption and processing.
14  */
15 public class NioPeerHandler {
16     private static class Peer {
17         SocketDescriptor descriptor;
18         // When we are told by LDK to disconnect, we can't return to LDK until we are sure
19         // won't call any more read/write PeerManager functions with the same connection.
20         // This is set to true if we're in such a condition (with disconnect checked
21         // before with the Peer monitor lock held) and false when we can return.
22         boolean block_disconnect_socket = false;
23         // Indicates LDK told us to disconnect this peer, and thus we should not call socket_disconnected.
24         boolean disconnect_requested = false;
25         SelectionKey key;
26     }
27
28     // Android's java.nio implementation has a big lock inside the selector, preventing any concurrent access to it.
29     // This appears to largely defeat the entire purpose of java.nio, but we work around it here by explicitly checking
30     // for an Android environment and passing any selector access on any thread other than our internal one through
31     // do_selector_action, which wakes up the selector before accessing it.
32     private static boolean IS_ANDROID;
33     static {
34         IS_ANDROID = System.getProperty("java.vendor").toLowerCase().contains("android");
35     }
36     private boolean wakeup_selector = false;
37     private interface SelectorCall {
38         void meth() throws IOException;
39     }
40     private void do_selector_action(SelectorCall meth) throws IOException {
41         if (IS_ANDROID) {
42             wakeup_selector = true;
43             this.selector.wakeup();
44             synchronized (this.selector) {
45                 meth.meth();
46                 wakeup_selector = false;
47             }
48         } else {
49             meth.meth();
50         }
51     }
52
53     private Peer setup_socket(SocketChannel chan) throws IOException {
54         chan.configureBlocking(false);
55         // Lightning tends to send a number of small messages back and forth between peers quickly, which Nagle is
56         // particularly bad at handling, so we disable it here.
57         chan.setOption(StandardSocketOptions.TCP_NODELAY, true);
58         long our_id;
59         synchronized (this) {
60             this.socket_id = this.socket_id + 1;
61             our_id = this.socket_id;
62         }
63
64         final Peer peer = new Peer();
65         SocketDescriptor descriptor = SocketDescriptor.new_impl(new SocketDescriptor.SocketDescriptorInterface() {
66             @Override
67             public long send_data(byte[] data, boolean resume_read) {
68                 try {
69                     if (resume_read) {
70                         do_selector_action(() -> peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_READ));
71                     }
72                     long written = chan.write(ByteBuffer.wrap(data));
73                     if (written != data.length) {
74                         do_selector_action(() -> peer.key.interestOps(peer.key.interestOps() | SelectionKey.OP_WRITE));
75                     }
76                     return written;
77                 } catch (IOException e) {
78                     // Most likely the socket is disconnected, let the background thread handle it.
79                     return 0;
80                 }
81             }
82
83             @Override
84             public void disconnect_socket() {
85                 synchronized (peer) {
86                     peer.disconnect_requested = true;
87                 }
88                 try {
89                     do_selector_action(() -> {
90                         peer.key.cancel();
91                         peer.key.channel().close();
92                     });
93                 } catch (IOException ignored) { }
94                 synchronized (peer) {
95                     while (peer.block_disconnect_socket) {
96                         try {
97                             peer.wait();
98                         } catch (InterruptedException ignored) { }
99                     }
100                 }
101             }
102             @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == our_id; }
103             @Override public long hash() { return our_id; }
104         });
105         peer.descriptor = descriptor;
106         return peer;
107     }
108
109     PeerManager peer_manager;
110     Thread io_thread;
111     final Selector selector;
112     long socket_id;
113     volatile boolean shutdown = false;
114
115     /**
116      * Constructs a new peer handler, spawning a thread to monitor for socket events.
117      * The background thread will call the PeerManager's timer_tick_occured() function for you on an appropriate schedule.
118      *
119      * @param manager The LDK PeerManager which connection data will be provided to.
120      * @throws IOException If an internal java.nio error occurs.
121      */
122     public NioPeerHandler(PeerManager manager) throws IOException {
123         this.peer_manager = manager;
124         this.selector = Selector.open();
125         io_thread = new Thread(() -> {
126             ByteBuffer buf = ByteBuffer.allocate(8192);
127             long lastTimerTick = System.currentTimeMillis();
128             while (true) {
129                 try {
130                     if (IS_ANDROID) {
131                         while (true) {
132                             synchronized (this.selector) {
133                                 if (!wakeup_selector) {
134                                     this.selector.select(1000);
135                                     break;
136                                 }
137                             }
138                         }
139                     } else {
140                         this.selector.select(1000);
141                     }
142                 } catch (IOException ignored) {
143                     System.err.println("java.nio threw an unexpected IOException. Stopping PeerHandler thread!");
144                     return;
145                 }
146                 if (shutdown) return;
147                 if (Thread.interrupted()) return;
148                 for (SelectionKey key : this.selector.selectedKeys()) {
149                     try {
150                         if ((key.interestOps() & SelectionKey.OP_ACCEPT) != 0) {
151                             if (key.isAcceptable()) {
152                                 SocketChannel chan;
153                                 try {
154                                     chan = ((ServerSocketChannel) key.channel()).accept();
155                                 } catch (IOException ignored) {
156                                     key.cancel();
157                                     continue;
158                                 }
159                                 if (chan == null) continue;
160                                 try {
161                                     Peer peer = setup_socket(chan);
162                                     Result_NonePeerHandleErrorZ res = this.peer_manager.new_inbound_connection(peer.descriptor);
163                                     if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_OK) {
164                                         peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer);
165                                     }
166                                 } catch (IOException ignored) { }
167                             }
168                             continue; // There is no attachment so the rest of the loop is useless
169                         }
170                         Peer peer = (Peer) key.attachment();
171                         synchronized (peer) {
172                             if (peer.disconnect_requested)
173                                 continue;
174                             peer.block_disconnect_socket = true;
175                         }
176                         try {
177                             if (key.isValid() && (key.interestOps() & SelectionKey.OP_WRITE) != 0 && key.isWritable()) {
178                                 Result_NonePeerHandleErrorZ res = this.peer_manager.write_buffer_space_avail(peer.descriptor);
179                                 if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_Err) {
180                                     key.channel().close();
181                                     key.cancel();
182                                 }
183                             }
184                             if (key.isValid() && (key.interestOps() & SelectionKey.OP_READ) != 0 && key.isReadable()) {
185                                 buf.clear();
186                                 int read = ((SocketChannel) key.channel()).read(buf);
187                                 if (read == -1) {
188                                     this.peer_manager.socket_disconnected(peer.descriptor);
189                                     key.cancel();
190                                 } else if (read > 0) {
191                                     buf.flip();
192                                     byte[] read_bytes = new byte[read];
193                                     buf.get(read_bytes, 0, read);
194                                     Result_boolPeerHandleErrorZ res = this.peer_manager.read_event(peer.descriptor, read_bytes);
195                                     if (res instanceof Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK) {
196                                         if (((Result_boolPeerHandleErrorZ.Result_boolPeerHandleErrorZ_OK) res).res) {
197                                             key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
198                                         }
199                                     } else {
200                                         key.channel().close();
201                                         key.cancel();
202                                     }
203                                 }
204                             }
205                         } catch (IOException ignored) {
206                             try { key.channel().close(); } catch (IOException ignored2) { }
207                             key.cancel();
208                             peer_manager.socket_disconnected(peer.descriptor);
209                         }
210                         synchronized (peer) {
211                             peer.block_disconnect_socket = false;
212                             peer.notifyAll();
213                         }
214                     } catch (CancelledKeyException e) {
215                         try { key.channel().close(); } catch (IOException ignored) { }
216                         // The key is only cancelled when we have notified the PeerManager that the socket is closed, so
217                         // no need to do anything here with the PeerManager.
218                     }
219                 }
220                 if (lastTimerTick < System.currentTimeMillis() - 30 * 1000) {
221                     peer_manager.timer_tick_occurred();
222                     lastTimerTick = System.currentTimeMillis();
223                 }
224                 peer_manager.process_events();
225             }
226         }, "NioPeerHandler NIO Thread");
227         io_thread.start();
228     }
229
230     /**
231      * Connect to a peer given their node id and socket address. Blocks until a connection is established (or returns
232      * IOException) and then the connection handling runs in the background.
233      *
234      * @param their_node_id A valid 33-byte public key representing the peer's Lightning Node ID. If this is invalid,
235      *                      undefined behavior (read: Segfault, etc) may occur.
236      * @param remote The socket address to connect to.
237      * @param timeout_ms The amount of time, in milliseconds, up to which we will wait for connection to complete.
238      * @throws IOException If connecting to the remote endpoint fails or internal java.nio errors occur.
239      */
240     public void connect(byte[] their_node_id, SocketAddress remote, int timeout_ms) throws IOException {
241         SocketChannel chan = SocketChannel.open();
242         chan.configureBlocking(false);
243         Selector open_selector = Selector.open();
244         chan.register(open_selector, SelectionKey.OP_CONNECT);
245         if (!chan.connect(remote)) {
246             open_selector.select(timeout_ms);
247         }
248         if (!chan.finishConnect()) { // Note that this may throw its own IOException if we failed for another reason
249             throw new IOException("Timed out");
250         }
251         Peer peer = setup_socket(chan);
252         Result_CVec_u8ZPeerHandleErrorZ res = this.peer_manager.new_outbound_connection(their_node_id, peer.descriptor);
253         if (res instanceof  Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) {
254             byte[] initial_bytes = ((Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) res).res;
255             if (chan.write(ByteBuffer.wrap(initial_bytes)) != initial_bytes.length) {
256                 throw new IOException("We assume TCP socket buffer is at least a single packet in length");
257             }
258             do_selector_action(() -> peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer));
259         } else {
260             throw new IOException("LDK rejected outbound connection. This likely shouldn't ever happen.");
261         }
262     }
263
264     /**
265      * Binds a listening socket to the given address, accepting incoming connections and handling them on the background
266      * thread.
267      *
268      * @param socket_address The address to bind the listening socket to.
269      * @throws IOException if binding the listening socket fail.
270      */
271     public void bind_listener(SocketAddress socket_address) throws IOException {
272         ServerSocketChannel listen_channel = ServerSocketChannel.open();
273         listen_channel.bind(socket_address);
274         listen_channel.configureBlocking(false);
275         do_selector_action(() -> listen_channel.register(this.selector, SelectionKey.OP_ACCEPT));
276     }
277
278     /**
279      * Interrupt the background thread, stopping all peer handling. Disconnection events to the PeerHandler are not made,
280      * potentially leaving the PeerHandler in an inconsistent state.
281      */
282     public void interrupt() {
283         shutdown = true;
284         selector.wakeup();
285         try {
286             io_thread.join();
287         } catch (InterruptedException ignored) { }
288     }
289
290     /**
291      * Calls process_events on the PeerManager immediately. Normally process_events is polled regularly to check for new
292      * messages which need to be sent, but you can interrupt the poll and check immediately by calling this function.
293      */
294     public void check_events() {
295         selector.wakeup();
296     }
297 }