objectbox / objectbox-swift

Swift database - fast, simple and lightweight (iOS, macOS)
https://swift.objectbox.io
Apache License 2.0
452 stars 30 forks source link

App Crashes while ObjectBox Database sharing between main app and its extension #61

Open gl-lovekesh opened 2 years ago

gl-lovekesh commented 2 years ago

(https://stackoverflow.com/posts/71414769/timeline)

I was trying to share the Objectbox created database between the app and its extension. I used the below code to share the database between the app and its extension.

static func storeURL() -> URL {
        guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "AppGroupName") else {
            fatalError("Shared file container could not be created.")
        }

        return fileContainer.appendingPathComponent("\(databaseName)")
    }

The app is working fine when the app is running in foreground. When the app goes to the background then the app is crashing. When I remove the above code and used the below code

let appSupport = try FileManager.default.url(for: .documentDirectory,
                                                     in: .userDomainMask,
                                                     appropriateFor: nil,
                                                     create: true)
                                                     .appendingPathComponent(Bundle.main.bundleIdentifier!)
        let directory = appSupport.appendingPathComponent(databaseName)

The app does not crash and started working fine without Database sharing.

Note: If we ignore this crash, I was able to use the database from the app and its extension. We can not ignore it because it happens every-time.

Basic Infor Object Box : 1.5.0 iOS: iOS 15 and above Device: Any iPhone not simulator

Step to Reproduce

  1. Add database sharing code into your project.
  2. Run the app on real device.
  3. Put the app into the background.
  4. App crashed with Message from debugger: Terminated due to signal 9 Crashlog.pdf

Log

Incident Identifier: 98C56047-74B1-451A-9C3B-43A692895A71
CrashReporter Key:   072d5704b215a896d22637c9624964d0a2c21945
Hardware Model:      iPhone12,1
Process:             ContentApp [2126]
Path:                /private/var/containers/Bundle/Application/47344E9B-4903-4BEA-B37C-B6BB3A1EDB45/ContentApp.app/ContentApp
Identifier:          com.alfresco.contentapp
Version:             1.2.0-M1 (2)
Code Type:           ARM-64 (Native)
Role:                unknown
Parent Process:      launchd [1]
Coalition:           com.alfresco.contentapp [1024]

Date/Time:           2022-04-08 13:31:49.0834 +0530
Launch Time:         2022-04-08 13:31:17.0746 +0530
OS Version:          iPhone OS 15.3.1 (19D52)
Release Type:        User
Baseband Version:    3.01.02
Report Version:      104

Exception Type:  EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: RUNNINGBOARD; [3735883980]

Triggered by Thread:  0

Thread 0 name:   Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib                 0x1b7b93504 mach_msg_trap + 8
1   libsystem_kernel.dylib                 0x1b7b93b9c mach_msg + 76
2   CoreFoundation                         0x1805a7738 __CFRunLoopServiceMachPort + 372
3   CoreFoundation                         0x1805aba2c __CFRunLoopRun + 1212
4   CoreFoundation                         0x1805bf468 CFRunLoopRunSpecific + 600
5   GraphicsServices                       0x19c16338c GSEventRunModal + 164
6   UIKitCore                              0x182f625d0 -[UIApplication _run] + 1100
7   UIKitCore                              0x182ce0f74 UIApplicationMain + 364
8   ContentApp                             0x1028d6b10 main + 64
9   dyld                                   0x103729aa4 start + 520

Thread 1:
0   libsystem_pthread.dylib                0x1f10cae8c start_wqthread + 0

Thread 2:
0   libsystem_pthread.dylib                0x1f10cae8c start_wqthread + 0

