Skip to content

Commit 57404f6

Browse files
authored
RCORE-2004 Make Realm::call_completion_callbacks() resilient to trasaction being deleted (#7437)
1 parent 2e9cee0 commit 57404f6

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Fix an assertion failure "m_lock_info && m_lock_info->m_file.get_path() == m_filename" that appears to be related to opening a Realm while the file is in the process of being closed on another thread ([Swift #8507](https://github.com/realm/realm-swift/issues/8507)).
99
* Fixed diverging history due to a bug in the replication code when setting default null values (embedded objects included) ([#7536](https://github.com/realm/realm-core/issues/7536)).
1010
* Version 19.39.33523 of MSVC would crash when compiling for arm64 in release mode ([PR #7533](https://github.com/realm/realm-core/pull/7533)).
11+
* Null pointer exception may be triggered when logging out and async commits callbacks not executed ([#7434](https://github.com/realm/realm-core/issues/7434), since v13.26.0)
1112

1213
### Breaking changes
1314
* Updated default base URL to be `https://services.cloud.mongodb.com` to support the new domains (was `https://realm.mongodb.com`). ([PR #7534](https://github.com/realm/realm-core/pull/7534))

src/realm/object-store/shared_realm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ void Realm::run_writes_on_proper_thread()
765765

766766
void Realm::call_completion_callbacks()
767767
{
768-
if (m_is_running_async_commit_completions) {
768+
if (m_is_running_async_commit_completions || m_async_commit_q.empty()) {
769769
return;
770770
}
771771

test/object-store/realm.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,6 +2440,68 @@ TEST_CASE("SharedRealm: async writes") {
24402440

24412441
_impl::RealmCoordinator::clear_all_caches();
24422442
}
2443+
2444+
TEST_CASE("Call run_async_completions after realm has been closed") {
2445+
// This requires a special scheduler as we have to call Realm::close
2446+
// just after DB::AsyncCommitHelper has made a callback to the function
2447+
// that asks the scheduler to invoke run_async_completions()
2448+
2449+
struct ManualScheduler : util::Scheduler {
2450+
std::mutex mutex;
2451+
std::condition_variable cv;
2452+
std::vector<util::UniqueFunction<void()>> callbacks;
2453+
2454+
void invoke(util::UniqueFunction<void()>&& cb) override
2455+
{
2456+
{
2457+
std::lock_guard lock(mutex);
2458+
callbacks.push_back(std::move(cb));
2459+
}
2460+
cv.notify_all();
2461+
}
2462+
2463+
bool is_on_thread() const noexcept override
2464+
{
2465+
return true;
2466+
}
2467+
bool is_same_as(const Scheduler*) const noexcept override
2468+
{
2469+
return false;
2470+
}
2471+
bool can_invoke() const noexcept override
2472+
{
2473+
return true;
2474+
}
2475+
};
2476+
2477+
auto scheduler = std::make_shared<ManualScheduler>();
2478+
2479+
TestFile config;
2480+
config.schema_version = 0;
2481+
config.schema = Schema{{"object", {{"value", PropertyType::Int}}}};
2482+
config.scheduler = scheduler;
2483+
config.automatic_change_notifications = false;
2484+
2485+
auto realm = Realm::get_shared_realm(config);
2486+
2487+
realm->begin_transaction();
2488+
realm->async_commit_transaction([](std::exception_ptr) {});
2489+
2490+
std::vector<util::UniqueFunction<void()>> callbacks;
2491+
{
2492+
std::unique_lock lock(scheduler->mutex);
2493+
// Wait for scheduler to be invoked
2494+
scheduler->cv.wait(lock, [&] {
2495+
return !scheduler->callbacks.empty();
2496+
});
2497+
callbacks.swap(scheduler->callbacks);
2498+
}
2499+
realm->close();
2500+
// Call whatever functions that was added to scheduler.
2501+
for (auto& cb : callbacks)
2502+
cb();
2503+
}
2504+
24432505
// Our libuv scheduler currently does not support background threads, so we can
24442506
// only run this on apple platforms
24452507
#if REALM_PLATFORM_APPLE

0 commit comments

Comments
 (0)