| 309 | } |
| 310 | |
| 311 | void Shutdown(NodeContext& node) |
| 312 | { |
| 313 | static Mutex g_shutdown_mutex; |
| 314 | TRY_LOCK(g_shutdown_mutex, lock_shutdown); |
| 315 | if (!lock_shutdown) return; |
| 316 | LogInfo("Shutdown in progress..."); |
| 317 | Assert(node.args); |
| 318 | |
| 319 | /// Note: Shutdown() must be able to handle cases in which initialization failed part of the way, |
| 320 | /// for example if the data directory was found to be locked. |
| 321 | /// Be sure that anything that writes files or flushes caches only does this if the respective |
| 322 | /// module was initialized. |
| 323 | util::ThreadRename("shutoff"); |
| 324 | if (node.mempool) node.mempool->AddTransactionsUpdated(1); |
| 325 | |
| 326 | StopHTTPRPC(); |
| 327 | StopREST(); |
| 328 | StopRPC(); |
| 329 | StopHTTPServer(); |
| 330 | for (auto& client : node.chain_clients) { |
| 331 | try { |
| 332 | client->stop(); |
| 333 | } catch (const ipc::Exception& e) { |
| 334 | LogDebug(BCLog::IPC, "Chain client did not disconnect cleanly: %s", e.what()); |
| 335 | client.reset(); |
| 336 | } |
| 337 | } |
| 338 | StopMapPort(); |
| 339 | |
| 340 | // Because these depend on each-other, we make sure that neither can be |
| 341 | // using the other before destroying them. |
| 342 | if (node.peerman && node.validation_signals) node.validation_signals->UnregisterValidationInterface(node.peerman.get()); |
| 343 | if (node.connman) node.connman->Stop(); |
| 344 | |
| 345 | if (node.tor_controller) { |
| 346 | node.tor_controller->Join(); |
| 347 | node.tor_controller.reset(); |
| 348 | } |
| 349 | |
| 350 | if (node.background_init_thread.joinable()) node.background_init_thread.join(); |
| 351 | // After everything has been shut down, but before things get flushed, stop the |
| 352 | // the scheduler. After this point, SyncWithValidationInterfaceQueue() should not be called anymore |
| 353 | // as this would prevent the shutdown from completing. |
| 354 | if (node.scheduler) node.scheduler->stop(); |
| 355 | |
| 356 | // After the threads that potentially access these pointers have been stopped, |
| 357 | // destruct and reset all to nullptr. |
| 358 | node.peerman.reset(); |
| 359 | node.connman.reset(); |
| 360 | node.banman.reset(); |
| 361 | node.addrman.reset(); |
| 362 | node.netgroupman.reset(); |
| 363 | |
| 364 | if (node.mempool && node.mempool->GetLoadTried() && ShouldPersistMempool(*node.args)) { |
| 365 | DumpMempool(*node.mempool, MempoolPath(*node.args)); |
| 366 | } |
| 367 | |
| 368 | // Drop transactions we were still watching, record fee estimations and unregister |