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

Realm sometimes crashes when writing if device is locked. #1874

Closed
anlaital opened this issue May 8, 2015 · 9 comments
Closed

Realm sometimes crashes when writing if device is locked. #1874

anlaital opened this issue May 8, 2015 · 9 comments
Assignees

Comments

@anlaital
Copy link

anlaital commented May 8, 2015

screen shot 2015-05-08 at 09 46 28

This seems to happen quite often when using Realm in response to background fetch request. I'm manually removing the file protection from the Realm files when first accessing it, but this does not seem to be sufficient to fix this issue.

Please consider having an option to completely opt-out of the platform-provided file protection, as it's completely unnecessary when using an encrypted Realm. I also have no way of knowing when the files are created by Realm, so I cannot respond to those by fixing the file protection attributes by hand.

Note that I'm just assuming this is caused by the default file protection attributes, as this only happens when the device is locked.

@anlaital
Copy link
Author

anlaital commented May 9, 2015

This actually seems to happen fairly often when doing background updates and is thus blocking us from auto-updating content for Apple Watch.

@segiddins
Copy link
Contributor

In the meantime, you'll need to also remove file protection from the lock and log files.

@anlaital
Copy link
Author

anlaital commented May 9, 2015

I am removing them from all the Realm files that I know of (.realm, .realm.lock, .realm.log, .realm.log_a, .realm.log_b and .realm.note). This doesn't seem to fix the issue entirely, however. I do this once every time the user signs in.

@anlaital
Copy link
Author

@segiddins Is there any way for you guys to create the files so that they do not have file protection set at all? This is causing a lot of issues for me now that I've enabled background updates. I have no way of knowing when Realm creates the files and therefore there are always scenarios that will crash when the app tries to access those files when the device is locked.

I would appreciate some help with this a lot!

@anlaital
Copy link
Author

I can reproduce this issue almost all the time. Before I'm opening the Realm, I'm removing file protection from all the Realm files that exist (note that .log files do not exist at this point):

default.realm
default.realm.lock
default.realm.note

Then immediately afterwards when I try to open the Realm, I get:

Opening the Realm threw an exception `open() failed: Operation not permitted`

Could you advise how to fix this without you adding support for completely removing file protection altogether.

@tgoyne
Copy link
Member

tgoyne commented May 11, 2015

Does disabling file protection for the directory work? The log files are created as needed and deleted when the realm is fully closed, which doesn't play well with setting the file protection in advance.

@tgoyne tgoyne self-assigned this May 11, 2015
@tgoyne
Copy link
Member

tgoyne commented May 11, 2015

Setting file protection for the containing directory does appear to work. I wrote the following bad test code:

    NSError *err = nil;
    [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionComplete} ofItemAtPath:[RLMRealm.defaultRealmPath stringByDeletingLastPathComponent] error:&err];

    [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:RLMRealm.defaultRealmPath error:&err];
    [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:[RLMRealm.defaultRealmPath stringByAppendingString:@".lock"] error:&err];
    [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:[RLMRealm.defaultRealmPath stringByAppendingString:@".log"] error:&err];
    [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:[RLMRealm.defaultRealmPath stringByAppendingString:@".log_a"] error:&err];
    [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:[RLMRealm.defaultRealmPath stringByAppendingString:@".log_b"] error:&err];
    [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey: NSFileProtectionNone} ofItemAtPath:[RLMRealm.defaultRealmPath stringByAppendingString:@".note"] error:&err];

    @autoreleasepool {
        [RLMRealm defaultRealm];
    }

    UIBackgroundTaskIdentifier task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:task];
    }];

    NSLog(@"ready");
    __block dispatch_block_t block;
    block = ^{
        @autoreleasepool {
            RLMRealm *realm = [RLMRealm defaultRealm];
            [realm beginWriteTransaction];
            [Person createInRealm:realm withValue:@[@"", @[]]];
            [realm commitWriteTransaction];
            NSLog(@"bg");
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
        }
    };
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), block);

which consistently throws the same exception when I lock the device with a passcode set. If I instead change NSFileProtectionComplete on the directory to NSFileProtectionNone, it works.

If you need other files to have file protection enabled, I suggest putting the Realm files in a subdirectory.

@tgoyne tgoyne added the pending label May 11, 2015
@anlaital
Copy link
Author

Thanks for the help. I'll start testing this today.

@realm-ci realm-ci removed the pending label May 12, 2015
@anlaital
Copy link
Author

It seems that removing file protection from the directory fixes this issue. I think you should document this as a "best practise" until a better solution is available. There does not appear to be any need to touch the individual Realm files residing in that directory so long as the directory itself has data protection set to NSFileProtectionNone or NSFileProtectionCompleteUntilFirstUserAuthentication.

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

No branches or pull requests

4 participants