Skip to content

Commit

Permalink
Fix an assertion failure if an async write callback ran during a writ…
Browse files Browse the repository at this point in the history
…e transaction (#6661)

Between when the callback after acquiring the write lock is scheduled and when
it's invoked a synchronous write transaction can be begun, and if it's not
ended before the next time the scheduler gets to run, the scheduled callback
will be invoked inside the write. When this happens we want to just do nothing.
Ending the synchronous write transaction will take care of rescheduling the
async write it preempted.
  • Loading branch information
tgoyne authored May 26, 2023
1 parent 75ae0e0 commit 85a4e63
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Access token refresh for websockets was not updating the location metadata ([#6630](https://github.com/realm/realm-core/issues/6630), since v13.9.3)
* Fix several UBSan failures which did not appear to result in functional bugs ([#6649](https://github.com/realm/realm-core/pull/6649)).
* Fix an out-of-bounds read in sectioned results when sectioned are removed by modifying all objects in that section to no longer appear in that section ([#6649](https://github.com/realm/realm-core/pull/6649), since v13.12.0)
* Using both synchronous and asynchronous transactions on the same thread or scheduler could hit the assertion failure "!realm.is_in_transaction()" if one of the callbacks for an asynchronous transaction happened to be scheduled during a synchronous transaction ([#6659](https://github.com/realm/realm-core/issues/6659), since v11.8.0)

### Breaking changes
* None.
Expand Down
6 changes: 6 additions & 0 deletions src/realm/object-store/shared_realm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,12 @@ void Realm::run_writes()
// writes as we can't add commits while in that state
return;
}
if (is_in_transaction()) {
// This is scheduled asynchronously after acquiring the write lock, so
// in that time a synchronous transaction may have been started. If so,
// we'll be re-invoked when that transaction ends.
return;
}

CountGuard running_writes(m_is_running_async_writes);
int run_limit = 20; // max number of commits without full sync to disk
Expand Down
34 changes: 30 additions & 4 deletions test/object-store/realm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,7 @@ TEST_CASE("SharedRealm: async writes") {
"an error");
REQUIRE(realm->is_closed());
}
#endif
SECTION("exception thrown from async commit completion callback with error handler") {
Realm::AsyncHandle h;
realm->set_async_error_handler([&](Realm::AsyncHandle handle, std::exception_ptr error) {
Expand All @@ -1912,6 +1913,7 @@ TEST_CASE("SharedRealm: async writes") {
wait_for_done();
verify_persisted_count(1);
}
#ifndef _WIN32
SECTION("exception thrown from async commit completion callback without error handler") {
realm->begin_transaction();
table->create_object();
Expand All @@ -1924,6 +1926,7 @@ TEST_CASE("SharedRealm: async writes") {
"an error");
REQUIRE(table->size() == 1);
}
#endif

if (_impl::SimulatedFailure::is_enabled()) {
SECTION("error in the synchronous part of async commit") {
Expand Down Expand Up @@ -2340,15 +2343,38 @@ TEST_CASE("SharedRealm: async writes") {
REQUIRE(table->size() == 6);
}

SECTION("async writes which would run inside sync writes are deferred") {
realm->async_begin_transaction([&] {
done = true;
});

// Wait for the background thread to hold the write lock (without letting
// the event loop run so that the scheduled task isn't run)
DBOptions options;
options.encryption_key = config.encryption_key.data();
auto db = DB::create(make_in_realm_history(), config.path, options);
while (db->start_write(true))
millisleep(1);

realm->begin_transaction();

// Invoke the pending callback
util::EventLoop::main().run_pending();
// Should not have run the async write block
REQUIRE(done == false);

// Should run the async write block once the synchronous transaction is done
realm->cancel_transaction();
REQUIRE(done == false);
util::EventLoop::main().run_pending();
REQUIRE(done == true);
}

util::EventLoop::main().run_until([&] {
return !realm || !realm->has_pending_async_work();
});
#endif


#ifdef _WIN32
_impl::RealmCoordinator::clear_all_caches();
#endif
}
// Our libuv scheduler currently does not support background threads, so we can
// only run this on apple platforms
Expand Down
23 changes: 23 additions & 0 deletions test/object-store/util/event_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ struct EventLoop::Impl {
// Schedule execution of the given function on the event loop.
void perform(util::UniqueFunction<void()>);

// Run the event loop until all currently pending work has been run.
void run_pending();

~Impl();

private:
Expand Down Expand Up @@ -124,6 +127,11 @@ void EventLoop::perform(util::UniqueFunction<void()> function)
return m_impl->perform(std::move(function));
}

void EventLoop::run_pending()
{
return m_impl->run_pending();
}

#if REALM_USE_UV

bool EventLoop::has_implementation()
Expand Down Expand Up @@ -204,6 +212,11 @@ void EventLoop::Impl::perform(util::UniqueFunction<void()> f)
uv_async_send(&m_perform_work);
}

void EventLoop::Impl::run_pending()
{
uv_run(m_loop, UV_RUN_NOWAIT);
}

#elif REALM_PLATFORM_APPLE

bool EventLoop::has_implementation()
Expand Down Expand Up @@ -254,6 +267,12 @@ void EventLoop::Impl::perform(util::UniqueFunction<void()> func)
CFRunLoopWakeUp(m_loop.get());
}

void EventLoop::Impl::run_pending()
{
while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) == kCFRunLoopRunHandledSource)
;
}

#else

bool EventLoop::has_implementation()
Expand All @@ -273,5 +292,9 @@ void EventLoop::Impl::perform(util::UniqueFunction<void()>)
{
printf("WARNING: there is no event loop implementation and nothing is happening.\n");
}
void EventLoop::Impl::run_pending()
{
printf("WARNING: there is no event loop implementation and nothing is happening.\n");
}

#endif
3 changes: 3 additions & 0 deletions test/object-store/util/event_loop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ struct EventLoop {
// Schedule execution of the given function on the event loop.
void perform(util::UniqueFunction<void()>);

// Run the event loop until all currently pending work has been run.
void run_pending();

EventLoop(EventLoop&&) = default;
EventLoop& operator=(EventLoop&&) = default;
~EventLoop();
Expand Down

0 comments on commit 85a4e63

Please sign in to comment.