Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writeCopyTo from non-synced to synced realm crashes when embedded object is undefined #5389

Open
fkjohn opened this issue Feb 8, 2023 · 3 comments

Comments

@fkjohn
Copy link

fkjohn commented Feb 8, 2023

How frequently does the bug occur?

Always

Description

I have an app which allows the user to choose if he wants to have an offline, non-synced or a synced realm. The goal is to offer the user to switch between both.
The data model contains objects which have embedded objects as attribute. It is no problem the create objects where the embedded object is undefined, no matter if the user starts non-synced or synced. The attribute is also not set as mandatory, so this is the behavior I expect.
However, when converting a non-synced realm to a synced realm with writeCopyTo the app crashes without meaningful error message. Converting a synced to a local realm is working without problem.

When investigating the issues it seems as if the error happens during the sync process to atlas. The realm file gets created correctly during writeCopyTo, and all data is existing in it. However, the data is not synced to the Atlas database. When creating new data after starting the app again, the new objects are synced to the Atlas database - however the ones that should be synced during migration are not.

Stacktrace & log output

The app crashes without crash report on the simulator, but I was able to retrieve the stacktrace from Sentry on the production app.

SIGABRT: Assertion failed: val.is_type(type_Link, type_TypedLink)
  libsystem_kernel    0x201efc200  __pthread_kill
  libsystem_pthread   0x21235e1a8  pthread_kill
  libsystem_c         0x1cc931c88  abort
  daytes              0x1006ab598  please_report_this_issue_in_github_realm_realm_core_v_13_1_1
  daytes              0x1006ab880  realm::util::terminate_internal
  daytes              0x1006ab6e0  realm::util::terminate
  daytes              0x10068b550  (anonymous namespace)::generate_properties_for_obj
  daytes              0x100688f08  realm::Transaction::replicate
  daytes              0x10059dc00  realm::DB::create_new_history
  daytes              0x10059e0bc  realm::DB::create_new_history
  daytes              0x1003edd94  realm::Realm::convert
  daytes              0x1002d9c1c  realm::js::RealmClass<T>::writeCopyTo
  daytes              0x10028b1dc  realm::js::wrap<T>
  daytes              0x100cd0b78  std::__1::__function::__value_func<T>::operator() (function.h:505)
  daytes              0x100cd0b78  std::__1::function<T>::operator() (function.h:1182)
  hermes              0x1019e029c  facebook::hermes::debugger::Debugger::jsiValueFromHermesValue
  hermes              0x1019f42f4  facebook::jsi::JSError::~JSError
  hermes              0x101a12c70  facebook::jsi::JSError::~JSError
  hermes              0x101a1218c  facebook::jsi::JSError::~JSError
  hermes              0x1019f4fcc  facebook::jsi::JSError::~JSError
  hermes              0x101a98c6c  facebook::jsi::JSError::~JSError
  hermes              0x1019f42f4  facebook::jsi::JSError::~JSError
  hermes              0x101a12c70  facebook::jsi::JSError::~JSError
  hermes              0x101a1218c  facebook::jsi::JSError::~JSError
  hermes              0x1019f459c  facebook::jsi::JSError::~JSError
  hermes              0x1019f2eb0  facebook::jsi::JSError::~JSError
  hermes              0x101ac4704  facebook::jsi::JSError::~JSError
  hermes              0x1019f42f4  facebook::jsi::JSError::~JSError
  hermes              0x101a12c70  facebook::jsi::JSError::~JSError
  hermes              0x101a1218c  facebook::jsi::JSError::~JSError
  hermes              0x1019f459c  facebook::jsi::JSError::~JSError
  hermes              0x1019f3e04  facebook::jsi::JSError::~JSError
  hermes              0x101a12c94  facebook::jsi::JSError::~JSError
  hermes              0x101a1218c  facebook::jsi::JSError::~JSError
  hermes              0x1019f459c  facebook::jsi::JSError::~JSError
  hermes              0x1019f3e04  facebook::jsi::JSError::~JSError
  hermes              0x1019d8d58  facebook::hermes::HermesRuntime::rootsListLength
  daytes              0x100ccfa88  facebook::jsi::RuntimeDecorator<T>::call (decorator.h:303)
  daytes              0x100ccfa88  facebook::jsi::WithRuntimeDecorator<T>::call (decorator.h:709)
  daytes              0x100c302c0  facebook::jsi::Function::call (jsi-inl.h:234)
  daytes              0x100c302c0  facebook::jsi::Function::call (jsi-inl.h:239)
  daytes              0x100c302c0  facebook::jsi::Function::call<T> (jsi-inl.h:247)
  daytes              0x100c30124  facebook::react::JSIExecutor::callFunction::lambda::operator() (JSIExecutor.cpp:255)
  daytes              0x100c30124  std::__1::__invoke<T> (type_traits:3918)
  daytes              0x100c30124  std::__1::__invoke_void_return_wrapper<T>::__call<T> (invoke.h:61)
  daytes              0x100c30124  std::__1::__function::__alloc_func<T>::operator() (function.h:178)
  daytes              0x100c30124  std::__1::__function::__func<T>::operator() (function.h:352)
  daytes              0x100a9ebc0  std::__1::__invoke<T> (type_traits:3918)
  daytes              0x100a9ebc0  std::__1::__invoke_void_return_wrapper<T>::__call<T> (invoke.h:61)
  daytes              0x100c2d678  std::__1::__function::__value_func<T>::operator() (function.h:505)
  daytes              0x100c2d678  std::__1::function<T>::operator() (function.h:1182)
  daytes              0x100c2d678  facebook::react::JSIExecutor::callFunction (JSIExecutor.cpp:253)
  daytes              0x100c058f8  std::__1::__function::__value_func<T>::operator() (function.h:505)
  daytes              0x100c058f8  std::__1::function<T>::operator() (function.h:1182)
  daytes              0x100c058f8  facebook::react::NativeToJsBridge::runOnExecutorQueue::lambda::operator() (NativeToJsBridge.cpp:310)
  daytes              0x100c058f8  std::__1::__invoke<T> (type_traits:3918)
  daytes              0x100c058f8  std::__1::__invoke_void_return_wrapper<T>::__call<T> (invoke.h:61)
  daytes              0x100c058f8  std::__1::__function::__alloc_func<T>::operator() (function.h:178)
  daytes              0x100c058f8  std::__1::__function::__func<T>::operator() (function.h:352)
  daytes              0x100af5214  std::__1::__function::__value_func<T>::operator() (function.h:505)
  daytes              0x100af5214  std::__1::function<T>::operator() (function.h:1182)
  daytes              0x100af5214  facebook::react::tryAndReturnError (RCTCxxUtils.mm:74)
  daytes              0x100b002ac  facebook::react::RCTMessageThread::tryFunc (RCTMessageThread.mm:69)
  daytes              0x100b00060  std::__1::__function::__value_func<T>::operator() (function.h:505)
  daytes              0x100b00060  std::__1::function<T>::operator() (function.h:1182)
  daytes              0x100b00060  facebook::react::RCTMessageThread::runAsync (RCTMessageThread.mm:45)
  CoreFoundation      0x1c5337560  __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__
  CoreFoundation      0x1c539fd98  __CFRunLoopDoBlocks
  CoreFoundation      0x1c53701f4  __CFRunLoopRun
  CoreFoundation      0x1c5374ed0  CFRunLoopRunSpecific
  daytes              0x100aeb28c  +[RCTCxxBridge runRunLoop] (RCTCxxBridge.mm:335)
  Foundation          0x1bf72f804  __NSThread__start__
  libsystem_pthread   0x2123586c8  _pthread_start

