Crash when db.close()

I’m seeing an intermittent crash after closing the db. I’m using Swift on iOS and couchbase-lite-swift-ee v2.8.4. Relevant parts of the log are below. My app calls db.close() and the crash comes quickly after that.

I’m looking for any advice on what’s going on.

Crashed: WebSocket to gateway.myserver.com:4984
0  CouchbaseLiteSwift             0x1152e8 litecore::repl::DatabaseCookies::DatabaseCookies(c4Database*) + 36
1  CouchbaseLiteSwift             0x155fcc FLSliceResult fleece::function_ref<FLSliceResult ()>::callback_fn<c4db_getCookies::$_0>(long) + 108
2  CouchbaseLiteSwift             0x12a000 FLSliceResult c4Internal::tryCatch<FLSliceResult>(C4Error*, fleece::function_ref<FLSliceResult ()>) + 168
3  CouchbaseLiteSwift             0x155fa0 c4db_getCookies + 64
4  CouchbaseLiteSwift             0xe0ec4 -[CBLDatabase getCookies:] + 21928
5  CouchbaseLiteSwift             0xbd7f4 -[CBLWebSocket _sendWebSocketRequest] + 33424
6  CouchbaseLiteSwift             0xbd1dc -[CBLWebSocket _connect] + 31864
7  libdispatch.dylib              0x631a4 _dispatch_call_block_and_release + 24
8  libdispatch.dylib              0x641a8 _dispatch_client_callout + 16
9  libdispatch.dylib              0x3f14c _dispatch_lane_serial_drain$VARIANT$armv81 + 604
10 libdispatch.dylib              0x3fc04 _dispatch_lane_invoke$VARIANT$armv81 + 388
11 libdispatch.dylib              0x495dc _dispatch_workloop_worker_thread + 616
12 libsystem_pthread.dylib        0x4e84 _pthread_wqthread + 284
13 libsystem_pthread.dylib        0x49f0 start_wqthread + 8

com.ecsg.couchbase_queue
0  libsystem_kernel.dylib         0x1c54 pread + 8
1  CouchbaseLiteSwift             0x195bb0 seekAndRead + 3356
2  CouchbaseLiteSwift             0x191b68 unixRead + 3512
3  CouchbaseLiteSwift             0x19f544 readDbPage + 22264
4  CouchbaseLiteSwift             0x19ef90 getPageNormal + 20804
5  CouchbaseLiteSwift             0x1e1778 getAndInitPage + 62088
6  CouchbaseLiteSwift             0x1b0f4c sqlite3VdbeExec + 18744
7  CouchbaseLiteSwift             0x1ab5a4 sqlite3_step + 272
8  CouchbaseLiteSwift             0x1cf9f4 sqlite3_exec + 168
9  CouchbaseLiteSwift             0x1aeb98 sqlite3VdbeExec + 9604
10 CouchbaseLiteSwift             0x1ab5a4 sqlite3_step + 272
11 CouchbaseLiteSwift             0x1cf9f4 sqlite3_exec + 168
12 CouchbaseLiteSwift             0x170248 SQLite::Database::exec(char const*) + 20
13 CouchbaseLiteSwift             0x1326d8 litecore::SQLiteDataFile::optimize() + 208
14 CouchbaseLiteSwift             0x13217c litecore::SQLiteDataFile::_close(bool) + 124
15 CouchbaseLiteSwift             0x11e690 litecore::DataFile::close(bool) + 108
16 CouchbaseLiteSwift             0x1662fc c4Internal::tryCatch(C4Error*, fleece::function_ref<void ()>) + 24
17 CouchbaseLiteSwift             0x120b04 c4db_close + 48
18 CouchbaseLiteSwift             0xdf9a8 -[CBLDatabase close:] + 16524
19 CouchbaseLiteSwift             0x2983c Database.close() + 4424734780
20 CouchbaseLiteSwift             0x29640 Database.close() + 4424734272
21 MobileCommon                   0x175508 closure #1 in CouchbaseDataStore.close() + 59 (CouchbaseDataStore.swift:59)
22 MobileCommon                   0x119b8 thunk for @escaping @callee_guaranteed () -> () + 20 (<compiler-generated>:20)
23 libdispatch.dylib              0x631a4 _dispatch_call_block_and_release + 24
24 libdispatch.dylib              0x641a8 _dispatch_client_callout + 16
25 libdispatch.dylib              0x3f14c _dispatch_lane_serial_drain$VARIANT$armv81 + 604
26 libdispatch.dylib              0x3fc04 _dispatch_lane_invoke$VARIANT$armv81 + 388
27 libdispatch.dylib              0x495dc _dispatch_workloop_worker_thread + 616
28 libsystem_pthread.dylib        0x4e84 _pthread_wqthread + 284
29 libsystem_pthread.dylib        0x49f0 start_wqthread + 8

Timer (Couchbase Lite Core)
0  libsystem_kernel.dylib         0x14f0 __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x58b8 _pthread_cond_wait$VARIANT$armv81 + 1216
2  libc++.1.dylib                 0x9350 std::__1::condition_variable::__do_timed_wait(std::__1::unique_lock<std::__1::mutex>&, std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000000l> > >) + 96
3  CouchbaseLiteSwift             0x15d258 void std::__1::condition_variable::__do_timed_wait<std::__1::chrono::steady_clock>(std::__1::unique_lock<std::__1::mutex>&, std::__1::chrono::time_point<std::__1::chrono::steady_clock, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000000l> > >) + 468
4  CouchbaseLiteSwift             0x15d000 litecore::actor::Timer::Manager::run() + 144
5  CouchbaseLiteSwift             0x15cf70 litecore::actor::Timer::Manager::run() + 462
6  CouchbaseLiteSwift             0x15cf64 litecore::actor::Timer::Manager::Manager()::$_0::operator()() const + 452
7  CouchbaseLiteSwift             0x15ceec void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, litecore::actor::Timer::Manager::Manager()::$_0> >(void*) + 332
8  libsystem_pthread.dylib        0x63a4 _pthread_start + 116
9  libsystem_pthread.dylib        0x49fc thread_start + 8

com.apple.main-thread
0  libsystem_kernel.dylib         0xb10 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x1134 mach_msg + 72
2  CoreFoundation                 0x6b60 __CFRunLoopServiceMachPort + 368
3  CoreFoundation                 0xac48 __CFRunLoopRun + 1184
4  CoreFoundation                 0x1de1c CFRunLoopRunSpecific + 572
5  GraphicsServices               0x19a0 GSEventRunModal + 160
6  UIKitCore                      0x4ec600 -[UIApplication _run] + 1080
7  UIKitCore                      0x281b08 UIApplicationMain + 2028
8  My Mobile App                  0x76d4 main + 17 (main.swift:17)
9  ???                            0x108378250 (Missing)

Stack trace looks similar to the one we already fixed, it might be a race with the replicator & database close.
https://issues.couchbase.com/browse/CBL-2657

This issue is already fixed in Lithium 3.0.0, which will be available soon.

Thanks. Good to know it’s already fixed. I see the stack trace is the same as in that bug report. This is my #1 crash in firebase.

My #2 crash also happens when closing the db. It crashes in sqlite function walIndexTryHdr. Apparently, this is a long-standing sqlite bug related to mmap of the wal-index file. I think the wal-index file is becoming inaccessible when the app goes to the background.

Is this also a known CBLite issue?