IGNITE-15398 Fix bugs in nodes start flow and free some netty resources properly. Fixes #314

Signed-off-by: Slava Koptilin <slava.koptilin@gmail.com>
diff --git a/modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientHandlerModule.java b/modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientHandlerModule.java
index 06ac43e..8e254c1 100644
--- a/modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientHandlerModule.java
+++ b/modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientHandlerModule.java
@@ -51,7 +51,7 @@
     private final IgniteTables igniteTables;
 
     /** Netty channel. */
-    private Channel channel;
+    private volatile Channel channel;
 
     /**
      * Constructor.
diff --git a/modules/rest/src/main/java/org/apache/ignite/rest/RestModule.java b/modules/rest/src/main/java/org/apache/ignite/rest/RestModule.java
index 59d18d1..c8338ae 100644
--- a/modules/rest/src/main/java/org/apache/ignite/rest/RestModule.java
+++ b/modules/rest/src/main/java/org/apache/ignite/rest/RestModule.java
@@ -79,6 +79,9 @@
     /** Presentation of cluster configuration. */
     private final ConfigurationPresentation<String> clusterCfgPresentation;
 
+    /** Netty channel. */
+    private volatile Channel channel;
+
     /**
      * Creates a new instance of REST module.
      *
@@ -97,6 +100,9 @@
 
     /** {@inheritDoc} */
     @Override public void start() {
+        if (channel != null)
+            throw new IgniteException("RestModule is already started.");
+
         var router = new Router();
 
         router
@@ -127,7 +133,7 @@
                 (req, resp) -> handleUpdate(req, resp, clusterCfgPresentation)
             );
 
-        startRestEndpoint(router);
+        channel = startRestEndpoint(router).channel();
     }
 
     /**
@@ -206,6 +212,11 @@
 
     /** {@inheritDoc} */
     @Override public void stop() throws Exception {
+        if (channel != null) {
+            channel.close().await();
+
+            channel = null;
+        }
     }
 
     /**
diff --git a/modules/runner/src/main/java/org/apache/ignite/internal/app/IgniteImpl.java b/modules/runner/src/main/java/org/apache/ignite/internal/app/IgniteImpl.java
index 0aa136c..d2ce62c 100644
--- a/modules/runner/src/main/java/org/apache/ignite/internal/app/IgniteImpl.java
+++ b/modules/runner/src/main/java/org/apache/ignite/internal/app/IgniteImpl.java
@@ -323,7 +323,7 @@
 
         if (explicitStop.get()) {
             doStopNode(List.of(vaultMgr, nodeCfgMgr, clusterSvc, raftMgr, metaStorageMgr,
-                clusterCfgMgr, baselineMgr, affinityMgr, schemaMgr, distributedTblMgr, qryEngine, restModule));
+                clusterCfgMgr, baselineMgr, affinityMgr, schemaMgr, distributedTblMgr, qryEngine, restModule, clientHandlerModule));
         }
     }
 
@@ -405,7 +405,7 @@
      */
     private void doStopNode(@NotNull List<IgniteComponent> startedComponents) {
         ListIterator<IgniteComponent> beforeStopIter =
-            startedComponents.listIterator(startedComponents.size() - 1);
+            startedComponents.listIterator(startedComponents.size());
 
         while (beforeStopIter.hasPrevious()) {
             IgniteComponent componentToExecBeforeNodeStop = beforeStopIter.previous();
@@ -420,7 +420,7 @@
         }
 
         ListIterator<IgniteComponent> stopIter =
-            startedComponents.listIterator(startedComponents.size() - 1);
+            startedComponents.listIterator(startedComponents.size());
 
         while (stopIter.hasPrevious()) {
             IgniteComponent componentToStop = stopIter.previous();