Assertion failed: has_refs()
O-Community
### How frequently does the bug occur?
Always
### Description
We're encountering a **crash in Realm Core** with the following message:
`Assertion failed: has_refs()` and this `terminating due to uncaught exception of type realm::util::DecryptionFailed: Decryption failed: page 98 in file of size 3198976 failed the HMAC check. Either the encryption key is incorrect or data is corrupted`
This occurs when **two processes simultaneously access the same encrypted Realm file** - one writing, the other reading. Based on our investigation, this seems to be a **race condition** that happens during concurrent encrypted access across processes.
### Reproduction
We've created a minimal example with two apps that reproduce the issue:
- **TestApp1** (the *listener*): opens the encrypted Realm and continuously reads from it.
- **TestApp2** (the *writer*): opens the same Realm file and performs repeated writes.
#### Steps to Reproduce:
0. Clone this: https://github.com/c128128/RealmBug
1. Start **TestApp1** (the listener).
2. Then launch **TestApp2** (the writer).
3. Within a few moments, **TestApp1 crashes** with:
```
Assertion failed: m_has_refs with (m_ref, ndx, m_size) = [5232, 0, 0]
```
or sometimes with:
```
terminating due to uncaught exception of type realm::util::DecryptionFailed: Decryption failed: page 98 in file of size 3198976 failed the HMAC check. Either the encryption key is incorrect or data is corrupted
```
### Environment
- **RealmSwift version**: 10.54.5
- **RealmCore version**: 14.14.0
- **Platform**: Mac Catalyst
- **Encryption**: Enabled (64-byte key)
- **Concurrency**: Two processes accessing the same Realm file simultaneously
### Key Observations
- The crash **does not occur** with an **unencrypted Realm**.
- The crash seems to occur during memory mapping or decryption, possibly due to **unsynchronized access to shared encrypted pages**.
- Appears to be an issue within Realm Core's internal caching or reference tracking.
- The bug was **introduced after `realm-swift` 10.51.0** - everything works fine on 10.51.0, but crashes consistently starting with **10.52.0** and later versions.
### Can you reproduce the bug?
Always
### Version
10.54.5
### What Atlas Services are you using?
Local Database only
### Are you using encryption?
Yes
### Platform OS and version(s)
macOS 15.5 (Apple M1 Max)
related https://github.com/realm/realm-core/issues/8081
1 条评论