realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.45k stars 1.75k forks source link

Fix deadlock during manual client reset #7696

Closed clementetb closed 1 year ago

clementetb commented 1 year ago

Closes https://github.com/realm/realm-java/issues/7691

A deadlock will occur if we try to close the last Realm instance while inside a manual client reset block.

When we receive a sync error we acquire a lock on the Sync instance, while locked we are required to close all open Realm instances. This last instance would try to close the sync session, but it needs to acquire the lock that was previously acquired by the sync error logic.

It was not captured in our tests because the simulated sync error method did not simulate that the error came from a different thread than the one being executed.

This PR addresses this issue by not acquiring the lock on client reset errors.