]> git.bitcoin.ninja Git - ldk-java/commitdiff
[Java] Close all sockets cleanly on `NioPeerHandler::interrupt`
authorMatt Corallo <git@bluematt.me>
Tue, 1 Feb 2022 22:33:02 +0000 (22:33 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 2 Feb 2022 19:30:47 +0000 (19:30 +0000)
src/main/java/org/ldk/batteries/NioPeerHandler.java

index 2ab48d627a67c0405d985cd6fdaf78969b276ff1..5963506feddf76b518c41b8a18f31a51fe4b8037 100644 (file)
@@ -332,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 {
@@ -347,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