Aristo+Kvt: Let destructor crash when `nil` argument is given (#2080)

why:
  Ignoring `nil` objects was handy for a while but eventually led to
  lazy programming which in turn led to double destructor calls for
  the rocks-db.
This commit is contained in:
Jordan Hrycaj 2024-03-15 14:20:00 +00:00 committed by GitHub
parent 90622c0915
commit 5379302ce9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 26 deletions

View File

@ -98,10 +98,9 @@ proc finish*(db: AristoDbRef; flush = false) =
##
## This distructor may be used on already *destructed* descriptors.
##
if not db.isNil:
if not db.backend.isNil:
db.backend.closeFn flush
discard db.getCentre.forgetOthers()
if not db.backend.isNil:
db.backend.closeFn flush
discard db.getCentre.forgetOthers()
# ------------------------------------------------------------------------------
# End

View File

@ -73,10 +73,9 @@ proc finish*(db: KvtDbRef; flush = false) =
##
## This distructor may be used on already *destructed* descriptors.
##
if not db.isNil:
if not db.backend.isNil:
db.backend.closeFn flush
discard db.getCentre.forgetOthers()
if not db.backend.isNil:
db.backend.closeFn flush
discard db.getCentre.forgetOthers()
# ------------------------------------------------------------------------------
# End

View File

@ -183,8 +183,8 @@ proc storagesRunner(
proc aristoMain*(noisy = defined(debug)) =
noisy.miscRunner()
noisy.accountsRunner(persistent=false)
noisy.storagesRunner(persistent=false)
noisy.accountsRunner()
noisy.storagesRunner()
when isMainModule:
const

View File

@ -202,8 +202,10 @@ proc dbTriplet(w: LeafQuartet; rdbPath: string): Result[DbTriplet,AristoError] =
# ----------------------
proc cleanUp(dx: DbTriplet) =
dx[0].finish(flush=true)
proc cleanUp(dx: var DbTriplet) =
if not dx[0].isNil:
dx[0].finish(flush=true)
dx.reset
proc isDbEq(a, b: FilterRef; db: AristoDbRef; noisy = true): bool =
## Verify that argument filter `a` has the same effect on the
@ -559,7 +561,7 @@ proc testDistributedAccess*(
block:
# Clause (8) from `aristo/README.md` example
let
var
dx = block:
let rc = dbTriplet(w, rdbPath)
xCheckRc rc.error == 0
@ -612,7 +614,7 @@ proc testDistributedAccess*(
# Work through clauses (12)..(15) from `aristo/README.md` example
block:
let
var
dy = block:
let rc = dbTriplet(w, rdbPath)
xCheckRc rc.error == 0

View File

@ -108,13 +108,15 @@ proc randomisedLeafs(
lvp2[n].swap lvp2[r]
ok lvp2
proc innerCleanUp(db: AristoDbRef): bool {.discardable.} =
proc innerCleanUp(db: var AristoDbRef): bool {.discardable.} =
## Defer action
let rx = db.txTop()
if rx.isOk:
let rc = rx.value.collapse(commit=false)
xCheckRc rc.error == 0
db.finish(flush=true)
if not db.isNil:
let rx = db.txTop()
if rx.isOk:
let rc = rx.value.collapse(commit=false)
xCheckRc rc.error == 0
db.finish(flush=true)
db = AristoDbRef(nil)
proc schedStow(
db: AristoDbRef; # Database
@ -334,10 +336,11 @@ proc testTxMergeAndDeleteOneByOne*(
): bool =
var
prng = PrngDesc.init 42
db = AristoDbRef()
db = AristoDbRef(nil)
fwdRevVfyToggle = true
defer:
db.finish(flush=true)
if not db.isNil:
db.finish(flush=true)
for n,w in list:
# Start with brand new persistent database.
@ -439,10 +442,10 @@ proc testTxMergeAndDeleteSubTree*(
): bool =
var
prng = PrngDesc.init 42
db = AristoDbRef()
db = AristoDbRef(nil)
defer:
db.finish(flush=true)
if not db.isNil:
db.finish(flush=true)
for n,w in list:
# Start with brand new persistent database.
@ -524,7 +527,7 @@ proc testTxMergeProofAndKvpList*(
let
oopsTab = oops.toTable
var
db = AristoDbRef()
db = AristoDbRef(nil)
tx = AristoTxRef(nil)
rootKey: Hash256
count = 0