Skip to content

Commit c38a503

Browse files
araujoguiaduh95
authored andcommitted
sqlite: settle BackupJob through a single exit point
Route every exit through Settle(), which finalizes, settles the promise and deletes the job once. This also frees the job when building the progress object fails. Signed-off-by: Guilherme Araújo <arauujogui@gmail.com> PR-URL: #66912 Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 2ec3329 commit c38a503

1 file changed

Lines changed: 18 additions & 34 deletions

File tree

‎src/node_sqlite.cc‎

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ class BackupJob : public ThreadPoolWork {
645645
Local<Promise::Resolver> resolver =
646646
Local<Promise::Resolver>::New(env()->isolate(), resolver_);
647647
if (backup_status_ != SQLITE_OK) {
648-
HandleBackupError(resolver);
648+
Settle(resolver, CreateSQLiteError(isolate, dest_.get()), {});
649649
return;
650650
}
651651

@@ -654,7 +654,7 @@ class BackupJob : public ThreadPoolWork {
654654
source_->Connection(),
655655
source_db_.c_str()));
656656
if (!backup_) {
657-
HandleBackupError(resolver);
657+
Settle(resolver, CreateSQLiteError(isolate, dest_.get()), {});
658658
return;
659659
}
660660

@@ -676,7 +676,7 @@ class BackupJob : public ThreadPoolWork {
676676

677677
if (!(backup_status_ == SQLITE_OK || backup_status_ == SQLITE_DONE ||
678678
backup_status_ == SQLITE_BUSY || backup_status_ == SQLITE_LOCKED)) {
679-
HandleBackupError(resolver, backup_status_);
679+
Settle(resolver, CreateSQLiteError(isolate, backup_status_), {});
680680
return;
681681
}
682682

@@ -702,17 +702,15 @@ class BackupJob : public ThreadPoolWork {
702702
env()->remaining_pages_string(),
703703
Integer::New(env()->isolate(), remaining_pages))
704704
.IsNothing()) {
705+
Settle(resolver, {}, {});
705706
return;
706707
}
707708

708709
Local<Value> argv[] = {progress_info};
709710
TryCatch try_catch(env()->isolate());
710711
USE(fn->Call(env()->context(), Null(env()->isolate()), 1, argv));
711712
if (try_catch.HasCaught()) {
712-
Local<Value> exception = try_catch.Exception();
713-
Finalize();
714-
resolver->Reject(env()->context(), exception).ToChecked();
715-
delete this;
713+
Settle(resolver, try_catch.Exception(), {});
716714
return;
717715
}
718716
}
@@ -723,15 +721,11 @@ class BackupJob : public ThreadPoolWork {
723721
}
724722

725723
if (backup_status_ != SQLITE_DONE) {
726-
HandleBackupError(resolver);
724+
Settle(resolver, CreateSQLiteError(isolate, dest_.get()), {});
727725
return;
728726
}
729727

730-
Finalize();
731-
resolver
732-
->Resolve(env()->context(), Integer::New(env()->isolate(), total_pages))
733-
.ToChecked();
734-
delete this;
728+
Settle(resolver, {}, Integer::New(isolate, total_pages));
735729
}
736730

737731
void Finalize() {
@@ -752,29 +746,19 @@ class BackupJob : public ThreadPoolWork {
752746
}
753747

754748
private:
755-
void HandleBackupError(Local<Promise::Resolver> resolver) {
756-
Local<Object> e;
757-
if (!CreateSQLiteError(env()->isolate(), dest_.get()).ToLocal(&e)) {
758-
Finalize();
759-
delete this;
760-
return;
761-
}
762-
749+
// The single exit point of a backup. Rejects with `error` if it is set,
750+
// otherwise resolves with `value` if it is set. Neither being set means an
751+
// exception is already pending. Deletes the job.
752+
void Settle(Local<Promise::Resolver> resolver,
753+
MaybeLocal<Value> error,
754+
Local<Value> value) {
763755
Finalize();
764-
resolver->Reject(env()->context(), e).ToChecked();
765-
delete this;
766-
}
767-
768-
void HandleBackupError(Local<Promise::Resolver> resolver, int errcode) {
769-
Local<Object> e;
770-
if (!CreateSQLiteError(env()->isolate(), errcode).ToLocal(&e)) {
771-
Finalize();
772-
delete this;
773-
return;
756+
Local<Value> e;
757+
if (error.ToLocal(&e)) {
758+
resolver->Reject(env()->context(), e).ToChecked();
759+
} else if (!value.IsEmpty()) {
760+
resolver->Resolve(env()->context(), value).ToChecked();
774761
}
775-
776-
Finalize();
777-
resolver->Reject(env()->context(), e).ToChecked();
778762
delete this;
779763
}
780764

0 commit comments

Comments
 (0)