Skip to content

Commit a997a7b

Browse files
committed
gui: don't rely on cookie to check successful start
1 parent 5e5c333 commit a997a7b

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

gui/src/bitcoind.rs

+29-33
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,10 @@ impl InternalBitcoindConfig {
357357
#[derive(PartialEq, Eq, Debug, Clone)]
358358
pub enum StartInternalBitcoindError {
359359
CommandError(String),
360-
CouldNotCanonicalizeExePath(String),
361360
CouldNotCanonicalizeDataDir(String),
362-
CouldNotCanonicalizeCookiePath(String),
363-
CookieFileNotFound(String),
364361
BitcoinDError(String),
365362
ExecutableNotFound,
363+
ProcessExited(std::process::ExitStatus),
366364
}
367365

368366
impl std::fmt::Display for StartInternalBitcoindError {
@@ -371,24 +369,14 @@ impl std::fmt::Display for StartInternalBitcoindError {
371369
Self::CommandError(e) => {
372370
write!(f, "Command to start bitcoind returned an error: {}", e)
373371
}
374-
Self::CouldNotCanonicalizeExePath(e) => {
375-
write!(f, "Failed to canonicalize executable path: {}", e)
376-
}
377372
Self::CouldNotCanonicalizeDataDir(e) => {
378373
write!(f, "Failed to canonicalize datadir: {}", e)
379374
}
380-
Self::CouldNotCanonicalizeCookiePath(e) => {
381-
write!(f, "Failed to canonicalize cookie path: {}", e)
382-
}
383-
Self::CookieFileNotFound(path) => {
384-
write!(
385-
f,
386-
"Cookie file was not found at the expected path: {}",
387-
path
388-
)
389-
}
390375
Self::BitcoinDError(e) => write!(f, "bitcoind connection check failed: {}", e),
391376
Self::ExecutableNotFound => write!(f, "bitcoind executable not found."),
377+
Self::ProcessExited(status) => {
378+
write!(f, "bitcoind process exited with status '{}'.", status)
379+
}
392380
}
393381
}
394382
}
@@ -455,35 +443,43 @@ impl Bitcoind {
455443
.map_err(|e| StartInternalBitcoindError::CommandError(e.to_string()))?;
456444

457445
// We've started bitcoind in the background, however it may fail to start for whatever
458-
// reason. And we need its JSONRPC interface to be available to continue. Thus wait for it
459-
// to have created the cookie file, regularly checking it did not fail to start.
460-
let cookie_path = internal_bitcoind_cookie_path(&bitcoind_datadir, network);
446+
// reason. And we need its JSONRPC interface to be available to continue. Thus wait for
447+
// the interface to be created successfully, regularly checking it did not fail to start.
461448
loop {
462449
match process.try_wait() {
463450
Ok(None) => {}
464451
Err(e) => log::error!("Error while trying to wait for bitcoind: {}", e),
465452
Ok(Some(status)) => {
466453
log::error!("Bitcoind exited with status '{}'", status);
467-
return Err(StartInternalBitcoindError::CookieFileNotFound(
468-
cookie_path.to_string_lossy().into_owned(),
469-
));
454+
return Err(StartInternalBitcoindError::ProcessExited(status));
470455
}
471456
}
472-
if cookie_path.exists() {
473-
log::info!("Bitcoind seems to have successfully started.");
474-
break;
457+
match liana::BitcoinD::new(&config, "internal_bitcoind_start".to_string()) {
458+
Ok(_) => {
459+
log::info!("Bitcoind seems to have successfully started.");
460+
return Ok(Self {
461+
config,
462+
_process: Arc::new(process),
463+
});
464+
}
465+
Err(liana::BitcoindError::CookieFile(_)) => {
466+
// This is only raised if we're using cookie authentication.
467+
// Assume cookie file has not been created yet and try again.
468+
}
469+
Err(e) => {
470+
if !e.is_transient() {
471+
// Non-transient error could happen, e.g., if RPC auth credentials are wrong.
472+
// Kill process now in case it's not possible to do via RPC command later.
473+
if let Err(e) = process.kill() {
474+
log::error!("Error trying to kill bitcoind process: '{}'", e);
475+
}
476+
return Err(StartInternalBitcoindError::BitcoinDError(e.to_string()));
477+
}
478+
}
475479
}
476480
log::info!("Waiting for bitcoind to start.");
477481
thread::sleep(time::Duration::from_millis(500));
478482
}
479-
480-
liana::BitcoinD::new(&config, "internal_bitcoind_start".to_string())
481-
.map_err(|e| StartInternalBitcoindError::BitcoinDError(e.to_string()))?;
482-
483-
Ok(Self {
484-
config,
485-
_process: Arc::new(process),
486-
})
487483
}
488484

489485
/// Stop (internal) bitcoind.

0 commit comments

Comments
 (0)