Merge pull request #82 from TheBlueMatt/main
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Wed, 2 Feb 2022 20:32:01 +0000 (20:32 +0000)
committerGitHub <noreply@github.com>
Wed, 2 Feb 2022 20:32:01 +0000 (20:32 +0000)
TypeScript Package cleanups and Java socket closure

genbindings.sh
src/main/java/org/ldk/batteries/ChannelManagerConstructor.java
src/main/java/org/ldk/batteries/NioPeerHandler.java
ts/.gitignore
ts/README.md [deleted symlink]
ts/package.json
ts/test/browser.mjs

index 7fc10bc9924ecfb0cc68d47208d0c4f3c2fa2929..0c2a8982a1fe43a5205388d1f23e6b41fea79bb5 100755 (executable)
@@ -222,6 +222,7 @@ else
                rm imports.mts.part
                tsc --types node --typeRoots .
                cp ../$WASM_FILE liblightningjs.wasm
+               cp ../README.md README.md
                echo Ready to publish!
                if [ -x "$(which node)" ]; then
                        NODE_V="$(node --version)"
index 7399ccb3fb18ffae950ca9e1ed445212dad88890..d64c0d6e60d063c53b8dbad808a479e81a981f7d 100644 (file)
@@ -207,7 +207,7 @@ assert this.payer != null;
      * Interrupt the background thread, stopping the background handling of events.
      */
     public void interrupt() {
-        this.background_processor.stop();
         this.nio_peer_handler.interrupt();
+        this.background_processor.stop();
     }
 }
index e0dd83e9edc570b542716ae88caacb4e45b58940..5963506feddf76b518c41b8a18f31a51fe4b8037 100644 (file)
@@ -87,7 +87,7 @@ public class NioPeerHandler {
                                                        (peer.key.interestOps() | SelectionKey.OP_READ) & (~SelectionKey.OP_WRITE)));
                                        }
                     return written;
-                } catch (IOException e) {
+                } catch (IOException|CancelledKeyException ignored) {
                     // Most likely the socket is disconnected, let the background thread handle it.
                     return 0;
                 }