Can you reproduce the bug?

Always

Reproduction Steps

I've created a repo to reproduce the bug based on the Expo Task Template:
https://github.com/fkjohn/realm-crash

It works when setting {} as value of the embedded object:

  1. Create account
  2. Use second "add new task description" bar and click on the plus button
  3. Click "convert offline to synced realm"
  4. App does not crash, realm is converted correctly

When leaving the embedded object undefined, it crashes:

  1. Create account
  2. Use first "add new task description - will crash" bar and click on the plus button
  3. Click "convert offline to synced realm"
  4. App does crash, realm file exists and can be opened in Realm Studio but data is not synced

Version

11.3.1, also noticed with earlier versions of v10 and v11

What services are you using?

Atlas Device Sync

Are you using encryption?

No

Platform OS and version(s)

iOS

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

@kneth
Copy link
Contributor

kneth commented Feb 8, 2023

@fkjohn Thank you for reporting and providing a reproduction case.

I have experimented a bit by using node.js on MacOS, and I have created the following script (convert.js):

const Realm = require("realm");
const appId = "gh5389-kjtot";

const crash = process.argv.length === 2;

const TaskSetting = {
	name: "TaskSetting",
	embedded: true,
	properties: {
		isActive: 'bool?',
	},
};

const Task = {
	name: "Task",
	primaryKey: '_id',
	properties: {
            _id: {type: 'objectId', default: () => new Realm.BSON.ObjectId()},
            description: 'string',
            isComplete: {type: 'bool', default: false},
            createdAt: {type: 'date', default: () => new Date()},
            userId: 'string',
            settings: 'TaskSetting',
        },
};

