Skip to content

Commit a1098b2

Browse files
committed
fix(archivist): improve shutdown with nil checks and store cleanup
1 parent 531f55c commit a1098b2

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

archivist/archivist.nim

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ type
5050
repoStore: RepoStore
5151
maintenance: BlockMaintainer
5252
taskpool: Taskpool
53+
started: bool # Track whether the node was started
54+
discoveryStore: Datastore # Store reference to close explicitly
5355

5456
NodePrivateKey* = libp2p.PrivateKey # alias
5557

@@ -118,24 +120,50 @@ proc start*(s: NodeServer) {.async.} =
118120
await s.connectMarketplace()
119121
await s.archivistNode.start()
120122
s.restServer.start()
123+
s.started = true
121124

122125
proc stop*(s: NodeServer) {.async.} =
123126
notice "Stopping node"
124127

125-
let res = await noCancel allFinishedFailed[void](
126-
@[
127-
s.restServer.stop(),
128-
s.archivistNode.switch.stop(),
129-
s.archivistNode.stop(),
130-
s.repoStore.stop(),
131-
s.maintenance.stop(),
132-
]
133-
)
134-
135-
if res.failure.len > 0:
136-
error "Failed to stop node", failures = res.failure.len
137-
raiseAssert "Failed to stop node"
138-
128+
if not s.started:
129+
# Close the discovery store to release the LevelDB lock
130+
if not s.discoveryStore.isNil:
131+
try:
132+
discard await s.discoveryStore.close()
133+
except Exception as e:
134+
error "Failed to close discovery store", error = e.msg
135+
if not s.taskpool.isNil:
136+
s.taskpool.shutdown()
137+
return
138+
139+
var futures: seq[Future[void]] = @[]
140+
141+
if not s.restServer.isNil:
142+
futures.add(s.restServer.stop())
143+
144+
if not s.archivistNode.isNil:
145+
futures.add(s.archivistNode.switch.stop())
146+
futures.add(s.archivistNode.stop())
147+
148+
if not s.repoStore.isNil:
149+
futures.add(s.repoStore.stop())
150+
151+
if not s.maintenance.isNil:
152+
futures.add(s.maintenance.stop())
153+
154+
if futures.len > 0:
155+
let res = await noCancel allFinishedFailed[void](futures)
156+
157+
if res.failure.len > 0:
158+
error "Failed to stop node", failures = res.failure.len
159+
raiseAssert "Failed to stop node"
160+
161+
# Close the discovery store to release the LevelDB lock
162+
if not s.discoveryStore.isNil:
163+
try:
164+
discard await s.discoveryStore.close()
165+
except Exception as e:
166+
error "Failed to close discovery store", error = e.msg
139167
if not s.taskpool.isNil:
140168
s.taskpool.shutdown()
141169

@@ -168,24 +196,27 @@ proc new*(
168196
except CatchableError as exc:
169197
raiseAssert("Failure in tp initialization:" & exc.msg)
170198

171-
info "Threadpool started", numThreads = tp.numThreads
172-
173199
let discoveryDir = config.dataDir / ArchivistDhtNamespace
174200

175201
if io2.createPath(discoveryDir).isErr:
176-
trace "Unable to create discovery directory for block store",
177-
discoveryDir = discoveryDir
178202
raise (ref Defect)(
179203
msg: "Unable to create discovery directory for block store: " & discoveryDir
180204
)
181205

206+
let discoveryProvidersDir = config.dataDir / ArchivistDhtProvidersNamespace
207+
if io2.createPath(discoveryProvidersDir).isErr:
208+
raise (ref Defect)(
209+
msg: "Unable to create discovery providers directory: " & discoveryProvidersDir
210+
)
211+
182212
let
183213
discoveryStore = Datastore(
184-
LevelDbDatastore.new(config.dataDir / ArchivistDhtProvidersNamespace).expect(
214+
LevelDbDatastore.new(discoveryProvidersDir).expect(
185215
"Should create discovery datastore!"
186216
)
187217
)
188218

219+
let
189220
discovery = Discovery.new(
190221
switch.peerInfo.privateKey,
191222
announceAddrs = config.listenAddrs,
@@ -276,4 +307,6 @@ proc new*(
276307
repoStore: repoStore,
277308
maintenance: maintenance,
278309
taskpool: tp,
310+
discoveryStore: discoveryStore,
311+
started: false,
279312
)

0 commit comments

Comments
 (0)