Thread 3 name:  com.apple.uikit.eventfetch-thread
Thread 3:
0   libsystem_kernel.dylib                 0x1b7b93504 mach_msg_trap + 8
1   libsystem_kernel.dylib                 0x1b7b93b9c mach_msg + 76
2   CoreFoundation                         0x1805a7738 __CFRunLoopServiceMachPort + 372
3   CoreFoundation                         0x1805aba2c __CFRunLoopRun + 1212
4   CoreFoundation                         0x1805bf468 CFRunLoopRunSpecific + 600
5   Foundation                             0x181dddc84 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 236
6   Foundation                             0x181e1edb8 -[NSRunLoop(NSRunLoop) runUntilDate:] + 92
7   UIKitCore                              0x182edb74c -[UIEventFetcher threadMain] + 524
8   Foundation                             0x181e2d3fc __NSThread__start__ + 808
9   libsystem_pthread.dylib                0x1f10cb9a4 _pthread_start + 148
10  libsystem_pthread.dylib                0x1f10caea0 thread_start + 8

Thread 4 name:  com.google.firebase.crashlytics.MachExceptionServer
Thread 4:
0   libsystem_kernel.dylib                 0x1b7b93504 mach_msg_trap + 8
1   libsystem_kernel.dylib                 0x1b7b93b9c mach_msg + 76
2   FirebaseCrashlytics                    0x103c82a6c FIRCLSMachExceptionReadMessage + 80
3   FirebaseCrashlytics                    0x103c829ac FIRCLSMachExceptionServer + 48
4   libsystem_pthread.dylib                0x1f10cb9a4 _pthread_start + 148
5   libsystem_pthread.dylib                0x1f10caea0 thread_start + 8

Thread 5 name:  com.apple.NSURLConnectionLoader
Thread 5:
0   libsystem_kernel.dylib                 0x1b7b93504 mach_msg_trap + 8
1   libsystem_kernel.dylib                 0x1b7b93b9c mach_msg + 76
2   CoreFoundation                         0x1805a7738 __CFRunLoopServiceMachPort + 372
3   CoreFoundation                         0x1805aba2c __CFRunLoopRun + 1212
4   CoreFoundation                         0x1805bf468 CFRunLoopRunSpecific + 600
5   CFNetwork                              0x181035510 0x180dbb000 + 2598160
6   Foundation                             0x181e2d3fc __NSThread__start__ + 808
7   libsystem_pthread.dylib                0x1f10cb9a4 _pthread_start + 148
8   libsystem_pthread.dylib                0x1f10caea0 thread_start + 8

Thread 6:
0   libsystem_pthread.dylib                0x1f10cae8c start_wqthread + 0

Thread 7:
0   libsystem_pthread.dylib                0x1f10cae8c start_wqthread + 0

Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000010004005   x1: 0x0000000007000806   x2: 0x0000000000000000   x3: 0x0000000000000c00
    x4: 0x0000000000000e03   x5: 0x00000000ffffffff   x6: 0x0000000000000000   x7: 0x00000002810e41a0
    x8: 0x00000000fffffbbf   x9: 0x0000000007000906  x10: 0x1b2bfbd61fb100d4  x11: 0x000000b2f529e48a
   x12: 0x00000000016e3600  x13: 0x000000000086833a  x14: 0x0000000000000001  x15: 0x0000010000000102
   x16: 0xffffffffffffffe1  x17: 0x0000000000000100  x18: 0x000000014653b940  x19: 0x0000000000000000
   x20: 0x00000000ffffffff  x21: 0x0000000000000e03  x22: 0x0000000000000c00  x23: 0x000000016d646ad0
   x24: 0x0000000007000806  x25: 0x0000000000000000  x26: 0x0000000007000806  x27: 0x0000000000000e03
   x28: 0x0000000000000e03   fp: 0x000000016d646950   lr: 0x00000001b7b93b9c
    sp: 0x000000016d646900   pc: 0x00000001b7b93504 cpsr: 0x60000000
   far: 0x0000000111837800  esr: 0x56000080  Address size fault

