Skip to content

Commit c3c8f74

Browse files
amabluea-maurice
amablue
authored andcommitted
Tear down connection explicitly during Repo destruction to prevent additional
messages from coming in while the sync tree is being torn down, which can cause a crash during shutdown. PiperOrigin-RevId: 246913461
1 parent 15875e0 commit c3c8f74

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

database/src/desktop/core/repo.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "database/src/desktop/core/repo.h"
16+
1617
#include "app/src/callback.h"
1718
#include "app/src/log.h"
1819
#include "app/src/scheduler.h"
@@ -87,8 +88,6 @@ Repo::Repo(App* app, DatabaseInternal* database, const char* url)
8788

8889
connection_.reset(new connection::PersistentConnection(app, host_info_, this,
8990
&s_scheduler_));
90-
connection_->ScheduleInitialize();
91-
9291
// Kick off any expensive additional initialization
9392
s_scheduler_.Schedule(NewCallback(
9493
[](ThisRef ref) {
@@ -98,9 +97,17 @@ Repo::Repo(App* app, DatabaseInternal* database, const char* url)
9897
}
9998
},
10099
safe_this_));
100+
101+
// Schedule the connection to initialize after the SyncTree is set up.
102+
connection_->ScheduleInitialize();
101103
}
102104

103-
Repo::~Repo() { safe_this_.ClearReference(); }
105+
Repo::~Repo() {
106+
// Terminate the connection immediately to prevent messages from arriving
107+
// while the SyncTree is being torn down.
108+
connection_.reset(nullptr);
109+
safe_this_.ClearReference();
110+
}
104111

105112
void Repo::AddEventCallback(UniquePtr<EventRegistration> event_registration) {
106113
PostEvents(server_sync_tree_->AddEventRegistration(Move(event_registration)));

0 commit comments

Comments
 (0)