Skip to content

Commit

Permalink
sqlite: clean up backups when database object is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Jan 12, 2025
1 parent 2aee11d commit 01ee4c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ class BackupJob : public ThreadPoolWork {
}
}

private:
void Cleanup() {
if (pBackup_) {
sqlite3_backup_finish(pBackup_);
Expand All @@ -299,7 +298,7 @@ class BackupJob : public ThreadPoolWork {
}
}

// https://github.com/nodejs/node/blob/649da3b8377e030ea7b9a1bc0308451e26e28740/src/crypto/crypto_keygen.h#L126
private:
int backup_status_;
Environment* env() const { return env_; }
sqlite3* pDest_;
Expand Down Expand Up @@ -459,6 +458,7 @@ void DatabaseSync::DeleteSessions() {
DatabaseSync::~DatabaseSync() {
if (IsOpen()) {
FinalizeStatements();
FinalizeBackups();
DeleteSessions();
sqlite3_close_v2(connection_);
connection_ = nullptr;
Expand Down Expand Up @@ -521,6 +521,14 @@ bool DatabaseSync::Open() {
return true;
}

void DatabaseSync::FinalizeBackups() {
for (auto backup : backups_) {
backup->Cleanup();
}

backups_.clear();
}

void DatabaseSync::FinalizeStatements() {
for (auto stmt : statements_) {
stmt->Finalize();
Expand Down Expand Up @@ -825,6 +833,7 @@ void DatabaseSync::Backup(const FunctionCallbackInfo<Value>& args) {

BackupJob* job = new BackupJob(
env, db, resolver, source_db, *destFilename, dest_db, rate, progressFunc);
db->backups_.insert(job);
job->ScheduleBackup();
}

Expand Down
4 changes: 3 additions & 1 deletion src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DatabaseOpenConfiguration {
};

class StatementSync;
class BackupJob;

class DatabaseSync : public BaseObject {
public:
Expand All @@ -65,6 +66,7 @@ class DatabaseSync : public BaseObject {
const v8::FunctionCallbackInfo<v8::Value>& args);
static void LoadExtension(const v8::FunctionCallbackInfo<v8::Value>& args);
void FinalizeStatements();
void FinalizeBackups();
void UntrackStatement(StatementSync* statement);
bool IsOpen();
sqlite3* Connection();
Expand All @@ -82,7 +84,7 @@ class DatabaseSync : public BaseObject {
bool enable_load_extension_;
sqlite3* connection_;

std::set<sqlite3_backup*> backups_;
std::set<BackupJob*> backups_;
std::set<sqlite3_session*> sessions_;
std::unordered_set<StatementSync*> statements_;

Expand Down

0 comments on commit 01ee4c0

Please sign in to comment.