NagyZoltanPeter e8566db8ee
fix(persistency): own Persistency per node instead of a process-global singleton (#4109)
* fix(persistency): own Persistency per node instead of a process-global singleton

The Persistency singleton (gPersistency) was allocated on whichever FFI
thread first ran waku.start; under --mm:refc its memory belonged to that
thread's heap, so a second library context adopting it read a foreign
heap (SIGSEGV in sdsPersistence -> openJob -> tables.rawGet), one
context's stop stole the other's SDS persistence, and a destroyed
context poisoned re-init with a different local-storage-path.

- remove the singleton (instance/reset/gPersistency); Persistency.new is
  the public constructor, no lock needed (instances are thread-confined)
- own the instance as Waku.persistency: created and provided in
  waku.start, cleared and closed in waku.stop on the owning thread
- expose it via a sync GetPersistency RequestBroker scoped to the node's
  BrokerContext; sdsPersistence resolves through it (same-thread ref
  return, no marshalling)
- add InMemoryStoragePath (":memory:") support: private in-memory SQLite
  per job worker, for tests
- rewrite test_singleton as per-instance + broker coverage; rewrite
  test_thread_affinity from a known-failing UB repro into a regression
  guard (two in-memory jobs, worker spinup/teardown, cross-thread broker
  denial) and register it in test_all; migrate remaining tests to
  new/close; the FFI lifecycle test's stop-steals and different-paths
  cases now pass against the real dylib

The FFI destroy-without-stop teardown gap remains tracked in #4108.

Fixes #4103

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(persistency): create the instance only after startup fully succeeds

waku.start has many error return paths; creating Persistency early meant
every one of them left the field set and the GetPersistency provider
installed with no teardown. Persistency.new is inert (no threads or
files until the first openJob) and every consumer runs post-start, so
creating and providing it as the last startup step removes the need for
any error-path cleanup entirely.

Addresses PR #4109 review feedback.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(persistency): create early for startup-stage restores, tear down on failed start

Creating the instance as the last startup step made it impossible for
any stage of start to restore persisted data (e.g. a future store-state
restore). Restore the original ordering -- create and provide the
instance first -- and cover every error return path of waku.start with a
success-flag defer that clears the provider and closes the instance.
Teardown is factored into closePersistency, shared by stop and the
failed-start path.

Addresses PR #4109 review discussion.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(persistency): address remaining Copilot review findings

- document that Persistency instances are thread-confined (not
  thread-safe) on the type itself, pointing at the GetPersistency broker
  as the sanctioned access path
- use tryRemoveFile for the FFI test's log cleanup so an unremovable
  file cannot fail the test for unrelated reasons

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Fix comment

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-06 23:35:10 +02:00

112 lines
2.6 KiB
Nim

{.used.}
import std/[os, strutils, times]
import chronos, results
import testutils/unittests
import brokers/[request_broker, broker_context]
import logos_delivery/waku/persistency/persistency
proc tmpRoot(label: string): string =
let p = getTempDir() / ("persistency_instance_" & label & "_" & $epochTime().int)
removeDir(p)
p
suite "Persistency instances":
test "new(rootDir) builds independent instances":
let rootA = tmpRoot("a")
let rootB = tmpRoot("b")
defer:
removeDir(rootA)
defer:
removeDir(rootB)
let pA = Persistency.new(rootA).get()
let pB = Persistency.new(rootB).get()
defer:
pA.close()
defer:
pB.close()
check pA.rootDir == rootA
check pB.rootDir == rootB
check pA != pB
test "new(rootDir) with the same rootDir yields distinct instances":
let root = tmpRoot("same")
defer:
removeDir(root)
let p1 = Persistency.new(root).get()
let p2 = Persistency.new(root).get()
defer:
p1.close()
defer:
p2.close()
check p1 != p2
test "close() is idempotent":
let root = tmpRoot("close")
defer:
removeDir(root)
let p = Persistency.new(root).get()
discard p.openJob("j").get()
p.close()
p.close()
check not p.hasJob("j")
suite "GetPersistency broker":
test "request fails when no provider is installed":
let ctx = NewBrokerContext()
check GetPersistency.request(ctx).isErr
test "request returns the provided instance, clearProvider removes it":
let root = tmpRoot("broker")
defer:
removeDir(root)
let ctx = NewBrokerContext()
let p = Persistency.new(root).get()
defer:
p.close()
discard GetPersistency.reprovideIt(ctx):
ok(p)
let got = GetPersistency.request(ctx)
check got.isOk
check got.get() == p
GetPersistency.clearProvider(ctx)
check GetPersistency.request(ctx).isErr
test "providers on different contexts resolve different instances":
let rootA = tmpRoot("ctx-a")
let rootB = tmpRoot("ctx-b")
defer:
removeDir(rootA)
defer:
removeDir(rootB)
let ctxA = NewBrokerContext()
let ctxB = NewBrokerContext()
let pA = Persistency.new(rootA).get()
let pB = Persistency.new(rootB).get()
defer:
pA.close()
defer:
pB.close()
discard GetPersistency.reprovideIt(ctxA):
ok(pA)
discard GetPersistency.reprovideIt(ctxB):
ok(pB)
defer:
GetPersistency.clearProvider(ctxA)
defer:
GetPersistency.clearProvider(ctxB)
check GetPersistency.request(ctxA).get() == pA
check GetPersistency.request(ctxB).get() == pB