diff --git a/src/app_network.cpp b/src/app_network.cpp index f65f094..216178b 100644 --- a/src/app_network.cpp +++ b/src/app_network.cpp @@ -3561,11 +3561,14 @@ void App::beginAdoptSeedWallet() bool swapDone = false; try { std::error_code ec; - // 1. Stop the main daemon and wait for it to fully exit. - stopEmbeddedDaemon(); - for (int i = 0; i < 60 && isEmbeddedDaemonRunning(); ++i) - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - if (isEmbeddedDaemonRunning()) { + // 1. Stop the main daemon and wait for it to FULLY release wallet.dat + the RPC port before we + // swap the file. Use the wallet-switch stop path: for an ADOPTED (external) daemon it sends a + // graceful RPC "stop" and gates on the port actually freeing — isEmbeddedDaemonRunning() is + // process-handle-only and would falsely read "stopped" for an adopted daemon, letting the swap + // run against a live daemon still holding wallet.dat. (For an owned daemon stopEmbeddedDaemon() + // already blocks for full process exit, so the file is closed before the swap.) + const bool port_free = stopDaemonForWalletSwitch(); + if (!port_free) { err = "The daemon did not stop in time; your wallet was not changed."; } else { // 2. Swap wallet.dat. Move the legacy one aside to a timestamped backup (NEVER @@ -3607,6 +3610,11 @@ void App::beginAdoptSeedWallet() // unless we're quitting, in which case don't resurrect it. if (swapDone && daemon_controller_) daemon_controller_->setRescanOnNextStart(true); if (!shutting_down_) { + // We stopped the daemon ourselves (port_free) — clear the adopted-external latch so the + // relaunched process is treated as owned (stop/isRunning/exit behave normally afterward). + // Skip when the stop failed: the old adopted daemon is still up, and we must not mark a + // process we can't control as owned. + if (port_free && daemon_controller_) daemon_controller_->clearExternalDaemonDetected(); if (!startEmbeddedDaemon() && swapDone) warn = "Your wallet was swapped, but the daemon did not restart — start it from Settings."; }