const app = new Realm.App(appId);
app.logIn(Realm.Credentials.anonymous()).then(user => {
	const configSync = {
		path: "converted",
		schema: [Task, TaskSetting],
		sync: {
			partitionValue: user.id,
			user, 
		},
	};
	const configLocal = {
		schema: [Task, TaskSetting],
	};

	console.log("Open Realm: ", JSON.stringify(configLocal));
	Realm.open(configLocal).then(realm => {
		console.log("Realm at ", realm.path);
		console.log("Task 1");
		realm.write(() => {
			realm.create("Task", {
				description: "Task 1",
				userId: user.id,
				settings: {
					isActive: true,
				},
			});
		});

		if (crash) {
			console.log("Task 2");
			realm.write(() => {
				realm.create("Task", {
					description: "Task 2",
					userId: user.id,
				});
			});
		}

		console.log("Convert to synced Realm");
		realm.writeCopyTo(configSync);

		console.log("Closing");
		realm.close();
		console.log("Closed!");
		process.exit();
	}).catch(err => console.log(err));
}).catch(err => console.log(err));

If I run node convert.js no-crash I observe no errors. But while I run node convert.js (and setting crash to true), I get the following error Error: No object with key '4840227489' in 'class_TaskSetting'.

We need to investigate it further.

@danieltabacaru
Copy link

@fkjohn I can repro it in core. We'll prepare a fix for it.

cbush pushed a commit to mongodb/docs-realm that referenced this issue Apr 27, 2023
<h3>Snyk has created this PR to upgrade realm from 11.5.2 to
11.6.0.</h3>

:information_source: Keep your dependencies up-to-date. This makes it
easier to fix existing vulnerabilities and to more quickly identify and
fix newly disclosed vulnerabilities when they affect your project.
<hr/>

- The recommended version is **1 version** ahead of your current
version.
- The recommended version was released **22 days ago**, on 2023-03-23.


<details>
<summary><b>Release notes</b></summary>
<br/>
  <details>
    <summary>Package name: <b>realm</b></summary>
    <ul>
      <li>
<b>11.6.0</b> - <a
href="https://snyk.io/redirect/github/realm/realm-js/releases/tag/v11.6.0">2023-03-23</a></br><h3>Enhancements</h3>
<ul>
<li>Added configuration option <code>App.baseFilePath</code> which
controls where synced Realms and metadata is stored.</li>
</ul>
<h3>Fixed</h3>
<ul>
<li>Fix type error when using <code>realm.create</code> in combination
with class base models. (since v11.0.0)</li>
</ul>
<h3>Compatibility</h3>
<ul>
<li>React Native &gt;= v0.71.0</li>
<li>Realm Studio v13.0.0.</li>
<li>File format: generates Realms with format v23 (reads and upgrades
file format v5 or later for non-synced Realm, upgrades file format v10
or later for synced Realms).</li>
</ul>
<h3>Internal</h3>



<ul>
<li>Test</li>
</ul>
      </li>
      <li>
