Skip to main content

nodedb_lite/nodedb/core/
shutdown.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! `NodeDbLite::shutdown` — stop every background task this database started.
4
5use crate::storage::engine::StorageEngine;
6
7use super::types::NodeDbLite;
8
9impl<S: StorageEngine> NodeDbLite<S> {
10    /// Stop every background task this database started, and wait for them.
11    ///
12    /// Call this before dropping the async runtime the database was opened on.
13    /// Auto-flush, auto-compact and the sync loop each outlive the call that
14    /// started them; a runtime torn down while one is still polling takes the
15    /// task down mid-poll, which on a native runtime has crashed the process.
16    ///
17    /// Call it also before reopening the same store in-process. A background
18    /// flush in flight holds the database, and so the store, open past the
19    /// drop of the last handle, and the reopen fails with `AlreadyOpen`.
20    ///
21    /// Each task is signalled first and leaves its loop at a point it chose.
22    /// One that ignores the signal past
23    /// [`TASK_STOP_TIMEOUT`](crate::tasks::TASK_STOP_TIMEOUT) is aborted.
24    /// Admitted search DDL completes before this returns.
25    ///
26    /// Idempotent, and safe on a database that started no tasks. It does not
27    /// flush: what is in memory at shutdown is still in memory, so call
28    /// [`flush`](Self::flush) first when the data must be durable.
29    pub async fn shutdown(&self) {
30        self.tasks.shutdown().await;
31        let _permit = self.fts_state.admit_exclusive().await;
32    }
33}