init: do not call the notarizations DB corrupt on one post-failure probe

OpenNotarizationDB() decided between "LOCK held" and "corrupt" with a
lock probe taken after DB::Open had already failed. If the holder (an
older dragonxd finishing its exit, the very upgrade-restart case the
lock-aware fallback exists for) released notarizations/LOCK between the
failed open and the probe, the probe found the lock free and a healthy
DB was moved aside and replaced by an empty one. CDBWrapper throws a
generic "Database I/O error", so the error text cannot tell the two
cases apart either.

Only treat the DB as unreadable when an open fails although the probe
just before that open found the lock free: on a free probe, open once
more, and move aside only if that attempt fails too. A held probe still
gets the single bounded wait and then the in-memory fallback.

The probe now returns the leveldb::Status of its LockFile call, and the
log reports it ("Resource temporarily unavailable" versus, say,
"Permission denied") instead of the generic open error alone, worded as
"could not be taken" since a non-ok probe is not always another holder.
Any non-ok probe still counts as held, so it never moves or wipes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 12:10:21 -05:00
co-authored by Claude Opus 5.5
parent 4d7de7a0eb
commit 8f70f494b3
+51 -36
View File
@@ -131,67 +131,82 @@ static const size_t NOTARIZATION_DB_CACHE_BYTES = 100 * 1024 * 1024; // 100 MiB
static const int NOTARIZATION_LOCK_WAIT_SECONDS = 30;
/**
* Is <dbdir>/LOCK held, by another process or by another open DB in this one? Probed with LevelDB's
* own Env::LockFile, i.e. the same lock DB::Open takes. A missing LOCK file cannot be held. Any other
* failure to take the lock counts as held: the caller then never moves or wipes the directory.
* Try to take <dbdir>/LOCK with LevelDB's own Env::LockFile, i.e. the same lock DB::Open takes, and
* release it at once. ok() means the lock is free (a missing LOCK file cannot be held). Anything else
* means it could not be taken: usually another process holds it ("Resource temporarily unavailable"),
* but it may also be e.g. a permission error. The caller treats every non-ok status as held, and so
* never moves or wipes the directory on its strength.
*/
static bool LevelDBLockHeld(const boost::filesystem::path& dbdir)
static leveldb::Status ProbeLevelDBLock(const boost::filesystem::path& dbdir)
{
const boost::filesystem::path lockpath = dbdir / "LOCK";
boost::system::error_code ec;
if (!boost::filesystem::exists(lockpath, ec))
return false;
return leveldb::Status::OK();
leveldb::Env* env = leveldb::Env::Default();
leveldb::FileLock* lock = NULL;
if (!env->LockFile(lockpath.string(), &lock).ok())
return true;
env->UnlockFile(lock);
return false;
leveldb::Status status = env->LockFile(lockpath.string(), &lock);
if (status.ok())
env->UnlockFile(lock);
return status;
}
/**
* Open the notarizations (dPoW) DB. It is non-essential and node-regenerable, so nothing here may fail
* startup (previously any open failure surfaced as a spurious "Error opening block database"):
* - LOCK held (typically a previous dragonxd that is still exiting): wait up to
* - LOCK cannot be taken (typically a previous dragonxd that is still exiting): wait up to
* NOTARIZATION_LOCK_WAIT_SECONDS for it and retry; if it is still held, run this session on an
* in-memory DB and leave the on-disk one alone.
* - otherwise the DB is unreadable (seen: a torn snapshot, 0-byte log -> leveldb "Database I/O error"):
* move it aside under a timestamped notarizations.corrupt-* name, never deleting an earlier copy, and
* create a fresh one. If even that fails, fall back to an in-memory DB.
* The lock is probed only after an open fails, so a holder that exits between the failed open and the
* probe makes the probe report "free" for a DB that is fine. The DB is therefore treated as corrupt only
* when an open fails although the probe just before that open found the lock free.
*/
static NotarizationDB* OpenNotarizationDB(bool fWipe)
{
const boost::filesystem::path ndir = GetDataDir() / "notarizations";
std::string strError;
try {
return new NotarizationDB(NOTARIZATION_DB_CACHE_BYTES, false, fWipe);
} catch (const std::exception& e) {
strError = e.what();
}
if (LevelDBLockHeld(ndir)) {
LogPrintf("%s: notarizations DB failed to open (%s): notarizations/LOCK is held by another process; waiting for a previous dragonxd to release notarizations/LOCK (up to %d s)\n",
__func__, strError, NOTARIZATION_LOCK_WAIT_SECONDS);
int nWaited = 0;
while (nWaited < NOTARIZATION_LOCK_WAIT_SECONDS && !ShutdownRequested() && LevelDBLockHeld(ndir)) {
MilliSleep(1000);
nWaited++;
}
if (!LevelDBLockHeld(ndir)) {
try {
NotarizationDB* pdb = new NotarizationDB(NOTARIZATION_DB_CACHE_BYTES, false, fWipe);
bool fWaited = false; // the bounded wait for the lock has been used up
bool fProbedFree = false; // the probe right before the last open attempt found the lock free
int nWaited = 0;
while (true) {
try {
NotarizationDB* pdb = new NotarizationDB(NOTARIZATION_DB_CACHE_BYTES, false, fWipe);
if (fWaited)
LogPrintf("%s: notarizations/LOCK released after %d s; notarizations DB opened\n", __func__, nWaited);
return pdb;
} catch (const std::exception& e) {
strError = e.what();
else if (fProbedFree)
LogPrintf("%s: notarizations DB opened on retry (notarizations/LOCK was released after the first attempt)\n", __func__);
return pdb;
} catch (const std::exception& e) {
strError = e.what();
}
if (fProbedFree)
break; // failed with the lock free beforehand: genuinely unreadable
leveldb::Status lockStatus = ProbeLevelDBLock(ndir);
if (!lockStatus.ok()) {
if (!fWaited) {
LogPrintf("%s: notarizations DB failed to open (%s): notarizations/LOCK could not be taken (%s); waiting for a previous dragonxd to release notarizations/LOCK (up to %d s)\n",
__func__, strError, lockStatus.ToString(), NOTARIZATION_LOCK_WAIT_SECONDS);
fWaited = true;
while (nWaited < NOTARIZATION_LOCK_WAIT_SECONDS && !ShutdownRequested()) {
MilliSleep(1000);
nWaited++;
lockStatus = ProbeLevelDBLock(ndir);
if (lockStatus.ok())
break;
}
}
if (!lockStatus.ok()) {
LogPrintf("%s: notarizations/LOCK still could not be taken after %d s (%s); using an in-memory notarizations DB for this run (on-disk notarizations DB left untouched)\n",
__func__, nWaited, lockStatus.ToString());
return new NotarizationDB(NOTARIZATION_DB_CACHE_BYTES, true, false);
}
}
if (LevelDBLockHeld(ndir)) {
LogPrintf("%s: notarizations/LOCK still held after %d s; using an in-memory notarizations DB for this run (on-disk notarizations DB left untouched)\n",
__func__, nWaited);
return new NotarizationDB(NOTARIZATION_DB_CACHE_BYTES, true, false);
}
// The lock is free but the DB still does not open: treat it as corrupt.
// The lock is free now: open once more before calling the DB corrupt.
fProbedFree = true;
}
LogPrintf("%s: notarizations DB failed to open (%s); moving aside and regenerating (non-fatal)\n", __func__, strError);