Skip to content

Commit

Permalink
Ensure that all native callbacks are setup always (#2331)
Browse files Browse the repository at this point in the history
* Ensure that all native callbacks are setup always

* Make ShouldCompactOnLaunch a static callback + clear app cache on reset

* Update to new core

* Rework how gchandles are freed in native callbacks

* New Core

* Rename the unity package

* Make Migration non-disposable

* Update to latest Core

* Use Core v11.

* Use Core with the snapshot fix

* Try to enable core dumps

* Use new Core

* Revert to a less buggy Core version

* Add a link for a TODO entry.
  • Loading branch information
nirinchev authored Apr 28, 2021
1 parent 69e5b0d commit cfb9888
Show file tree
Hide file tree
Showing 24 changed files with 435 additions and 463 deletions.
6 changes: 3 additions & 3 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ stage('Unity Package') {

bat "dotnet run --project Tools/SetupUnityPackage/SetupUnityPackage/ -- --path ${packagePath} --utils-path ${utilsPackagePath} --weaver-path ${weaverPackagePath} --pack"
dir('Realm/Realm.Unity') {
archiveArtifacts "realm.unity-${packageVersion}.tgz"
bat "del realm.unity-${packageVersion}.tgz"
archiveArtifacts "io.realm.unity-${packageVersion}.tgz"
bat "del io.realm.unity-${packageVersion}.tgz"
}

bat "dotnet run --project Tools/SetupUnityPackage/SetupUnityPackage/ -- --path ${packagePath} --utils-path ${utilsPackagePath} --weaver-path ${weaverPackagePath} --include-dependencies --pack"
Expand Down Expand Up @@ -387,7 +387,7 @@ def NetCoreTest(String nodeName, String targetFramework) {
"objectid-partition-key": "${env.WORKSPACE}/Tests/TestApps/objectid-partition-key",
"uuid-partition-key": "${env.WORKSPACE}/Tests/TestApps/uuid-partition-key"
]) { networkName ->
test_runner_image.inside("--network=${networkName}") {
test_runner_image.inside("--network=${networkName} --ulimit core=-1") {
script += " --baasurl http://mongodb-realm:9090"
// see https://stackoverflow.com/a/53782505
sh """
Expand Down
85 changes: 0 additions & 85 deletions Realm/Realm.Unity/Runtime/Realm.dll.config.meta

This file was deleted.

6 changes: 3 additions & 3 deletions Realm/Realm.Unity/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "realm.unity",
"name": "io.realm.unity",
"version": "10.2.0-beta.2",
"displayName": "Realm",
"description": "Realm is a mobile database: a replacement for SQLite",
"unity": "2019.1",
"unityRelease": "0b5",
"unity": "2021.1",
"unityRelease": "3f1",
"dependencies": {
},
"keywords": [
Expand Down
31 changes: 10 additions & 21 deletions Realm/Realm/Configurations/RealmConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using System.Threading.Tasks;
using Realms.Exceptions;
using Realms.Helpers;
using Realms.Native;
using Realms.Schema;

namespace Realms
Expand Down Expand Up @@ -133,14 +132,14 @@ internal override Realm CreateRealm(RealmSchema schema)
if (MigrationCallback != null)
{
migration = new Migration(this, schema);
migration.PopulateConfiguration(ref configuration);
configuration.managed_migration_handle = GCHandle.ToIntPtr(migration.MigrationHandle);
}

GCHandle? shouldCompactHandle = null;
if (ShouldCompactOnLaunch != null)
{
var handle = GCHandle.Alloc(ShouldCompactOnLaunch);
configuration.should_compact_callback = ShouldCompactOnLaunchCallback;
configuration.managed_should_compact_delegate = GCHandle.ToIntPtr(handle);
shouldCompactHandle = GCHandle.Alloc(ShouldCompactOnLaunch);
configuration.managed_should_compact_delegate = GCHandle.ToIntPtr(shouldCompactHandle.Value);
}

var srPtr = IntPtr.Zero;
Expand All @@ -152,11 +151,16 @@ internal override Realm CreateRealm(RealmSchema schema)
{
throw new AggregateException("Exception occurred in a Realm migration callback. See inner exception for more details.", migration?.MigrationException);
}
finally
{
migration?.ReleaseHandle();
shouldCompactHandle?.Free();
}

var srHandle = new SharedRealmHandle(srPtr);
if (IsDynamic && !schema.Any())
{
srHandle.GetSchema(nativeSchema => schema = RealmSchema.CreateFromObjectStoreSchema(nativeSchema));
schema = srHandle.GetSchema();
}

return new Realm(srHandle, this, schema);
Expand All @@ -178,20 +182,5 @@ internal override Task<Realm> CreateRealmAsync(RealmSchema schema, CancellationT

return Task.FromResult(CreateRealm(schema));
}

[MonoPInvokeCallback(typeof(ShouldCompactCallback))]
private static bool ShouldCompactOnLaunchCallback(IntPtr delegatePtr, ulong totalSize, ulong dataSize)
{
var handle = GCHandle.FromIntPtr(delegatePtr);
var compactDelegate = (ShouldCompactDelegate)handle.Target;
try
{
return compactDelegate(totalSize, dataSize);
}
finally
{
handle.Free();
}
}
}
}
5 changes: 2 additions & 3 deletions Realm/Realm/Configurations/SyncConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,12 @@ internal override Realm CreateRealm(RealmSchema schema)
var srHandle = SharedRealmHandle.OpenWithSync(configuration, syncConfiguration, schema, EncryptionKey);
if (IsDynamic && !schema.Any())
{
srHandle.GetSchema(nativeSchema => schema = RealmSchema.CreateFromObjectStoreSchema(nativeSchema));
schema = srHandle.GetSchema();
}

return new Realm(srHandle, this, schema);
}

[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The Realm instance will own its handle")]
internal override async Task<Realm> CreateRealmAsync(RealmSchema schema, CancellationToken cancellationToken)
{
var configuration = CreateNativeConfiguration();
Expand Down Expand Up @@ -195,7 +194,7 @@ internal override async Task<Realm> CreateRealmAsync(RealmSchema schema, Cancell
var sharedRealmHandle = new SharedRealmHandle(realmPtr);
if (IsDynamic && !schema.Any())
{
sharedRealmHandle.GetSchema(nativeSchema => schema = RealmSchema.CreateFromObjectStoreSchema(nativeSchema));
schema = sharedRealmHandle.GetSchema();
}

return new Realm(sharedRealmHandle, this, schema);
Expand Down
Loading

0 comments on commit cfb9888

Please sign in to comment.