realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.3k stars 2.14k forks source link

Occasional "Permission denied" (FileAccess error) #3707

Closed floorish closed 8 years ago

floorish commented 8 years ago

Goals

Use/init the realm database on OSX

Expected Results

Realm instance is created

Actual Results

Realm throws an error: NSFilePath=/Users/**/Library/Containers/**/Data/Library/Application Support/default.realm, NSLocalizedDescription=Permission denied, Error Code=2

This looks to be a RLMError.FileAccess file I/O error

Steps to Reproduce

Some users of my app consistently get the above error, but I cannot reproduce this locally. The usual reboot Mac/reinstall app did not solve the issue. I have received the realm files from a user with this problem and tried to load them myself, but they work fine on my machine (same OS/Realm versions etc.). The user has used the app before and it worked fine, I'm not sure since when exactly the app couldn't load the files anymore. Any idea how I can debug what's going wrong on the user's Macs?

Code Sample

do {
   // create realm with default config
    let realm = try Realm()        
} catch {
    // Error Code=2
    Swift.print(error)
}

Version of Realm and Tooling

Realm version: 0.98.6

Xcode version: 7.3

iOS/OSX version: 10.10.4 / 10.10.5 (Have not got any reports on 10.11 so far)

Dependency manager + version: Carthage 0.15.2

jpsim commented 8 years ago

Any idea how I can debug what's going wrong on the user's Macs?

Sure! Here are some ideas of things you could check, either via additional logging, or getting your user to perform commands in their Terminal:

  1. Does the Realm file's parent directory already exist?
  2. If not, is it being created successfully?
  3. Is the Realm file being created for the first time, or did it already exist?
  4. If the file already existed, what were its permissions?
  5. What are the permissions of the parent directory?
floorish commented 8 years ago

Thanks for the suggestions.

  1. Does the Realm file's parent directory already exist? Yes, I have the default.realm files from the user (zipped & emailed)
  2. If not, is it being created successfully? It has been created successfully before
  3. Is the Realm file being created for the first time, or did it already exist? It did already exist & worked before
  4. If the file already existed, what were its permissions? Don't know yet
  5. What are the permissions of the parent directory? Don't know yet

I'll check the permissions, but if they're incorrect (no read or write access) Realm would throw RLMError.FilePermissionDenied instead of RLMError.FileAccess right? At least it gives a different error for me if I test that locally.

floorish commented 8 years ago

Permissions look fine to me:

ls -al@ ~/Library/Containers/appname/Data/Library/Application\ Support/

drwx------  13 username  staff    442 Jun  4 11:17 .
drwx------  29 username  staff    986 Jun  4 09:23 ..
-rw-r--r--@  1 username  staff  12288 May  9 15:51 default.realm
    com.apple.quarantine       23 
-rw-r--r--@  1 username  staff   1464 May  9 15:50 default.realm.lock
    com.apple.quarantine       23 
-rw-r--r--@  1 username  staff    168 May 11 11:54 default.realm.log
    com.apple.quarantine       23 
-rw-r--r--@  1 username  staff   4096 May  9 15:51 default.realm.log_a
    com.apple.quarantine       23 
-rw-r--r--@  1 username  staff   4096 May  9 15:50 default.realm.log_b
    com.apple.quarantine       23 
prw-------   1 root        staff      0 May 30 16:49 default.realm.note
jpsim commented 8 years ago

I don't know what else could be happening here, or how to help you further.

jpsim commented 8 years ago

@bdash brought up that using a tool like fs_usage might be helpful in logging file system events to determine which operations are failing. I'm not sure how well fs_usage handles mmapped files, but it's worth a shot.

Of course, this would only be useful if you can run this on a machine that can reproduce the problem, so this is dependent on your user being comfortable enough to run such a tool while the problem occurs.

bdash commented 8 years ago
prw-------   1 root        staff      0 May 30 16:49 default.realm.note

That file being owned by root seems like it would be the cause of the problem. If we're unable to open the FIFO I believe we'll throw an error of type RLMErrorFileAccess.

As to how it came to be owned by root… all I can say is that's not something Realm can do unless your app was launched as root.

floorish commented 8 years ago

Thanks for the help @jpsim & @bdash I don't know how I missed that file being owned by root... It seems to be the obvious problem (of course I tried chown-ing the default.realm and default.realm.lock files but not the .note :/ )

No clue how that happened either, the app should not be launched as root (not sure if that's even possible). Thanks again!