public void Shutdown() { if (nodeState.GetState() != NodeStateEnum.Shutdown) { logger.Debug("Shutdown"); //Exit any non-shutdown state immediately nodeState.SetState(NodeStateEnum.Shutdown); //Stop and wait for concurrent operations cts.Cancel(); try { nodeTask?.Wait(); } catch (AggregateException e) when(e.InnerException is OperationCanceledException) { } catch (AggregateException e) { logger.Error(e, "Application termination "); } finally { cts.Dispose(); } //transport and store should only be closed once all concurrent operations //are finished otherwise they will panic trying to use close objects Trans.Close(); Controller.Hg.Store.Close(); } }