<b>11.5.2</b> - <a
href="https://snyk.io/redirect/github/realm/realm-js/releases/tag/v11.5.2">2023-03-16</a></br><h3>Fixed</h3>
<ul>
<li>Suppress omitting <code>objcMsgsend</code> stubs to ensure backward
compatibility with Xcode 13. It can be observed as <code>Undefined
symbols for architecture arm64: "_objc_msgSend$allBundles", referenced
from: realm::copy_bundled_realm_files() in
librealm-js-ios.a(platform.o)</code> when using a React Native app for
iOS. (<a
href="https://snyk.io/redirect/github/realm/realm-js/issues/5511"
data-hovercard-type="issue"
data-hovercard-url="/realm/realm-js/issues/5511/hovercard">#5511</a>,
since v11.5.1)</li>
<li>It is not allowed to specify
<code>deleteRealmIfMigrationIsNeeded</code> and sync. This can lead to
error messages like <code>Schema validation failed due to the following
errors</code>. (<a
href="https://snyk.io/redirect/github/realm/realm-js/issues/5548"
data-hovercard-type="issue"
data-hovercard-url="/realm/realm-js/issues/5548/hovercard">#5548</a>,
v10.12.0)</li>
<li>Installation will no longer hang when using Node 19. (<a
href="https://snyk.io/redirect/github/realm/realm-js/issues/5136"
data-hovercard-type="issue"
data-hovercard-url="/realm/realm-js/issues/5136/hovercard">#5136</a>,
since v10.13.0)</li>
<li>Fixed enums which was accidentally exported on the
<code>Realm</code> namespace without a backing implementation. (<a
href="https://snyk.io/redirect/github/realm/realm-js/pull/5493"
data-hovercard-type="pull_request"
data-hovercard-url="/realm/realm-js/pull/5493/hovercard">#5493</a>,
since v11.0.0)</li>
<li>Converting local Realm to synced Realm crashes if an embedded object
is null. (<a
href="https://snyk.io/redirect/github/realm/realm-js/issues/5389"
data-hovercard-type="issue"
data-hovercard-url="/realm/realm-js/issues/5389/hovercard">#5389</a>,
since v10.13.0)</li>
<li>Fixed performance degradation on subqueries. (<a
href="https://snyk.io/redirect/github/realm/realm-core/issues/6327"
data-hovercard-type="issue"
data-hovercard-url="/realm/realm-core/issues/6327/hovercard">realm/realm-core#6327</a>,
since v6.0.0)</li>
<li>Fixed crash if secure transport returns an error with a non-zero
length. It can be observed as <code>Reading failed: Premature end of
input</code> in the log. (<a
href="https://snyk.io/redirect/github/realm/realm-core/issues/5435"
data-hovercard-type="issue"
data-hovercard-url="/realm/realm-core/issues/5435/hovercard">realm/realm-core#5435</a>,
since v10.0.0)</li>
<li>Creating subscriptions with queries having Unicode parameters causes
a server error e.g., <code>query from client:
"{"Product":"(stringQueryField BEGINSWITH B64\"2KzZhdi52Kpz\" )"}" could
not be parsed</code>. (<a
href="https://snyk.io/redirect/github/realm/realm-core/issues/6350"
data-hovercard-type="issue"
data-hovercard-url="/realm/realm-core/issues/6350/hovercard">realm/realm-core#6350</a>,
since v10.11.0)</li>
</ul>
<h3>Compatibility</h3>
<ul>
<li>React Native &gt;= v0.71.0</li>
<li>Atlas App Services.</li>
<li>Realm Studio v13.0.0.</li>
<li>File format: generates Realms with format v23 (reads and upgrades
file format v5 or later for non-synced Realm, upgrades file format v10
or later for synced Realms).</li>
</ul>
<h3>Internal</h3>
<ul>
<li>Upgraded Realm Core from v13.4.2 to v13.6.0. (<a
href="https://snyk.io/redirect/github/realm/realm-js/issues/5495"
data-hovercard-type="issue"
data-hovercard-url="/realm/realm-js/issues/5495/hovercard">#5495</a>)</li>
<li>All exceptions thrown out of Realm Core are now of type
<code>Exception</code>.</li>
</ul>
      </li>
    </ul>
from <a
href="https://snyk.io/redirect/github/realm/realm-js/releases">realm
GitHub release notes</a>
  </details>
</details>


<details>
  <summary><b>Commit messages</b></summary>
  </br>
  <details>
    <summary>Package name: <b>realm</b></summary>
    <ul>
<li><a
href="https://snyk.io/redirect/github/realm/realm-js/commit/5196e76d65c5faffa4246daeb3683ac6d0d17b35">5196e76</a>
[11.6.0] Bump version</li>
<li><a
href="https://snyk.io/redirect/github/realm/realm-js/commit/430afad55f3883c210449470788b93cc7dc41cd0">430afad</a>
Expose baseFilePath on App configuration (#5572)</li>
<li><a
href="https://snyk.io/redirect/github/realm/realm-js/commit/22977eaf83d21acec6200f636f75e2c99d365085">22977ea</a>
Fix Create Call Signature (#5617)</li>
<li><a
href="https://snyk.io/redirect/github/realm/realm-js/commit/832fd6a03d30123e047996005c6c1fc812653048">832fd6a</a>
Reenable all tests (#5606)</li>
<li><a
href="https://snyk.io/redirect/github/realm/realm-js/commit/b7245b3c60305baee7a22c35857c71de4fb407ff">b7245b3</a>
Disable ccache for node builds (#5602)</li>
<li><a
href="https://snyk.io/redirect/github/realm/realm-js/commit/bf629d375370285c8870b025c704b8a65b008c4f">bf629d3</a>
Prepare for vNext (#5567)</li>
<li><a
href="https://snyk.io/redirect/github/realm/realm-js/commit/46711a59419a87c8a3bf973d155ff8fe9a12818c">46711a5</a>
Prepare for 11.5.2 (#5560)</li>
    </ul>

<a
href="https://snyk.io/redirect/github/realm/realm-js/compare/d401cbeb41e76d42a45ba5dd4dc0b26eb71cbb9c...5196e76d65c5faffa4246daeb3683ac6d0d17b35">Compare</a>
  </details>
</details>
<hr/>

**Note:** *You are seeing this because you or someone else with access
to this repository has authorized Snyk to open upgrade PRs.*

For more information: <img
src="https://api.segment.io/v1/pixel/track?data=eyJ3cml0ZUtleSI6InJyWmxZcEdHY2RyTHZsb0lYd0dUcVg4WkFRTnNCOUEwIiwiYW5vbnltb3VzSWQiOiJiYmYyNzYyNS0wZWU5LTQ2MDEtODU2MS00Y2Y4YmY2ZGY0MmYiLCJldmVudCI6IlBSIHZpZXdlZCIsInByb3BlcnRpZXMiOnsicHJJZCI6ImJiZjI3NjI1LTBlZTktNDYwMS04NTYxLTRjZjhiZjZkZjQyZiJ9fQ=="
width="0" height="0"/>

🧐 [View latest project
report](https://app.snyk.io/org/sandbox-2ba/project/852e6e4f-be96-45c8-b370-1060f5ebee55?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr)

🛠 [Adjust upgrade PR
settings](https://app.snyk.io/org/sandbox-2ba/project/852e6e4f-be96-45c8-b370-1060f5ebee55/settings/integration?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr)

🔕 [Ignore this dependency or unsubscribe from future upgrade
PRs](https://app.snyk.io/org/sandbox-2ba/project/852e6e4f-be96-45c8-b370-1060f5ebee55/settings/integration?pkg&#x3D;realm&amp;utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr#auto-dep-upgrades)

<!---
(snyk:metadata:{"prId":"bbf27625-0ee9-4601-8561-4cf8bf6df42f","prPublicId":"bbf27625-0ee9-4601-8561-4cf8bf6df42f","dependencies":[{"name":"realm","from":"11.5.2","to":"11.6.0"}],"packageManager":"npm","type":"auto","projectUrl":"https://app.snyk.io/org/sandbox-2ba/project/852e6e4f-be96-45c8-b370-1060f5ebee55?utm_source=github&utm_medium=referral&page=upgrade-pr","projectPublicId":"852e6e4f-be96-45c8-b370-1060f5ebee55","env":"prod","prType":"upgrade","vulns":[],"issuesToFix":[],"upgrade":[],"upgradeInfo":{"versionsDiff":1,"publishedDate":"2023-03-23T17:43:34.744Z"},"templateVariants":[],"hasFixes":false,"isMajorUpgrade":false,"isBreakingChange":false,"priorityScoreList":[]})
--->

---------

Co-authored-by: snyk-bot <snyk-bot@snyk.io>
@bimusiek
Copy link
Contributor

Was this fixed? We are getting the similar issue when fetching relationship of the object.
CleanShot 2024-08-26 at 14 02 30@2x
realm: 12.12.1
react-native with Hermes enabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants