Skip to content

Commit e7ceffd

Browse files
amabluea-maurice
authored andcommitted
When a transaction completes, the NoopListener should be removed so that it
does not respond to further changes to the database. If it were not removed and the listener is deleted then this would result in a crash on the next write to the database. PiperOrigin-RevId: 251932053
1 parent 605a70d commit e7ceffd

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

database/src/desktop/core/repo.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,10 @@ void Repo::StartTransaction(const Path& path,
512512
new DatabaseReferenceInternal(database_, path);
513513
DatabaseReference watch_ref(ref_impl);
514514
UniquePtr<NoopListener> listener = MakeUnique<NoopListener>();
515+
NoopListener* listener_ptr = listener.get();
515516
QuerySpec query_spec(path);
516-
AddEventCallback(MakeUnique<ValueEventRegistration>(database_, listener.get(),
517-
query_spec));
517+
AddEventCallback(
518+
MakeUnique<ValueEventRegistration>(database_, listener_ptr, query_spec));
518519

519520
TransactionDataPtr transaction_data = MakeShared<TransactionData>(
520521
handle, api, query_spec.path, transaction_function, context,
@@ -535,6 +536,9 @@ void Repo::StartTransaction(const Path& path,
535536
transaction_data->status = TransactionData::kStatusNeedsAbort;
536537
transaction_data->ref_future->Complete(transaction_data->future_handle,
537538
kErrorWriteCanceled);
539+
// If there was an error, the listener must be removed to prevent calls to
540+
// it in case the listener is destroyed.
541+
RemoveEventCallback(listener_ptr, query_spec);
538542
} else {
539543
// Mark as run and add to our queue.
540544
transaction_data->status = TransactionData::kStatusRun;

0 commit comments

Comments
 (0)