@@ -97,7 +97,7 @@ public class NioPeerHandler {
             public void disconnect_socket() {
                 try {
                     do_selector_action(() -> {
-                        peer.key.cancel();
+                        try { peer.key.cancel(); } catch (CancelledKeyException ignored) {}
                         peer.key.channel().close();
                     });
                 } catch (IOException ignored) { }
@@ -189,8 +189,8 @@ public class NioPeerHandler {
                             if (key.isValid() && (key.interestOps() & SelectionKey.OP_WRITE) != 0 && key.isWritable()) {
                                 Result_NonePeerHandleErrorZ res = this.peer_manager.write_buffer_space_avail(peer.descriptor);
                                 if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_Err) {
-                                    key.channel().close();
                                     key.cancel();
+                                    key.channel().close();
                                 }
                             }
                             if (key.isValid() && (key.interestOps() & SelectionKey.OP_READ) != 0 && key.isReadable()) {
@@ -199,6 +199,7 @@ public class NioPeerHandler {
                                 if (read == -1) {
                                     this.peer_manager.socket_disconnected(peer.descriptor);
                                     key.cancel();
+                                    key.channel().close(); // This may throw, we read -1 so the channel should already be closed, but do this to be safe
                                 } else if (read > 0) {
                                     ((Buffer)buf).flip();
                                     // This code is quite hot during initial network graph sync, so we go a ways out of
@@ -223,15 +224,15 @@ public class NioPeerHandler {
                                             key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
                                         }
                                     } else {
-                                        key.channel().close();
                                         key.cancel();
+                                        key.channel().close();
                                     }
                                     bindings.CResult_boolPeerHandleErrorZ_free(read_result_pointer);
                                 }
                             }
                         } catch (IOException ignored) {
-                            try { key.channel().close(); } catch (IOException ignored2) { }
                             key.cancel();
+                            try { key.channel().close(); } catch (IOException ignored2) { }
                             peer_manager.socket_disconnected(peer.descriptor);
                         }
                     } catch (CancelledKeyException e) {
@@ -265,13 +266,21 @@ public class NioPeerHandler {
      */
     public void connect(byte[] their_node_id, SocketAddress remote, int timeout_ms) throws IOException {
         SocketChannel chan = SocketChannel.open();
-        chan.configureBlocking(false);
-        Selector open_selector = Selector.open();
-        chan.register(open_selector, SelectionKey.OP_CONNECT);
-        if (!chan.connect(remote)) {
-            open_selector.select(timeout_ms);
+        boolean connected;
+        try {
+            chan.configureBlocking(false);
+            Selector open_selector = Selector.open();
+            chan.register(open_selector, SelectionKey.OP_CONNECT);
+            if (!chan.connect(remote)) {
+                open_selector.select(timeout_ms);
+            }
+            connected = chan.finishConnect();
+        } catch (IOException e) {
+            try { chan.close(); } catch (IOException _e) { }
+            throw e;
         }
-        if (!chan.finishConnect()) { // Note that this may throw its own IOException if we failed for another reason
+        if (!connected) {
+            try { chan.close(); } catch (IOException _e) { }
             throw new IOException("Timed out");
         }
         Peer peer = setup_socket(chan);
@@ -323,10 +332,12 @@ public class NioPeerHandler {
     }
 
     /**
-     * Interrupt the background thread, stopping all peer handling. Disconnection events to the PeerHandler are not made,
-     * potentially leaving the PeerHandler in an inconsistent state.
+     * Interrupt the background thread, stopping all peer handling.
+     *
+     * After this method is called, the behavior of future calls to methods on this NioPeerHandler are undefined.
      */
     public void interrupt() {
+        this.peer_manager.disconnect_all_peers();
         shutdown = true;
         selector.wakeup();
         try {
@@ -338,6 +349,7 @@ public class NioPeerHandler {
                 for (ServerSocketChannel chan : listening_sockets) {
                     chan.close();
                 }
+                listening_sockets.clear();
             } catch (IOException ignored) {}
         }
         Reference.reachabilityFence(this.peer_manager); // Almost certainly overkill, but no harm in it
index dbcf79823bfd6d1167a81076cd66723f57fccd50..487260bb03e73ce3bfc67a52f4ef1d5fa04f5c02 100644 (file)
@@ -2,3 +2,4 @@
 *.mjs.map
 *.d.mts
 version.mts
+README.md
diff --git a/ts/README.md b/ts/README.md
deleted file mode 120000 (symlink)
index 32d46ee..0000000
+++ /dev/null
@@ -1 +0,0 @@
-../README.md
\ No newline at end of file
index 1ae0fb50bd3ddfb163fc46235a840245a5de3269..02a503a2a478c044fcc59e1847da4e77d6fe9790 100644 (file)
@@ -3,6 +3,7 @@
   "version": "0.0.104.1alpha4",
   "description": "Lightning Development Kit",
   "main": "index.mjs",
+  "types": "index.d.mts",
   "type": "module",
   "directories": {
     "test": "test"
index 5f65191a3bb7df5d221a9d21bdd1eb5d31281620..e4e6112cbc88c065b162d38245b7508e17493d97 100644 (file)
@@ -10,10 +10,12 @@ for (const browserType of [chromium, firefox]) { // We'd like to test webkit, bu
        const context = await browser.newContext();
        const page = await context.newPage();
        page.on('console', async msg => {
-               const values = [];
-               for (const arg of msg.args())
-                       values.push(await arg.jsonValue());
-               console.log(...values);
+               try {
+                       const values = [];
+                       for (const arg of msg.args())
+                               values.push(await arg.jsonValue());
+                       console.log(...values);
+               } catch (_) { /* sometimes this gets hit if we're logging while the browser shuts down */ }
        });
        await page.goto('http://localhost:8000/test/index.html');
        var ret;