Binary Images:
       0x1b7b92000 -        0x1b7bc5fff libsystem_kernel.dylib arm64e  <ff3ee512a0bc3123b983d365a71fb06a> /usr/lib/system/libsystem_kernel.dylib
       0x1805a0000 -        0x1809f3fff CoreFoundation arm64e  <e2f84645290531ef8ec73cc19c3cddb3> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
       0x19c162000 -        0x19c16afff GraphicsServices arm64e  <b40e3b1790c73ebcb6bc8c7b605f47a9> /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
       0x182a49000 -        0x1842d4fff UIKitCore arm64e  <920a0ee6d2663058814427a27b23ad62> /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
       0x1027b8000 -        0x102bbffff ContentApp arm64  <2c330856c5e63c468bea0c0fb63cea11> /private/var/containers/Bundle/Application/47344E9B-4903-4BEA-B37C-B6BB3A1EDB45/ContentApp.app/ContentApp
       0x103710000 -        0x103767fff dyld arm64e  <780553c7d39f322a8cb9f4f6de52610d> /usr/lib/dyld
       0x1f10ca000 -        0x1f10d5fff libsystem_pthread.dylib arm64e  <a17d07cfe86a392387c4921045898a8a> /usr/lib/system/libsystem_pthread.dylib
       0x181dc5000 -        0x1820cbfff Foundation arm64e  <925a43cdeaf2316193783ed87468301d> /System/Library/Frameworks/Foundation.framework/Foundation
       0x103c60000 -        0x103caffff FirebaseCrashlytics arm64  <ef0322c05d0d3ddbbba31b59b1831eec> /private/var/containers/Bundle/Application/47344E9B-4903-4BEA-B37C-B6BB3A1EDB45/ContentApp.app/Frameworks/FirebaseCrashlytics.framework/FirebaseCrashlytics
       0x180dbb000 -        0x18127ffff CFNetwork arm64e  <23a465f1e97c36c5a30fcefec059f79f> /System/Library/Frameworks/CFNetwork.framework/CFNetwork

EOF
greenrobot commented 2 years ago

Could you try the following (pseudo code) to open the store:

if Store.isOpen(directory) {
    store = Store.attachTo(directory)
else {
    store = Store(directory: directory)
}

If the app goes into background, it also may help to close the store.

gl-lovekesh commented 2 years ago

Hi @greenrobot, Thanks for your response, We tried your solution as below.

init() {
            do {
                let directory = URL.storeURL()
                if (try Store.isOpen(directory: directory.path)) == true {
                    self.store = try Store.attachTo(directory: directory.path)
                } else {
                    self.store = try Store(directoryPath: directory.path)
                }
            } catch let error {
                print("Unable to initialize persistence store: \(error)")
            }
        }

We are still getting crashes while going into the background. for reference: Shared app group directory path(/private/var/mobile/Containers/Shared/AppGroup/B2974F9C-4189-4597-8098-98A5171FC0FD/MobileAppDB)

ankitgoyalgloballogic commented 2 years ago

Hi Guys Any update on this? I am also getting the same issue.

gl-lovekesh commented 2 years ago

Could you try the following (pseudo-code) to open the store:

if Store.isOpen(directory) {
    store = Store.attachTo(directory)
else {
    store = Store(directory: directory)
}

If the app goes into the background, it also may help to close the store.

Any update on it? We got the same kind of crash on iOS 15 for realm for other applications. They fixed something. I tried to figure it out. I didn't get anything.

gl-lovekesh commented 2 years ago

Any update?

QL-Nisha-Rana commented 6 months ago

Any update on this? This is the major blocker now.

greenrobot-team commented 5 months ago

@nisha-rana16 Is this still an issue with the latest version (1.9.2)? I'm not familiar with app extensions, but upon reading extensions like widgets seem to run in a different process? If the app goes to the background, this might probably cause the database to close? Then it can not longer be accessed from the extension process? So maybe handling that case might avoid crashes? (Though, it's generally a bad idea to access the same database files from different processes.)

As for a workaround: is it possible to just pass data to the extension from the app and the extension does not touch the database itself at all?