fix(backup): open the database before checkpointing on export

Issue:
- Exporting the database and settings fails with an ExecutionException
  caused by "IllegalStateException: database is not initialized".
- The error is shown as a snackbar and no backup is written.

Cause:
- NewPipeDatabase.checkpoint() read the nullable singleton directly and
  threw when it was null, while getInstance() creates it lazily.
- Nothing on the export path creates it, so the export only worked if
  some other component had already called getInstance() in the same
  process.

Fix:
- checkpoint() now takes a Context and gets the database through
  getInstance(), opening it if needed.
- Opening also recovers any WAL left by an earlier process, so the
  following wal_checkpoint(full) folds all committed data into
  newpipe.db, the only database file the export zips.
- Both callers are updated: BackupRestoreSettingsFragment (the
  application context is captured inside its Dispatchers.IO block) and
  ComposeBackupRestoreHost.

Rejected fix proposals:
- Keep the no-argument checkpoint() and call getInstance() in each
  caller first. This leaves the hidden precondition in the API, so a
  new caller can reintroduce the crash.
- Initialize the database at app startup. This adds startup work to
  every launch to fix one code path, and checkpoint() would still
  depend on that ordering.
This commit is contained in:
Shubham Naik authored and Tobi committed 2026-11-19 10:50:23 +01:00
1 parent cf0d4013d3
commit 757dd7382e
3 files changed
+21 -4

No files matched your search

@@ -60,9 +60,25 @@ object NewPipeDatabase {
return result!!
}
/**
* Runs `PRAGMA wal_checkpoint(full)`, folding the write-ahead log into the main database file
* so that copying only `newpipe.db` yields a complete backup.
*
* Opens the database if this process has not done so yet. That is required for correctness,
* not just convenience: committed but not yet checkpointed data lives in the `-wal` file, and
* only opening the database recovers it. This is reachable from a cold process where nothing
* has opened the database yet — e.g. the main page only has a Blank tab, or the process was
* reclaimed while the system file picker was in the foreground. Callers must not assume that
* some other component has already opened it.
*
* Blocks the calling thread, so call it off the main thread.
*
* @throws RuntimeException if the checkpoint was blocked from completing, i.e. another
* connection kept part of the log from being folded into `newpipe.db`.
*/
@JvmStatic
fun checkpoint() {
val database = checkNotNull(databaseInstance) { "database is not initialized" }
fun checkpoint(context: Context) {
val database = getInstance(context)
runBlocking {
database.useWriterConnection { transactor ->
transactor.usePrepared("pragma wal_checkpoint(full)") { statement ->
@@ -116,7 +116,8 @@ class BackupRestoreSettingsFragment : BasePreferenceFragment() {
) {
// checkpoint before export
withContext(Dispatchers.IO) {
NewPipeDatabase.checkpoint()
val appContext = requireContext().applicationContext
NewPipeDatabase.checkpoint(appContext)
}
manager.exportDatabase(defaultPreferences, file)
@@ -74,7 +74,7 @@ class ComposeBackupRestoreHost(private val context: Context) : BackupRestoreHost
override suspend fun exportData(file: String): DataExportResult = withContext(Dispatchers.IO) {
try {
NewPipeDatabase.checkpoint()
NewPipeDatabase.checkpoint(context)
manager.exportDatabase(preferences, zipFile(file))
// Remembered only on success, so the next picker opens where it worked
saveLastBackupFile(file)